Page 1 of 1

*Solved*multidimensional array w/o assigning to variable.

Posted: Tue Feb 20, 2007 10:38 pm
by skyrise85
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm trying to loop through some rows I've grabbed from the database and throw them into a multidimensional array.  I'm having trouble figuring out how to do it.  Below is what I have right now, but it is only saving the last row of information because "$b" doesn't work as an incremental variable.  It just keeps overwriting.  If anyone knows of a different way to do it, or how to fix my process please let me know!  THX for looking.

Code: Select all

$boxResults = mysql_query($queryBox) or die("box query failed");
		$numrowsBox = mysql_numrows($boxResults);

		if ($boxResults)
		{//start if-3
			$b=0;
			while ($b < $numrowsBox ) 
			{ //start while-1
				$brow = mysql_fetch_assoc($boxResults);
				$item = $brow['itemNum'];
				$description = $brow['desc'];
				$size = $brow['size'];
				$inches = $brow['inches'];
				$unit = $brow['unit'];
				$unitP = $brow['unitsPerBox'];
				$color = $brow['color'];
				$priceP = $brow['pricePerBox'];
				$optional = $brow['optional'];
				$plat = $brow['plat'];
				$boxArray = array( "$b" => array ("item" => $item, 
								"description" => $description, 
								"size" => $size, 
								"inches" => $inches, 
								"unit" => $unit, 
								"unitsPerBox" => $unitsP, 
								"color" => $color, 
								"pricePerBox" => $priceP, 
								"optional" => $optional, 
								"plat" => $plat, 
								)
								); //end boxArray
				$b++;
				}//end while-1

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Feb 21, 2007 12:25 am
by Kieran Huggins
how about:

Code: Select all

$boxResults = mysql_query($queryBox) or die("box query failed: ".MySQL_error()); 

while($row=MySQL_fetch_assoc($boxResults)){
  $boxArray[] = $row;
}

// look at the resulting array
print_r($boxArray);

Posted: Thu Feb 22, 2007 2:15 pm
by skyrise85
Thanks for the reply. Your solution seems to work. However it is extremely slow. Do you have any idea why? Also how do you access an element of the resulting array? For instance "ItemId" below.

Here's what I get with a print_r():

Code: Select all

Array ( [0] => Array ( [ItemId] => 10 [itemNum] => NEW [desc] => ITEM [size] => 889 [inches] => 89 [unit] => 67.00 [unitPerBox] => 57 [color] => kjh [pricePerBox] => 98.00 [optional] => jhvk [plat] => 98.00 ) )

Posted: Thu Feb 22, 2007 2:21 pm
by jyhm

Code: Select all

$ArrayName[0]['ItemID']

Posted: Thu Feb 22, 2007 2:37 pm
by Mordred
skyrise85 wrote:Thanks for the reply. Your solution seems to work. However it is extremely slow. Do you have any idea why?
My guess is that your DB query is not optimised. Print your $queryBox variable and paste it in phpMyAdmin and see what time it reports.
Also, the query may be good enough, but you are not LIMIT-ing the number of results and get a huge amount of rows.

Posted: Thu Feb 22, 2007 2:49 pm
by skyrise85
Thank you. For that to work it is forcing me to store in a variable before I echo. Is there anyway to skip storing and go straight to echo?

Posted: Fri Feb 23, 2007 2:19 am
by Mordred
skyrise85 wrote:Thank you. For that to work it is forcing me to store in a variable before I echo. Is there anyway to skip storing and go straight to echo?
It doesn't matter, it is temporary, only for testing. Of course you'll delete/comment the echo line afterwards.

Posted: Fri Feb 23, 2007 2:32 am
by skyrise85
I wasn't talking about a test echo.

I can't echo the array elements directly. I have to assign each element to a variable before i can display.

Posted: Fri Feb 23, 2007 2:52 am
by Mordred
skyrise85 wrote:I wasn't talking about a test echo.

I can't echo the array elements directly. I have to assign each element to a variable before i can display.
You should be able to, the array is a variable too ;)
Did you try what jyhm said (only you have to 'fix' it a bit):

Code: Select all

echo $ArrayName[0]['ItemID'];

Posted: Fri Feb 23, 2007 1:43 pm
by jyhm
Mordred wrote:You should be able to, the array is a variable too ;)
Did you try what jyhm said (only you have to 'fix' it a bit):

Code: Select all

echo $ArrayName[0]['ItemID'];
Opps, got me. Edited.

Posted: Fri Feb 23, 2007 7:00 pm
by skyrise85
Thanks for the help i'm going to title as solved.