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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

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

Post 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]
Last edited by skyrise85 on Fri Feb 23, 2007 7:01 pm, edited 2 times in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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);
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Post 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 ) )
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Code: Select all

$ArrayName[0]['ItemID']
Last edited by jyhm on Fri Feb 23, 2007 1:44 pm, edited 1 time in total.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Post 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?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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'];
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post 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.
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Post by skyrise85 »

Thanks for the help i'm going to title as solved.
Post Reply