Jump to content

How do I retrieve and display data according to a field name?


phpnoobie9

Recommended Posts

I have a table that I insert my categories into. I then have a form that lists those categories in a drop down menu so I can enter my data according to the category and sent to a different table 'images'. It is then sent to the database with the category name saved in a field called 'category' in the images table. I got the following code to display the list of categories in my category table:

 

				//Get categories
				$categories = 'SELECT * FROM categories ORDER BY name ASC';
				$catresults = mysql_query ($categories);
				$categoryname = 'SELECT category FROM templates WHERE (category=$categories)';

				while($categorylist = mysql_fetch_array($catresults)) {
				?>
                    
				<a href="<?php echo URL_PATH ?>indextest.php?category=<?php echo $categoryname ?>"><?php echo $categoryname['category'].'<br />'; ?></a>
				<?php
                    }*/

 

I want the list of categories in a link for example like:

Beach pictures

forest pictures

water pictures

car pictures...

etc..

 

When I click on the link I want to be able to retrieve everything in the image table that has the category according to the above.

How do I do that?

Very basic example:

<?php
if(isset($_GET['cat'])) {
    $r = mysql_query("SELECT * FROM `images` WHERE `category`='$_GET[cat]'") or die(mysql_error());
    while($rr = mysql_fetch_assoc($r)) {
        //Do whatever...
    }
} else {
    $r = mysql_query("SELECT * FROM `categories` ORDER BY `name` ASC") or die(mysql_error());
    while($rr = mysql_fetch_assoc($r)) echo "<a href='?cat=$rr[id]'>$rr[name]</a><br />";
}
?>

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.