Jump to content

Only display the category user chooses.


georgebates

Recommended Posts

Hi there,

 

I found some code that get information from a database and prints it into a table. Each entry in the database had three columns of information. It does this fine. But the way it was made is so that it just puts all the entries in the database in the table sorted in groups of their category. I want it to be so that user chooses which category they want to see and it only displays the entrys which are only in that catergory. Heres the code that does that:

<?php 

$myResult = mysql_query('SELECT ref_links.*, ref_categories.category_name FROM ref_links, ref_categories WHERE ref_links.ref_categories_id=ref_categories.id ORDER BY ref_categories_id, ref_links.topic_name', $connectID)
  or die ("Unable to select from database");

while ($row=mysql_fetch_array($myResult, MYSQL_ASSOC)) {
  $thisCat= $row['category_name'];
//print the category heading, but only once for the listings in that catagory
if ($lastCat<>$thisCat) { // true first time($lastCat not set), and each time a new category is found
			print "<h2>".$row['category_name']."</h2>";  // print the next category heading
	}
	print '<div id="ref_links">';
	print "<h3>".$row['topic_name']."</h3>\n";
	print "<p>Price: ".$row['topic_desc']."</p>\n";
	print '<br/>'."\n";
	print "<a href=".$row['topic_url']."><img src=".$row['topic_url']." /></a>";
	print '<p><a href="art_inquiry_form.html">Inquire</a></p>'."\n";

  $lastCat = $row['category_name']; // record which category this listing was in so we can test if the cat. changes
  
    print "</div>\n";  //end of ref_links div
}
?>

 

I think it might be in this bit that needs changing.

 

$myResult = mysql_query('SELECT ref_links.*, ref_categories.category_name FROM ref_links, ref_categories WHERE ref_links.ref_categories_id=ref_categories.id ORDER BY ref_categories_id, ref_links.topic_name', $connectID)
  or die ("Unable to select from database");

 

I'm new to PHP so some help would be greatly appreciated.

 

Thanks

 

Link to comment
Share on other sites

simple. u use well... u use;;

$numberofrows =mysql_num_rows(ur query here) OR the count(any column) thing in a mysql_query

to see how many rows ur going to have to output.

the u do a  $thequery = mysql_query("SELECT specificcategorycolumn FROM table")

and then u do

for($i = 0; $i < $numberofrows; $i++)
{$thefielddata = mysql_result($thequery, $i); echo $thefielddata;}

it will start from the first row  and go to the last in whatever order u specify in the query aka ORDER BY id DESC or whatever u should know what i mean.

 

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.