Functions and arrays

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
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Functions and arrays

Post by Mr Tech »

I have a function that displays the orders that were made in a month... It shows 6 months, one after the other. The function selects the orders and order items and adds the price together to displays it on the page like this:

Code: Select all

Month	           Sales	  Orders	  Average
November, 2006	  $100.00	2	       $50.00
October, 2006	   $150.00	3	       $50.00
September, 2006	 $0.00	  1	       $0.00
However, want to be able to display the total price of all the months added together... (100 + 150 + 0)

I've got it set-up in my function to add the prices together, but how do I get it to pull two values from the one function? Using an Array of course however my problem is that at the end of the function, I call the function again... When I call ti again, in the $month field I add $month - 1 so that I gets orders from the previous month.

How do I get the function to read the array values added by the function within itself add that to the overall array?

I don't know if I'm making sense.... Let me know if I'm not.

Here is a snippet of my function:

Code: Select all

function statistics($month, $year, $price, $count) {
$price = $price + $total_price; // This adds the total price of the orders for this month to the overall price ($price)

$orders .= "<tr>\n<td>".date('F, Y', $this_month)."</td><td>$".number_format($total_price, 2)."</td><td>".$orders_count."</td><td>$".number_format($sales_average, 2)."</td>\n</tr>\n"; // This displays the details for the order which I displayed above

$orders .= statistics($month - 1, $year, $price, $count +1); // This calls the function again however it gets results for the previous month

return $orders;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

return an array?
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

For example:

Code: Select all

<?php
$months = statistics(11, 2006, 0, 1);
?>
<table border="0">
<tr>
<td>Month</td><td>Sales</td><td>Orders</td>
</tr>
<?php
foreach ($months as $id => $month) {

echo $month[months];
$price = $price + $months[price];

}
?>
</table>

Total Earnings: <?php echo $price; ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your example makes less sense to me than your first post.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

LOL... never mind... It's all in my head I guess. It's all up to me to figure out I guess. I wont bother explaining more just to make it more confusing.

Thanks anyway mate :wink:
Post Reply