amyk Posted December 23, 2010 Share Posted December 23, 2010 Hi guys.. 1st post here. I'm pretty new to php.. just a few weeks in. I've gotten pretty decent at making mysql connections and extracting data, but now I'm wanting to take one of my urls that I echo to my page and create a new page. Hoping someone can help... Here's the picture: MY MySQL Setup: I have 1 table called table1. It has 2 columns which are: 'Title' & 'Description' My Index.php Page: I connect to mysql. I pull the 'Title' from MySQL and loop it to produce 100 rows of data that fill my index page, as expected. My Problem: I now want to be able to click one of those rows and dynamically create a new page (description.php) that includes the 'Title' & 'Description'. My Progress: I've managed to click a row and create a hyperlink to mysite.com/$title (where 'title' is pulling the 'title' from the mysql db) but I all I get is page not found. My Question: How do I tell description.php to pull the 'Title' and 'Description' and link to it from index.php? My Gratitude: Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/ Share on other sites More sharing options...
Rifts Posted December 23, 2010 Share Posted December 23, 2010 basically this is how it works in your table you need id title description then you have mainpage use php to loop through your table creating links with like this <a href="articles.php?$row['id']">$row['title']</a> then your article page is really just a shell so you will use $_GET['id'] to grab which article was clicked then you can use a while loop using WHERE id=$_GET['id'] and pull that ids title and description thus filling in the shell page with whatever article was clicked. I hope that helps/makes sense its a very rough explanation and I dont really know how much you already know. If you want more help just let me know! Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/#findComment-1150937 Share on other sites More sharing options...
amyk Posted December 24, 2010 Author Share Posted December 24, 2010 That's pretty much exactly the information I was looking for so thank you for pointing me in the right direction! I'll try putting it into practice and holler back with any problems. Thanks again! Big help! Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/#findComment-1150980 Share on other sites More sharing options...
amyk Posted December 24, 2010 Author Share Posted December 24, 2010 Okay... so this worked perfectly for me. I am able to create the 'shell' page for my entire database. Thank you! Now that I have the URL, I was just wondering if it was possible to reformat the url of the new page? Currently, it looks something like: http://www.mydomain.com/description.php?id=the-title For SEO purposes, is there a way to reconstruct it as: http://www.mydomain.com/the-title Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/#findComment-1151157 Share on other sites More sharing options...
mmarif4u Posted December 24, 2010 Share Posted December 24, 2010 Yup, you can put this in the .htaccess file which will be in root directory. RewriteEngine on Options +FollowSymLinks #If base(root) directory is different , change the line below to RewriteBase /directory RewriteBase / RewriteRule ^([^/\.]+)/?$ description.php?id=$1 [L] In your php script: Description: <?php echo $_REQUEST['id']; ?> But make sure that mod_rewrite is enabled. Good luck! Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/#findComment-1151161 Share on other sites More sharing options...
amyk Posted December 24, 2010 Author Share Posted December 24, 2010 Excellent. You guys are good! Thanks very much! I can see how this could get addicting. Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/#findComment-1151197 Share on other sites More sharing options...
the182guy Posted December 24, 2010 Share Posted December 24, 2010 Use a trailing slash in the URL, this is actually better for SEO than without... http://www.mydomain.com/the-title/ Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/#findComment-1151212 Share on other sites More sharing options...
mmarif4u Posted December 25, 2010 Share Posted December 25, 2010 Use a trailing slash in the URL, this is actually better for SEO than without... http://www.mydomain.com/the-title/ This line should cover that: RewriteRule ^([^/\.]+)/?$ description.php?id=$1 [L] /?$ which means that trailing slash is optional, in case some one miss it. ? represents optional. So in this case both URL pattern will work like: http://domain.com/the-article http://domain.com/the-article/ It is because some time we miss the trailing slash, so to avoid that problem, just adding ? will make it optional. Link to comment https://forums.phpfreaks.com/topic/222533-trying-to-understand-how-to-create-a-dynamic-page-from-a-mysql-link/#findComment-1151309 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.