samedi 9 juin 2018

Fetching data from 3 related tables with INNER JOIN fails

I want to get sum of the prices and items columns which are related to 3 tables.I am trying with INNER JOIN in the following script. It only displays 1 invoice total. And else statement is not displaying 0 When data is 0.

function TotalRecord($total){
global $db;
$date = date("Y-m-d");
$query = $db->prepare("SELECT sum(a.prices), sum(a.item) FROM items a INNER JOIN invoice_items b ON a.item_id = b.item_id INNER JOIN invoices c ON b.invoiceId = c.invoiceId  WHERE c.invoice_date = :date ");
$query->bindParam(':date', $date);
$query->execute();
for($i=0; 
    $rows = $query->fetch(); 
    $i++){
      $totalPrice = $rows['sum(a.prices)'];
      $totalitems = $rows['sum(a.item)'];
      $array = array('total' => $totalPrice , 'items' => $totalitems );

            if(count($array) > 0)
            {
                echo $array[$total];
            }
            else
            {
                echo "0";
            }

     }
}

Aucun commentaire:

Enregistrer un commentaire