Jump to content

PHP add some string to filename in address bar and display


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.

Dont, know why you'd do this but. What you need to do is build the filename from the values in the ID and Cityname field from the database, eg

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

$linkFile = $city.'-'.$id.'.php';

echo "<a href=\"$linkFile\">$city</a>";

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 ...

hi adding to that I would like to see similar to this website (http://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/)

 

 

Where i see ID and my title of my post.

 

This is my exact requirement.

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

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
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.