montgomery Posted January 8, 2008 Share Posted January 8, 2008 Hi guys, there are a lot of tutorials about this subject on the net. But I still could not answer my question (requirement). I have a site in PHP5 running on an Apache Server. Right now my URLs are: mydomain.com/item.php?id=123 I found instructions to turn it into the URL: mydomain.com/item/123 But I want to write the title of the item in the URL: mydomain.com/This_is_the_best_Product I have no idea how to do this. Obviously the title comes from the database. What if by accident, the same title exists 2 times? I really appreciate you help! Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted January 9, 2008 Share Posted January 9, 2008 What if by accident, the same title exists 2 times? You just have to avoid this or present the two possible products if the query based on the product name returns two rows. Right now you must have something like this: item.php: mydomain.com/item.php?id=123 <?php $id = $_GET['id']; $get_product = mysql_query("SELECT * FROM products WHERE id = $id"); //display the product ?> If you want the product name instead you should just base your query on that: item.php: mydomain.com/item.php?product_name=123 <?php $product_name = $_GET['product_name']; $get_product = mysql_query("SELECT * FROM products WHERE product_name = $product_name"); //display the product ?> or you could allow both: item.php: mydomain.com/item.php?id=123 or mydomain.com/item.php?product_name=123 <?php $id = $_GET['id'] $product_name = $_GET['product_name']; if($id) { $get_product = mysql_query("SELECT * FROM products WHERE id = $id"); } elseif($product_name) { $get_product = mysql_query("SELECT * FROM products WHERE id = $id"); } else { echo "No product selected"; } //display the product ?> If the above didn't answer your question please explain more about exactly what you don't understand / don't know how to do Quote Link to comment Share on other sites More sharing options...
montgomery Posted January 9, 2008 Author Share Posted January 9, 2008 Thanks Wuhtzu! This is probably the first step. There is already a variable with the product title on the page. I probably do not have to query that. I think, there are some basic things, that I do not understand: Do I have to change all the links on other pages that link to my product page? Or, do I only have to change the code in the product page? In case the user types the URL "mydomain.com/this-is-the-product-title" in the browser, how does the server find the "mydomain.com/item.php?id=123"? I am lost :-) Quote Link to comment Share on other sites More sharing options...
trq Posted January 9, 2008 Share Posted January 9, 2008 In case the user types the URL "mydomain.com/this-is-the-product-title" in the browser, how does the server find the "mydomain.com/item.php?id=123"? Using mod_rewrite. Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted January 9, 2008 Share Posted January 9, 2008 Right now my URLs are: mydomain.com/item.php?id=123 I found instructions to turn it into the URL: mydomain.com/item/123 But I want to write the title of the item in the URL: mydomain.com/This_is_the_best_Product This sounds like you (he) knows how to use mod_rewrite to perform basic rewriting.... e.g. turning /123 into ?id=123. So that is now his problem. But I want to write the title of the item in the URL: mydomain.com/This_is_the_best_Product I have no idea how to do this. Obviously the title comes from the database. The id too comes from the database... right now you have just chosen to use the id to look up the product. Now you just have to use the product name to look up the product instead. This means making a rewrite rule that rewrites http://domain.com/Possibly_CamelCased_ProductName to http://domain.com/item.php?product_name=Possibly_CamelCased_ProductName (100% analog to what you said you found instructions for) and adjusting your script: <?php $product_name = $_GET['product_name']; $get_product = mysql_query("SELECT * FROM products WHERE product_name = $product_name"); //display the product ?> This is of course simplified as the product name with _ as separator probably have to be converted to use space as separator and convert all to lowercase ect.: <?php //Make the product name obtained from the url lower case and replace _ with space $product_name = strtolower(str_replace('_', ' ', $_GET['product_name'])) //Query the database for the product, making sure it check the lower case name against a lower case name $get_product = mysql_query("SELECT * FROM products WHERE LOWER(product_name) = $product_name"); ?> And yes, you have to change your links. For example your menu / navigation have to look like this now: <a href="/Nifty_Gadget">Nifty Gadget</a> <a href="/Smart_Self-Digging_Spade">Smart Self-Digging Spade</a> Again, explain your problem in more detail if I didn't answer your question Quote Link to comment 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.