georgebates Posted December 12, 2009 Share Posted December 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
emopoops Posted December 12, 2009 Share Posted December 12, 2009 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. Quote Link to comment Share on other sites More sharing options...
georgebates Posted December 12, 2009 Author Share Posted December 12, 2009 ok so how/where would i put that into my code? thanks Quote Link to comment Share on other sites More sharing options...
emopoops Posted December 13, 2009 Share Posted December 13, 2009 whenn the user selects the category they want to view like add it before or something if($_GET[category]){add my code here} Quote Link to comment 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.