Potatis Posted July 16, 2008 Share Posted July 16, 2008 I'm creating a simple blog, but have got stuck on one thing. My blog database table has the fields: id, time, category, heading, text. Posting to the database is fine. From the reader's side, I want to have the categories listed in a menu for searching. The list is generated by a simple query from the database. I see the list of categories from test posts I have made to the database. How do I add a link to each category in the list so that I can "SELECT category from table WHERE category='$cat'" so to speak. I mean, I know if I made a php file to run that query, it'd be fine, but the category name is dynamic and I can't hard code it in any file because categories can be added at any time. How are these kinds of links made? Thanks for reading. Quote Link to comment https://forums.phpfreaks.com/topic/115053-solved-how-to-add-links-to-dynamic-categories/ Share on other sites More sharing options...
rhodesa Posted July 16, 2008 Share Posted July 16, 2008 you do it via a GET variable usually...so the links to the categories would be: http://www.host.com/list.php?cat=somecat http://www.host.com/list.php?cat=anothercat http://www.host.com/list.php?cat=foobar and the PHP would look like: <?php $cat = $_GET['cat']; //run query here ?> Quote Link to comment https://forums.phpfreaks.com/topic/115053-solved-how-to-add-links-to-dynamic-categories/#findComment-591657 Share on other sites More sharing options...
Potatis Posted July 16, 2008 Author Share Posted July 16, 2008 Ok, thanks. What is in list.php? The php code that you wrote? Quote Link to comment https://forums.phpfreaks.com/topic/115053-solved-how-to-add-links-to-dynamic-categories/#findComment-591667 Share on other sites More sharing options...
MadTechie Posted July 16, 2008 Share Posted July 16, 2008 yes the list.php is build from rhodesa php code you could also create HTML form, you could pass it when its submitted.. <form method="POST"> <input type="text" name="search"> <select name="cat"> <option value="cat1">category1</option> <option value="cat2">category2</option> <option value="cat3">category3</option> </select> <input type="submit" name="go"> </form> <?php if(isset($_POST['go'])) { echo "SELECT category: ".$_POST['cat']; } ?> an extra step would be building the drop down <input type="text" name="search"> <select name="cat"> <?php //select * from cat while($ROW = mysql_fetchrows($result)) { echo "<option value=\"{$ROW['id']}\">{$ROW['name']}</option>"; } ?> </select> *untested hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/115053-solved-how-to-add-links-to-dynamic-categories/#findComment-591671 Share on other sites More sharing options...
Potatis Posted July 16, 2008 Author Share Posted July 16, 2008 Thanks so much rhodesa and MadTechie, I've got it working now thanks to both of you! Quote Link to comment https://forums.phpfreaks.com/topic/115053-solved-how-to-add-links-to-dynamic-categories/#findComment-591678 Share on other sites More sharing options...
rhodesa Posted July 16, 2008 Share Posted July 16, 2008 One last note...i would go with GET instead of POST. That way the category name is in the URL and people can bookmark it/send the link to a friend. Quote Link to comment https://forums.phpfreaks.com/topic/115053-solved-how-to-add-links-to-dynamic-categories/#findComment-591680 Share on other sites More sharing options...
MadTechie Posted July 16, 2008 Share Posted July 16, 2008 Thats a very good point rhodesa, reminds me of my a ajax hell project lol Quote Link to comment https://forums.phpfreaks.com/topic/115053-solved-how-to-add-links-to-dynamic-categories/#findComment-591686 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.