Jump to content

PHP add some string to filename in address bar and display


durgaprasadcs
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello ,
 
I have two fields in database (ID , CITYNAME ) like below
 
ID    |  CITYNAME

----------------------
123 |  DELHI

----------------------

124 | MAHARASHTRA

----------------------

125 |  PUNJAB

----------------------

 

I am trying to display records based on id number in a file called viewrecords.php.
 
where i am passing ID parameter (http://www.example.com/viewrecords.php?ID=123) and display the record in viewrecords.php file.
 
It simply displays the records where the corresponding id is matches.
 
Now I have a requirement where i need to display a city field ( for example http://www.example.com/cityname-ID.php that is http://www.example.com/delhi-123.php) in the addressbar instead of viewrecords.php.

 

any help appreciated.

Link to comment
Share on other sites

Hi Thanks for the reply.

 

in my end both city name and is dynamic where city user enters. so I need to create the page in run time. 

 

So I see two possibilities here..

 

one is to create the filename entirely in runtime.

 

second one is to have the filename already like $city-$id.php

 

But i am not sure these are possible. 

 

Please let me know ...

Link to comment
Share on other sites

Oh now I understand. 

What you'll want to look at is mod_rewrite. You don't dynamically create new files on the fly. 

 

So if you want your urls to be site.com/delphi-123 (like this forum) you'd use the following mod_rewrite rule a in a .htaccess file

RewriteEngine On

RewriteRule ([a-z+])-([0-9+)/ viewrecords.php?ID=$2&cityname=$1 [NC]

The above rewrite rule will match a url like site.com/delphi-123 and then pass delphi and 123 as querystring parameters to viewrecords.php.

 

 

Now for your code create that url you'd use this

$id   = $row['ID'];
$city = $row['CITYNAME'];


echo "<a href=\"/$city-$id/\">$city</a>";
Edited by Ch0cu3r
Link to comment
Share on other sites

Hi,

 

I understand the concept you explained. But however If i use the above code, my entire site shows as 500 Internal Server Error.

 

Hi I tried the following code :  just alone with ID.

 

RewriteEngine On

RewriteRule (.*)/$ load_page.php?&page_id=$1

 

 

it is working with slash in end of the url.  ( http://www.example.com/384/) 

and not working without the slash in the end of the url. (http://www.example.com/384)

 

any suggestions 

Edited by durgaprasadcs
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.