Everton Thomazi
Everton Thomazi

Reputation: 61

Count and sum values from array in PHP

I have a page that show my orders in day, but now i want to count and sum the values from 7 / 15 and 30 days ago (if possible in list), or just count/sum in this step: <strong>Soon</strong>

$pedido->valor_pedido is my value from order
$pedido->id its id from order, i use to count.

My code:

<div class="row">

<?php

$totalPedido = 0;
$contagPedido = 0;

foreach ($pedidos as $pedido) {
    $totalPedido += $pedido->valor_pedido; // sum day orders
    $contagPedido += count($pedido->id); //count day orders
}
?>
    <div class="span12">
    <!-- Widgets -->
        <ul class="widgets">
    <!-- Basic widget item -->
            <li class="span3">
                <span class="widget-label"><span class="awe-star"></span> This Week</span>
                <strong>Soon</strong>
            </li>
    <!-- /Basic widget item -->
    <!-- Clickable widget item -->
            <li class="span3">
                    <span class="widget-label"><span class="awe-star"></span> Totay</span>
                    <strong>R$ <?= ConverteReal($totalPedido) ?></strong> 
            </li>
    <!-- /Clickable widget item -->
    <!-- Widgets with graphs -->
            <li class="span3">
                <span class="widget-label"><span class="awe-star"></span> Orders this week</span>
                <strong>Soon</strong>
            </li>
            <li class="span3">
                <span class="widget-label"><span class="awe-star"></span> Orders today</span>
                <strong><?= $contagPedido ?></strong>   
            </li>
    <!-- /Widgets with graphs -->
        </ul>
    <!-- /Widgets -->
    </div>
</div>

Upvotes: 1

Views: 94

Answers (1)

devpro
devpro

Reputation: 16117

According to PHP Manual, count() will use to get the count of all elements in an array

If $pedido->id is a unique ID, you can just get the count of $pedidos array like

<?
$contagPedido = count($pedidos); // total no of orders 
foreach ($pedidos as $pedido) {
    $totalPedido += $pedido->valor_pedido; // sum day orders    
}
?>

Upvotes: 2

Related Questions