Wolphie Posted December 19, 2007 Share Posted December 19, 2007 I'm not really sure how i'd go about writing a script with dynamic query strings in the URL. e.g. articles.php?cat=php&id=23 That will display the "PHP" category and the article inside that with the id "23" So examples of how to do this would be appreciated. Also, are clean URL's the way to go? If so, how would i go about using them? Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/ Share on other sites More sharing options...
papaface Posted December 19, 2007 Share Posted December 19, 2007 I would encode the parameters in the URL and make sure the variables are escaped. Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419024 Share on other sites More sharing options...
CMC Posted December 19, 2007 Share Posted December 19, 2007 are you using a database? Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419038 Share on other sites More sharing options...
Wolphie Posted December 19, 2007 Author Share Posted December 19, 2007 Ok, i'm pretty experienced with SQL strings etc....along with PHP itself. The thing i do not understand is how people use query strings within the URL itself dynamically. Yes i do use a database, and i would most likely encode them....depending on the kind of website i plan on developing. (My understanding is that encoded URL's are not particulary search engine friendly) Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419041 Share on other sites More sharing options...
trq Posted December 19, 2007 Share Posted December 19, 2007 A simple example. articles.php (lists all articles as links) <?php // connect to db. $sql = "SELECT id,title FROM articles"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<a href=\"article.php?id={$row['id']}\">{$row['title']}</a><br />"; } } } ?> article.php (displays article as requested via $_GET) <?php // connect to db. if (isset($_GET['id'])) { $id = mysql_real_escape_string($_GET['id']); $sql = "SELECT data,title FROM articles WHERE id = '$id' LIMIT 1"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); echo "<span class=\"title\">{$row['title']}</span>"; echo "<span class=\"data\">{$row['data']}</span>"; } } } ?> Theres no error handling or defaults, but should give you a good start. Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419046 Share on other sites More sharing options...
Wolphie Posted December 19, 2007 Author Share Posted December 19, 2007 I know that, i know the basics. But what i mean is, how would you write a complex URL query string. For example, include categories in the URL too. articles.php?cat=php&id=12 Rather than just the single parameter Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419050 Share on other sites More sharing options...
CMC Posted December 20, 2007 Share Posted December 20, 2007 Just name another $_GET variable. ex: articles.php?cat=cars&id=67 <?php $cat = $_GET['cat']; //pulls data from cat= $id = $_GET['id']; //pulls data from id= ?> Can use as many parameters as you want, you've just got to name them all. ex: pets.php?cat=dogs&type=poodle&color=black&id=8 <?php $cat = $_GET['cat']; $type = $_GET['type']; $color = $_GET['color']; $id = $_GET['id']; ?> And your right about non-friendly URL's. A good way around this is to use Apache's mod_rewrite feature. Following is a tutorial I found very helpful: http://www.workingwith.me.uk/articles/scripting/mod_rewrite Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419094 Share on other sites More sharing options...
Wolphie Posted December 20, 2007 Author Share Posted December 20, 2007 Does this require a different method of coding? (For clean URLs) Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419104 Share on other sites More sharing options...
teng84 Posted December 20, 2007 Share Posted December 20, 2007 Does this require a different method of coding? (For clean URLs) regex ! Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419106 Share on other sites More sharing options...
trq Posted December 20, 2007 Share Posted December 20, 2007 I know that, i know the basics. But what i mean is, how would you write a complex URL query string. For example, include categories in the URL too. Im not sure what you mean by complex, the principle is exactly the same. <?php // connect to db. $sql = "SELECT id,cat,title FROM articles"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<a href=\"article.php?cat={$row['cat']}&id={$row['id']}\">{$row['title']}</a><br />"; } } } ?> As for clean urls. I think your talking about mod_rewrite, this is handled via apache, not php. Quote Link to comment https://forums.phpfreaks.com/topic/82417-query-strings-in-url/#findComment-419113 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.