Page 1 of 1

[SOLVED]late nite multi dimensional array blues

Posted: Fri Jul 30, 2004 7:46 pm
by phait
hey all,
wondered if someone could point me in the direction...

I have a query that pulls out 'n' number of stoppages associated to one or more waterways.

A sample query might be such as:

Code: Select all

<?php
SELECT stoppages.type, stoppages.name, stoppages.wway_id, waterways.name AS wwayname FROM stoppages LEFT JOIN waterways ON stoppages.wway_id = waterways.id WHERE stoppages.wway_id = 253 OR stoppages.wway_id = 2 OR stoppages.wway_id = 3 OR stoppages.wway_id = 4 AND stoppages.start_date >= "2004-07-31"
?>
which returns the following results:

Code: Select all

type: name: wway_id: wwayname: start_date:-
r, this is the third stoppage, 276, River Brue, 2004-10-10
s, this is the sixth stoppages, 3, Ashby Canal, 2004-10-10
r, this is the seventh stoppage, 3, Ashby Canal, 2004-08-09
r, this is the eigth stoppage, 3, Ashby Canal, 2004-09-29
I am putting the data into an array:

Code: Select all

<?php
while($data = mysql_fetch_array($result, MYSQL_ASSOC)) {
    
    //create an array whose index is the waterway name
    //then create a sub array to hold each stoppage details for that waterway

	$search_results = array($data['wwayname'] => array( "type" => $data['type'], "name" => $data['name']) );

} // wend
?>
And I'm trying to write out the results like so:

Code: Select all

<?php

if( is_array($search_results) ) {
	//loop over $search_results array
	print_r($search_results);

	foreach($search_results AS $wwayname => $val) {
		$html .= "$wwayname : $val[type] : $val[name]<br />";
	
	}
}
else {
	$html .= '<p>No results found</p>';
}
?>
The problem was that even though the results in mysql show that there are 4 records I was only getting the last one. I think I have two problems.

One is that if I have more than 1 stoppage on a waterway it will overwrite a previous entry in the array for a waterway with the same name .e.g:

Code: Select all

<?php
$search_results = array($data['wwayname'] => array( "type" => $data['type'], "name" => $data['name']) );

//could be translated as

$search_results = array(Ashby Canal => array("type" => r, "name" => this is the seventh stoppage) );

// which is then written over by the eigth stoppage

?>
I think the second problem then resides in how to write out the data. I need to write the waterway name and then iterate out all the stoppages for that waterway before moving onto the next waterway and any stoppages on that waterway.

Anyone pass on any hints where I could hold multiple stoppage records per waterway and then how to iterate over the array created to geth the data written to the screen?

Sorry for the verbosity, but I thought better to give too much info than too little.

TIA

Posted: Fri Jul 30, 2004 7:58 pm
by feyd
$search_results will only have 1 element. the last fetched $data['wwayname']. I think you want to use this:

Code: Select all

$search_results[] = $data['wwayname'] => array( "type" => $data['type'], "name" => $data['name']);

// instead of

$search_results = array($data['wwayname'] => array( "type" => $data['type'], "name" => $data['name']) );

[SOLVED] - late nite multi dimensional array problem

Posted: Mon Aug 02, 2004 8:04 am
by phait
doh! yes of course. Thanks feyd, its always the little things that you miss isn't it.

Got that part of the script working as intended now.

many thanks for your eyeballs :)