a multi dimensional array needed?

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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

a multi dimensional array needed?

Post by robster »

Hi all,

I've just about given up trying to understand these arrays. I've searched and read tutorials and am getting headaches :(

If anyone has some advice I basically have this:

Code: Select all

//get the transactions that are of type 'stock' and that are within the date range and stick them in an array
	//===========================================================================
	mysql_select_db($dbname);
	$sql = "SELECT * FROM transactions WHERE type = 'stock' && date <= '$end_date'  && date >= '$start_date' ORDER BY id ASC";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$ShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$ShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$transaction_id = $Xcontent["id"];  
		$transaction_type_id = $Xcontent["type_id"]; 
		$transaction_price = $Xcontent["price"];
		
//now stick this stuff into an array
		$trans_multi_array = array("$transaction_id", "$transaction_type_id", "$transaction_price");
		
	$Xcontent = mysql_fetch_array($content);
	}
Now, does that work? Assuming there are 3000 products in the database table \, will they all be stuck into that array? And if so, how can I get them out? It appears using this kind of code (below) I can only extract the id and the type_id. But I suspect I'm not understanding fully.

Code: Select all

foreach($trans_multi_array as $transaction_id => $transaction_type_id) {}

How can I also access the price part of that array?

Any help REALLY appreciated...

Rob
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: a multi dimensional array needed?

Post by Christopher »

This would be cleaner. Note that I am only fetching the columns that you want and am using the DB column names throughout.

Code: Select all

//get the transactions that are of type 'stock' and that are within the date range and stick them in an array
	//===========================================================================
	mysql_select_db($dbname);
	$sql = "SELECT id,type_id,price FROM transactions WHERE type = 'stock' && date <= '$end_date'  && date >= '$start_date' ORDER BY id ASC";
	$content = mysql_query($sql);

	while ($row = mysql_fetch_array($content)) { 
//now stick this stuff into an array
		$trans_multi_array[] = $row;
		
	}

	foreach($trans_multi_array as $transaction) {
		echo 'id= . $transaction['id'] . ', type_id=' . $transaction["type_id"] . ', price=' . $transaction["price"] . '<br/>';
	}
(#10850)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

OMG... THAT is SO .... simple... sigh... :|

Why isn't there a tutorial on the net that spells it out like that.

Thank you so much, far out, I can't believe how hard they all make it sound!!!! ARRGHHHH, what a wasted day on arrays, and just like that I get it!


:)

big thanks from me.


Rob
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

robster wrote:Why isn't there a tutorial on the net that spells it out like that.
I'd recommend clicking around in the array section and thearray functions in the PHP manual.

Also of interest is that the code above is essentially the findAll() method in a Gateway class. You can see how it would nicely abstract a database by simply returning an array of rows that you can iterate over.
(#10850)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

absolutely!

Before reading this post I've been out and about, putting kids to bed, making meals etc and thinking of all the things I can do now I have my database tables (read slow) in my php array (read speed). This leads to the question (I'm a real example learner sorry, the manuals I find so hard)...

How can I use array_multisort?

I went through and got a bunch of totals and now have an array called $trans_draw_multi_array[] (great name no? :)) and it contains 0,1,2,3 key identifiers...

If I want to sort by 3 ASC, can I use array_multisort?

Thanks again, feeling much better about all of this already...

Rob
Post Reply