Jump to content

Adding in an array


bateman

Recommended Posts

I've got a site running, and I would like to add up the totals from each location to one number, now I know it can be done, I just don't know how it'll work with my current code (see below)

 

<table summary="" border="1" width="1100">
<tr>
	<td>Date</td>
	<!-- <td>Employee</td> -->
	<td>Store</td>
	<td>Customers</td>
	<td>Quotes</td>
	<td>Phone Calls</td>
	<td>Services In</td>
	<td>Services Out</td>
	<td>Invoices</td>
	<td>Conversion</td>
	<td>End of Day</td>
	<td>Emails</td>
	<td colspan=3>Notes</td>
</tr>
<?
$x = 1; $y=1; $lastdate=0;
while ($list = mysql_fetch_assoc($query)) {
$dateadd = $list['dateadd']; $dateb = strtotime($list['dateadd']); $date = date("M j Y", $dateb);$date2 = date("Y-m-d", $dateb); $firstname = $list['firstname'];$lastname = $list['lastname'];$store = $list['store'];$customers = $list['cust'];$sales = $list['sales'];$invoices = $list['invoices'];$sin = $list['servin'];$sout = $list['servout'];$eod = $list['eod']; $phone = $list['phone']; $notes = $list['notes']; $emails = $list['emails'];
if ($store == "1") {$storename = "Broadmead";} elseif  ($store == "2") {$storename = "Duncan";} elseif  ($store == "3") {$storename = "Surrey";} elseif  ($store == "4") {$storename = "Sooke";} elseif  ($store == "5") {$storename = "Nanaimo";}	elseif  ($store == "6") {$storename = "Shelbourne";}



?>	  <?php if ($lastdate == $date) { ?>

	<tr>
	<td width="125"><?=$date;?></td>
	<!-- <td width="125"><?=$firstname;?> <?=$lastname;?></td> -->
	<td width="100"><?=$storename;?></td>
	<td width="71"><?=$customers;?></td>
	<td width="71"><?=$sales;?></td>
	<td width="80"><?=$phone;?></td>
	<td width="80"><?=$sin;?></td>
	<td width="90"><?=$sout;?></td>
	<td width="71"><?=$invoices;?></td>
    <td width="71"><? $convbase = $sout+$sales; if (!$invoices==0 && !$customers==0) {$conv = $invoices / $customers * 100;} else {$conv=0;} echo money_format('%(#1n', $conv) . "\n"; ?>%</td>
	<td width="71">$<?echo money_format('%(#1n', $eod) . ""; ?><td>
	<td width="71"><?=$emails;?> </td>
	<td width="150"><?if (!$notes==NULL){echo $notes;} else {echo " ";}?></td>
	</tr>
	<?php } else { ?>
	 <tr>
	 <td colspan="9">Daily Total</td>
	 <td><?echo money_format('%(#1n', $total) . ""; ?> </td>
	 <td colspan="4"> </td>
	 </tr>
	</table> 
<hr width="1100" align="left" color="0000ff">

<table summary="" border="1" width="1100">
	<tr>
	<td width="125"><?=$date;?></td>
	<!-- <td width="125"><?=$firstname;?> <?=$lastname;?></td> -->
	<td width="100"><?=$storename;?></td>
	<td width="71"><?=$customers;?></td>
	<td width="71"><?=$sales;?></td>
	<td width="80"><?=$phone;?></td>
	<td width="80"><?=$sin;?></td>
	<td width="90"><?=$sout;?></td>
	<td width="71"><?=$invoices;?></td>
    <td width="71"><? $convbase = $sout+$sales; if (!$invoices==0 && !$customers==0) {$conv = $invoices / $customers * 100;} else {$conv=0;} echo money_format('%(#1n', $conv) . "\n"; ?>%</td>
	<td width="71">$<?echo money_format('%(#1n', $eod) . ""; ?><td>
	<td width="71"><?=$emails;?> </td>
	<td width="150"><?if (!$notes==NULL){echo $notes;} else {echo " ";}?></td>
	</tr>
	<?php } $lastdate = $date;
} // end while    

?>
</table>

 

Any help would be appreciated.

Link to comment
Share on other sites

Without trying to decipher the code (what's a "location"? total of what?), the total per location could be:

$totals = array();

your loop {
    if (!isset($totals[the location])) $totals[the location] = 0;
    $totals[the location] += the amount;
}

while the total of locations could be (something which should be fairly obvious):

$total = 0;

your loop {
    $total += the amount;
}

Link to comment
Share on other sites

Requinix has it....

 

Pretty much

 

$arrayname[] to add a value to an array, and += to add a value to the current array + the value.

 

I strongly suggest making your code easier to read, obviously it's personal preference but that is just impossible. When you find yourself with 5 elseifs you should really setup a switch statement, would make things a lot easier.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.