1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help with arrays

Discussion in 'PHP' started by rishirajsingh, Sep 4, 2007.

  1. #1
    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
     
    rishirajsingh, Sep 4, 2007 IP
  2. James.Blant

    James.Blant Active Member

    Messages:
    250
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    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 )
     
    James.Blant, Sep 4, 2007 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    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:
     
    nabil_kadimi, Sep 4, 2007 IP
  4. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    sea otter, Sep 4, 2007 IP
    nabil_kadimi likes this.
  5. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    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.
     
    webrickco, Sep 5, 2007 IP
    sea otter likes this.
  6. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ^^^ 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.
     
    sea otter, Sep 5, 2007 IP
  7. rishirajsingh

    rishirajsingh Banned

    Messages:
    286
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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):
     
    rishirajsingh, Sep 6, 2007 IP