Re: Catagory list

From: Date: Sat, 31 Mar 2001 15:23:16 +0000
Subject: Re: Catagory list
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-46446@lists.php.net to get a copy of this message
This is what I'd do: $sql = "SELECT disinct category FROM article ORDER BY category"; $r = mysql_query($sql); while($cat = mysql_fetch_array($r)) { $id = $cat['id']; $sql = "SELECT * FROM article WHERE category='$id'"; $x = mysql_query($sql); while($row = mysql_fetch_array($x)) { // echo articles here } } The permanent solution is to make a separate table called "categories" and then do a join on the categoryID from the two tables. --Joe On Fri, Mar 30, 2001 at 10:33:11PM +1000, Mark Bayfield wrote: > I am trying to make a catagory list that displays a distinct catagory, and > then listing titles under that catagory. > Unfortunatly all the data is in one table, and the catagory field is not set > catagories, so there is duplicates in that field. > The code so far is (but I am probably going about it the wrong way, if you > can see whats wrong with my code plase help, or point me in some direction), > > $query = mysql_query("SELECT DISTINCT catagory from article order by > catagory"); > $query2 = mysql_query ("SELECT * from article ORDER BY catagory"); > > for ($index = 0; $index < mysql_num_rows($query); $index++) { > $Catagory = mysql_fetch_row ($query) or die (mysql_error()); > $name1 = mysql_fetch_row ($query2) or die (mysql_error()); > $x = $name1; > $result1 = array_unique($Catagory); > $result = array_merge($Catagory, $name1); > > print ("$result1[0]<BR>"); > for ($x=0; $x == $result1[0]; $x++) { > print ("&nbsp;&nbsp;&nbsp;&nbsp;<a > href='update.php?passed=$name1[0]'>$name1[2]</a><br>\n"); > } > } > > I am trying everything to get it do just print the following: > > Catagory > title1 > title2 > Catagory1 > title3 > title4 > > Please help me out.... > > thanks > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net /******************************************************************************\ * Joe Stump - PHP/SQL/HTML Developer * * http://www.care2.com - http://www.miester.org - http://gtk.php-coder.net * * "Better to double your money on mediocrity than lose it all on a dream." * \******************************************************************************/

« previous php.general (#46446) next »