Jump to content

arithmatic in php


bhavin_85

Recommended Posts

hey guys

 

ive created the following code which cheks through all of a certain customers invoices and prints the item id for all the items they have brought (they are generic id's so things like ring, watch)

 

I need to count the output for each row then depending on what is highest show a certain image....how do i get it to count the values?

 

<?
session_start();
if ( empty($_SESSION['username'])){
header("location:default.php");
exit;
}

$id=$_SESSION['cust_id'];

include ('config.php');

$sql = "SELECT invoice_id FROM invoices WHERE cust_id='$id'";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
    $invoice_id = $row['invoice_id'];
//	echo "Invoice ID: ".$row['invoice_id'];

$sql1 = "SELECT item_item_id FROM invoice_items WHERE invoices_invoice_id=$invoice_id";
//echo $sql1;
$query1 = mysql_query($sql1) or die(mysql_error());
while ($row1 = mysql_fetch_assoc($query1)) { 
    echo "Item ID: ".$row1['item_item_id'];
}
}

?>

 

heres what its printing at the moment:

Item ID: 1Item ID: 2Item ID: 3Item ID: 1

so i would need to add all the 1's, 2's and 3's then depending on which is highest (in this case 1) shown image for it

 

any ideas?

Link to comment
https://forums.phpfreaks.com/topic/41850-arithmatic-in-php/
Share on other sites

If there are only the three items you could use a switch statement with counters inside the loop.

 

ID1Counter = 0;
ID2Counter = 0;
ID3Counter = 0;

while ($row1 = mysql_fetch_assoc($query1)) {
    switch ($row1['item_item_id']){
         case '1' :
             ID1Counter += 1;
         case '2' :
             ID2Counter += 1;
         case '3' :
             ID3Counter += 1;
    }

if ( $ID1Counter > $ID2Counter && $ID1Counter > $ID3Counter)
   //Display ID1 Image
elseif ( $ID2Counter > $ID1Counter && $ID2Counter > $ID3Counter)
   //Display ID2 Image
elseif ( $ID3Counter > $ID1Counter && $ID3Counter > $ID2Counter)
   //Display ID3 Image
else
   // Two or all Three are equal, Don't know what you want to do with that.
}

 

There's probably a much easier way to do this, but I don't know what it is.

Link to comment
https://forums.phpfreaks.com/topic/41850-arithmatic-in-php/#findComment-202970
Share on other sites

ive been working on it and got this far:

 

$sql = "SELECT invoice_id FROM invoices WHERE cust_id='$id'";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
    $invoice_id = $row['invoice_id'];
//	echo "Invoice ID: ".$row['invoice_id'];

$sql1 = "SELECT item_item_id FROM invoice_items WHERE invoices_invoice_id=$invoice_id ORDER BY item_item_id  DESC";

//echo $sql1;
$query1 = mysql_query($sql1) or die(mysql_error());
while ($row1 = mysql_fetch_assoc($query1)) { 
    echo "Item ID: ".$row1['item_item_id'];

}

}
$row2 = mysql_fetch_array($query1);
$num_arrays = count($row2);
echo "Total items: $num_arrays";  
?>

 

its printing : Item ID: 3Item ID: 2Item ID: 1Item ID: 1Total items: 1

 

where the total items should be 4 not 1  ???

Link to comment
https://forums.phpfreaks.com/topic/41850-arithmatic-in-php/#findComment-202995
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.