Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building an multi-level associative array from flat file

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I am working on a small project and for the first time, I find that the use of multi-level arrays is something I need to use.

My objective is to read through a flat file, render a printer ready page, show sub-totals within the pages and at the end, display a summary page.

The summary page needs to show the totals for two elements. Each element has three columns to total (by element I mean unique key or id).

The flat file looks like this:
shipper,cartons,kilos,volume,location,...

The summary page needs to look like this

SHIPPERS CTN KGS VOL
------------ ---- ---- -----
SHIPPER A 1 10 2.2
SHIPPER B 2 8 1.0

LOCATIONS CTN KGS VOL
------------ ---- ---- -----
LOC A 3 18 3.2

This is the PHP code I have to accumulate the summary data
Code:
$summaryA[$row[0]][0]=$row[0];
$summaryA[$row[0]][1]=+$row[2];
$summaryA[$row[0]][2]=+$row[3];
$summaryA[$row[0]][3]=+$row[4];

$summaryA[$row[12]][0]=$row[0];
$summaryB[$row[12]][1]=+$row[2];
$summaryB[$row[12]][2]=+$row[3];
$summaryB[$row[12]][3]=+$row[4];

and this is the code to display the summary data
Code:
for($x=0;$x<sizeof($summaryA);$x++) {
	$shpsummary .= '
	<tr>
		<td>'.$summaryA[$x][0].'</td>
		<td>'.$summaryA[$x][1].'</td>
		<td>'.$summaryA[$x][2].'</td>
		<td>'.$summaryA[$x][3].'</td>
	</tr>';
}
for($x=0;$x<sizeof($summaryB);$x++) {
	$locsummary .= '
	<tr>
		<td>'.$summaryB[$x][0].'</td>
		<td>'.$summaryB[$x][1].'</td>
		<td>'.$summaryB[$x][2].'</td>
		<td>'.$summaryB[$x][3].'</td>
	</tr>';
}

Arrays are blank ... Count you please point out my problem?

Thank you all in advance!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hello,

I can't tell exactly what's wrong with what you want to do but I think that you should use a foreach loop instead.

Also, for debugging purpose, you should print_r your array just before your script in order to make sure your array is constructed the way you want.
 
i do not have any dummy data to test with, and i am assuming that you are using php5+

if so try this code.

Code:
<?php
$flatFile = '/path/to/file.txt';
/**
 * The flat file looks like this:
shipper,cartons,kilos,volume,location,...

The summary page needs to look like this

SHIPPERS     CTN   KGS    VOL
------------ ---- ----  -----
SHIPPER A       1   10    2.2
SHIPPER B       2    8    1.0

LOCATIONS     CTN   KGS    VOL
------------ ---- ----  -----
LOC A           3   18    3.2
 */

//now open the file and iterate
$fh = fopen($flatFile, 'rbt');

//create the blank arrays
$shippers = $locations = array();

while (false != ($row = fgetcsv($fh, 0, ',', '"'))){
	//ignore blank lines
	if (count ($row) === 1) continue;
	
	//zero the data
	$shipper = $location = '';
	$kilos = $vol = $cartons = 0;
	
	//extract the info
	list ($shipper, $cartons, $kilos, $vol, $location) = $row;
	
	//build the arrays
	
	$shippers[$shipper]['cartons'] =+ $cartons;
	$shippers[$shipper]['kilos'] =+ $kilos;
	$shippers[$shipper]['vol'] =+ $vol;
	
	$locations[$location]['cartons'] =+ $cartons;
	$locations[$location]['kilos'] =+ $kilos;
	$locations[$location]['vol'] =+ $vol;
}

//output the summaries
$class = 'even';
echo <<<HTML
<div>
Summary A<br/>
<table>
	<thead>
	<tr>
		<th>Shippers</th>
		<th>Cartons</th>
		<th>Kilos</th>
		<th>Volume</th>
	</tr>
	</thead>
	<tfoot>
	<tr>
		<th>Shippers</th>
		<th>Cartons</th>
		<th>Kilos</th>
		<th>Volume</th>
	</tr>
	</tfoot>
	<tbody>
	
HTML;
foreach ($shippers as $sName => $sData){
	$class = ($class === 'even') ? 'odd' : 'even';
	echo <<<HTML
	
	<tr class="$class">
		<td>$sName</td>
		<td>{$sData['cartons']}</td>
		<td>{$sData['kilos']}</td>
		<td>{$sData['vol']}</td>
	</tr>
	
HTML;
}
echo <<<HTML
	
	</tbody>
</table>
</div>
HTML;


$class = 'even';
echo <<<HTML
<div>
Summary B<br/>
<table>
	<thead>
	<tr>
		<th>Locations</th>
		<th>Cartons</th>
		<th>Kilos</th>
		<th>Volume</th>
	</tr>
	</thead>
	<tfoot>
	<tr>
		<th>Locations</th>
		<th>Cartons</th>
		<th>Kilos</th>
		<th>Volume</th>
	</tr>
	</tfoot>
	<tbody>
	
HTML;
foreach ($locations as $lName => $lData){
	$class = ($class === 'even') ? 'odd' : 'even';
	echo <<<HTML
	
	<tr class="$class">
		<td>$lName</td>
		<td>{$lData['cartons']}</td>
		<td>{$lData['kilos']}</td>
		<td>{$lData['vol']}</td>
	</tr>
	
HTML;
}
echo <<<HTML
	
	</tbody>
</table>
</div>
HTML;


?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top