My table structure is like ----------------------------------------- |category | Products | |---------------------------------------- |category1 | product1 | |category1 | product2 | |category1 | product3 | |category1 | product4 | |category1 | product5 | |category1 | product6 | |category2 | product7 | |category2 | product8 | |category2 | product9 | |category2 | product10 | |category3 | product11 | |category3 | product12 | |category3 | product13 | |--------------------------------------- I want to select all products from table and arrange them in categories. I am using two dimensional array and then foreach loop to do it but not able to suceed. Please help with logic and code. I want to print my products like this. Category1 : Product1 Product2 Product3 Product4 Product5 Product6 Category2: Product7 Product8 Product9 Category3: Product10 Product11 Product12 Product13
i think you should load category names on array like : $ar_cat['1']="category1"; PHP: (it will assign in a loop automatically ) now you should load products with a SQL that have a condition . $sql="SELECT products FROM `table` WHERE category = '$ar_cat['1']"; PHP: i will write a sample for you as soon ( because i'm soo busy this days )
I hope this will help you: <?php $categories = mysql_query(" SELECT category FROM table_name GROUP BY category "); while($category=mysql_fetch_array($categories)){ echo "$category[category]:<br />"; $products_on_category=mysql_query(" SELECT products FROM table_name WHERE category LIKE '$category[category]' "); while($product=mysql_fetch_array($products_on_category)){ echo "$product[products]<br />"; } } ?> PHP:
I think it's better to get all the data from mysql in one shot (instead of constantly hammering the db on a category basis) and then post-filtering in PHP. The following code does just that, resulting in a two-dimensional array as you wanted: $inventory['category1'] => array('product1','product2'); $inventory['category2'] => array('product1','product2'); Code (markup): This way, you can get at each category's products using regular array indexes: $inventory['category1'][0]; // would be 'widget' or whatever $inventory['category2'][16]; // would be 'brass pipe' or whatever count($inventory); // how many categories are there? count($inventory['category1']) // how many products in category 1? Code (markup): Here's the actual code: // connect to mysql, choose the database $conn = mysql_connect('localhost','name','password'); mysql_select_db('inventory'); // make the query, ordering by category and product $result = mysql_query('SELECT * FROM inventory ORDER BY category, product'); // set up our data structure $inventory = array(); // our current category name placeholder $cur_category=''; // get all the rows while (($row=mysql_fetch_array($result)) !== false) { // start a new category if ($row['category'] != $cur_category) { // not first time? add our newly-populated category if ($cur_category) $inventory[$cur_category]=$products; // now start the new category's product array $products=array($row['product']); // and set the new category's name $cur_category=$row['category']; } else // add to existing category $products[] = $row['product']; } // the final category $inventory[$cur_category]=$products; // close the handle mysql_close($conn); PHP:
When you load an array into a memory structure, please always keep in mind server load issues. A table of 1000 rows of 100 bytes accessed by 1000 users simultaneously would generate almost 100mb of memory consume, which is enormous in the case of a share server.
^^^ You're right, and that load would be incurred by the OP's code as well as my own. If the OP has many categories and products, the ideal things would be to first retrieve only a list categories and the product count within them, e.g. select category, count(*) from inventory group by category Code (markup): And then, when the user drills down into a category, page the results via a LIMIT clause on the select.
I am using this code after query and its working fine, thanks friends <?php $lastCat = ' '; while(//there are more rows){ $cat = $row['category']; if($cat != $lastCat){ // it's a new category print $cat; } $lastCat = $cat; print $row['product']; } ?> Code (markup):