Page 1 of 1

a multi dimensional array needed?

Posted: Wed Apr 19, 2006 10:37 pm
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

Re: a multi dimensional array needed?

Posted: Wed Apr 19, 2006 10:58 pm
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/>';
	}

Posted: Thu Apr 20, 2006 12:57 am
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

Posted: Thu Apr 20, 2006 2:08 am
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.

Posted: Thu Apr 20, 2006 8:27 am
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