durgaprasadcs Posted February 23, 2014 Share Posted February 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/ Share on other sites More sharing options...
Ch0cu3r Posted February 23, 2014 Share Posted February 23, 2014 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/#findComment-1470260 Share on other sites More sharing options...
durgaprasadcs Posted February 23, 2014 Author Share Posted February 23, 2014 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/#findComment-1470275 Share on other sites More sharing options...
durgaprasadcs Posted February 23, 2014 Author Share Posted February 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/#findComment-1470280 Share on other sites More sharing options...
Ch0cu3r Posted February 23, 2014 Share Posted February 23, 2014 (edited) 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 February 23, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/#findComment-1470282 Share on other sites More sharing options...
durgaprasadcs Posted February 23, 2014 Author Share Posted February 23, 2014 (edited) 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 February 23, 2014 by durgaprasadcs Quote Link to comment https://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/#findComment-1470289 Share on other sites More sharing options...
Solution Ch0cu3r Posted February 23, 2014 Solution Share Posted February 23, 2014 Sorry, my rewriteRule should of been RewriteRule ([a-z]+)-([0-9]+)/? viewrecords.php?ID=$2&cityname=$1 [NC] Quote Link to comment https://forums.phpfreaks.com/topic/286451-php-add-some-string-to-filename-in-address-bar-and-display/#findComment-1470299 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.