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?

Link to comment
Share on other sites

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 />";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.