Re: Any ideas?

From: Date: Sun, 02 Jul 2000 16:37:06 +0000
Subject: Re: Any ideas?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-809@lists.php.net to get a copy of this message
I had a similar situation. First off, for good database design you might consider moving 'category' off into it's own table then referencing a category_id from the category table in the product table which would still include price, description and a product_id as well. Makes the queries a bit more complex but it's much easier to add / change categories later on in your db. Regardless, the following code should come close to what you want. Make sure your query sorts the results by category or this won't work. // In the following, $array holds all my results. // $array[0]["category"] is my first category, // $array[1]["category"] is my second category, etc.. // Before anything intitalize the $catname var to ""; // $catname remembers our current category which is // empty at first. $catename = ""; // Do the query and put the results into $array // This lacks any error checking for this example $db ="mysql_db_name"; $sql "select category,price,description from mytable order by category;" $dbh = @mysql_connect($db_host,$db_user,$db_passwd); $results = mysql_db_query($db,$sql,$dbh); for($i = 0; $i < mysql_num_rows($results); $i++) { $array[$i] = mysql_fetch_array($results); } mysql_close($dbh); // Let's loop through the results which are storied in $array. for ($i = 0; $i < count($array); $i++) { if ($array[$i]["category"] != $catname) { echo ' // The current category item doesn't equal the saved category name. // Time to make a new category header and save the new category // name for later comparison. echo "\n\n<H1>".array[$i]["category"]."</H1>"; $catname = $array[$i]["category"]; } // Here's what's under each category. echo "\n<P>".$array[$i]["description"]." - ".$array[$i]["price"]; } That's it. Hope it helps. -- Scott On Mon, Jul 03, 2000 at 10:28:27AM +0930, Shannon Doyle wrote: > Hi people, > > Any ideas on how I can achieve the below scenario? > > I am using PHP3 and MYSQL > > 1 table containing product description, price and category. > > I can do a simple query to dump out all the data in tabluar form. ie > description, price, category > > However, I need the data to be in the form of: > > Category > description price > description price > > category 2 > description price > description price > > etc etc. > > Is there a way that I can get the category field from the table and use it > as a table header? then under this displaying the products that only fall > into that category? > > A code example might be useful here as I am fairly new to both php and MYSQL > > Thanks in advance. > > Shannon > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net > For additional commands, e-mail: php-db-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net -- Scott

« previous php.db (#809) next »