Jump to content

[SOLVED] How to add links to dynamic categories


Potatis

Recommended Posts

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.

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
?>

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.