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.

Creating multidimensional array in MYSQL loop

Discussion in 'PHP' started by zoomzoomzoom, Oct 1, 2010.

  1. #1
    Hello everyone, I've been trying to solve this for couple of days now. Here's where I'm at:

    
    foreach ($contents as $id=>$qty) {
       $query = mysql_query("SELECT * FROM items WHERE item_id='$id'") or die(mysql_error());
       while ($result = mysql_fetch_array($query)) {
          //IN CASE IT'S THE FIRST ELEMENT IN ARRAY 
          if(!isset($_SESSION['price_fix'])){ 
             $$result['item_id'] = array($result['item_id'],$qty,$price); 
             $woohoo = $$result['item_id'];
          //FOR EVERYTHING ELSE
          }else{
             $$result['item_id'] = array($result['item_id'],$qty,$price); 
             $woohoo = ''.$woohoo.','.$$result['item_id'].''; 
          }
          $_SESSION['price_fix'] = array($woohoo);
       }
    }
    
    PHP:
    I need to get array like so:
    $_SESSION['price_fix'] = array( array(item_id1, $qty1, $price1),array(item_id2, $qty2, $price2),array(item_id3, $qty3, $price3) );

    I think I'm on the right track as first element goes in as suppose to. The problem is within else brackets- I can't figure out how to join in properly if there are more than 2 items in array.

    Hope this makes any sense to you.
     
    zoomzoomzoom, Oct 1, 2010 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Try following code and let me know if you face any difficulty.

    As per my understanding, I have removed unwanted code.

    
    $result = array();
    foreach($contents as $id => $quantity)
    {
    	$rs = mysql_query("SELECT * FROM items WHERE item_id='$id'");
    	while($row = mysql_fetch_array($rs))
    	{
    		$result['item_id'] = array($row['item_id'], $quantity, $row['price']);
    	}
    }
    $_SESSION['price_fix'] = $result;
    
    PHP:
     
    mastermunj, Oct 1, 2010 IP
  3. zoomzoomzoom

    zoomzoomzoom Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for help but it doesn't work as it produces following array:
    Array ( [item_id] => Array ( [0] => 32 [1] => 1 [2] => 35.00 ) )

    and takes into array only last item out of three.
     
    zoomzoomzoom, Oct 2, 2010 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Okay, following should add all elements in array.

    
    $result = array();
    foreach($contents as $id => $quantity)
    {
        $rs = mysql_query("SELECT * FROM items WHERE item_id='$id'");
        while($row = mysql_fetch_array($rs))
        {
            $result[] = array($row['item_id'], $quantity, $row['price']);
        }
    }
    $_SESSION['price_fix'] = $result;
    
    PHP:
     
    mastermunj, Oct 2, 2010 IP
  5. zoomzoomzoom

    zoomzoomzoom Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, it's as simple as 1,2,3... Everything as in my code except whole else statement suppose to look like this:
    
    else{
       $$result['item_id'] = array($result['item_id'],$qty,$price); 
       array_push($woohoo, $$result['item_id']); 
       print_r ($woohoo);
    }
    $_SESSION['price_fix'] = $woohoo;
    
    PHP:
    Hope this will help someone.
     
    zoomzoomzoom, Oct 2, 2010 IP