954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

create dynamic link without creating new php page??

hi,

I am created 3 tables for
1> category
2> subcategory
3> projectdetail

In my website folder structure

root-index.php, header.php footer.php

folder- about -index.php and other files

folder - project- index.php

folder - err- 404.php

In my php page project/index.php

i make left column dynamic, i.e call value from table

e.g

School project ( this is category) and href link is project/School project
xyz (subcategory) and href link is project/xyz
abc and href link is project/abc

College project
www
yyy

I want do like when click on link project/School project
It should display that project
but id gives me error 404 page.
Not to create folder for school project or xyz or abc

How to do like when click on link it should display that value from database.
How create dynamic link without creating new php page??
How to do that??

my .htaccess file is

Options +FollowSymLinks

RewriteEngine On
ErrorDocument 404 http://www.aaa.com/err/404.html
RewriteRule ^([^.]+).html$ $1.php [QSA,L]

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

you can create a dynamic link on the bases of field name,id like you have project.php and there are three projects in db
e.g
fetch from db and put in a variable

$project_name='english'; 
$project_name='math';
$project_name='science';


for three courses saved in database

now you want to show details on project.php with different urls

so link will be <a href="www.mysite.com/projects/<?$project_name?>/" ><?$project_name?></a> as href or page action

and use htacces file
for url rewriting like

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^projects/(.*)/$ project.php?project_id=$1 [L]


request that project id in new page as $project_id=$_REQUEST['project_id'];

and do what ever you want

now you are using one php page project.php
but there will be three different links www.mysite.com/projects/english/
www.mysite.com/projects/math/
www.mysite.com/projects/science/

these examples are just to give you idea

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

hi,

In my folder - project- index.php

Fetching all data

<a href="www.mysite.com/projects/<?$project_name?>/" ><?$project_name?></a>

It shows like

link:- www.mysite.com/projects/School project/ name:- School project
link:- www.mysite.com/projects/xyz/ name:- xyz
link:- www.mysite.com/projects/abc/ name:- abc


My .htaccess file

Options +FollowSymlinks 

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^projects/(.*)/$ index.php?project_id=$1 [L]

RewriteRule ^([^.]+).html$ $1.php [QSA,L]
RewriteRule ../index.html [L]


when click on link it gives error
The requested URL was not found on this server.

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

My question is you are giving $project_name in link
so how you get

$project_id=$_REQUEST['project_id'];


please explain??

rush1
Newbie Poster
9 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

No you are not getting my point

Look when you are using link for example

<a href="www.mysite.com/projects/<?$project_name?>/" ><?$project_name?></a>


the link made by this should be as www.mysite.com/projects/School-project/

the spaces are not allowed in url so if you have space in your project name use string replace function and replace them with "-" not underscors as "-" is more effective for search engines.

when we will go at htacces file as

Options +FollowSymlinks 

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
<strong>RewriteRule ^projects/(.*)/$ index.php?project_id=$1 [L]</strong>


understand the meaning of bold url rewrite rule

that means when it got www.mysite.com/projects/ and (.*)=parameter that passes and that is project name means

projects/(.*)=www.mysite.com/projects/School-project/

then redirect this url to index.php and the project name that is 1st parameter put the value of that in project_id variable

means

at this stage project_id='School-project';

now request the project_id variable on detail page

$project_id=$_REQUEST['project_id'];

this $project_id is just variable name it contains the value of project name not ID

now again apply string replace function on $project_id and replace that "-" with "%" that will help you to apply LIKE command for sql quesry

after apply replace function it will be $proj_name="school%project";

now apply the select query like

select project_id from table-project where project_name <strong>like</strong> '$proj_name% or 

project_name like 'school%name%'


that will match each word

now you will get the ID of that project name and use that if for your required results.

All this process is for proper url re-writing for dynamic urls

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

hi,
can you please explain briefly...with example...
I am doing same as you suggested...

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

Aamit i can explain it briefly with example but now i have to go. if you can wait i can send you the example tomorrow.

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

can you wait

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

Yes. sure

Can you post full code with htaccess??

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

hello amit. i have done it for you. i am attaching a folder. use it at your localhost. and tell me whats the results.

i have made a database with single table. database backup is also in the folder. just use the database and run the file.

htaccess file is also there. All the work is done. just see the folder.

i am here for further help

Attachments project_test.zip (2.84KB)
BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

Wait for my replay i am going through your code...

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

ok m here but hurry up... :)

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

Thank You Thank You Thank You Thank You Thank You
Very Much...

You r rock star...

It perfectly work on localhost...

I integrating on live if i found any difficulty with htaccess i will tell you

Thanks for your help...god bless you

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

:) no no its really ok. I always feel so happy when i be able to help others. please feel free to ask any question.

i will be happy if you will write your kind remarks in my reputation

Add to BzzBee's Reputation about my posts

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

hi,
In htaccess

RewriteEngine on
RewriteBase /project_test/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1 [L]

and my project_detail.php file at http://localhost/demo/abc/index.php
http://localhost/demo/abc/project_detail.php
when click on link
http://localhost/demo/abc/projects/school-project/
IT gives page not found

I think some modification needed at this line

RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1 [L]


plz tell me what to do??

Aamit
Posting Whiz
342 posts since Apr 2008
Reputation Points: 3
Solved Threads: 15
 

hi, In htaccess

RewriteEngine on
RewriteBase /project_test/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1 [L]

and my project_detail.php file at http://localhost/demo/abc/index.php http://localhost/demo/abc/project_detail.php when click on link http://localhost/demo/abc/projects/school-project/ IT gives page not found

I think some modification needed at this line

RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1 [L]

plz tell me what to do??


Try making the following your .htaccess file:

RewriteEngine on

RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1

Also make sure that mod_rewrite is enabled in the apachie configurations.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

hi, In htaccess

RewriteEngine on
RewriteBase /project_test/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1 [L]

and my project_detail.php file at http://localhost/demo/abc/index.php http://localhost/demo/abc/project_detail.php when click on link http://localhost/demo/abc/projects/school-project/ IT gives page not found

I think some modification needed at this line

RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1 [L]

plz tell me what to do??

hello amit

look, when we are in root directory meanswww the rewrite base will be as

RewriteBase /

when you are in demo folder with in the root directory so the rewite base will be as

RewriteBase /demo/


as same in your case it should be

RewriteBase /demo/abc/


now try it

RewriteEngine on
RewriteBase /demo/abc/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1 [L]
BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

RewriteEngine on

RewriteRule ^projects/(.*)/$ project_detail.php?pname=$1

GameGape.com
Newbie Poster
3 posts since Apr 2009
Reputation Points: 10
Solved Threads: 1
 

hello Amit,

Is you issue solved or not?

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

BzzBee that script worked great !! I do have a question how can we add title and description to metag tags. for each page created example page:
mydomain.com/web-design-software/
I would want to have Title: Web Design Software
Description: All you need to know about Web Design

I tired echo

<title><?php echo $pname ?> </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="<?php echo $project_other_info ?>" />
  <meta name="keywords" content="<?php echo $tagz=$rowp['tagz']; ?>" />


But that just brings the title as web-design-software
And the Description - Keywords just shows blank.

Any help would be appreciated .
Thanks

JEFEX13
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You