0

I have this list of "coupons" each with a unique "productid"

coupons

Now I am trying to convert the list into an array using:

$claimed = array($rowOrder['productid']);

My issue is when I try to use "count" and "array_sum" it outputs individual numbers:

$count_claimed = array(count($claimed));
echo array_sum($count_claimed);

Using the echo I get and output of: "1111111" What should I change to get a sum count of 7? (as displayed with the number of "coupons")

additional info:

The "coupons" are being outputted by this SELECT statement, $rowOrder is calling this.

public function SelectLst_ByUsrCustomerIDInfo($db, $usrcustomerid) {
    $stmt = $db->prepare(
      " SELECT o.orderid, o.productid, o.usrcustomerid, o.amount, o.amountrefunded, o.createddate, o.scheduleddate, o.useddate, o.expirationdate, p.photosrc
        FROM `order` o LEFT JOIN `product` p ON o.productid = p.productid

        WHERE usrcustomerid = :usrcustomerid"
    );

    $stmt->bindValue(':usrcustomerid', $usrcustomerid, PDO::PARAM_INT);
    $stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $rows;
}

This is called like this

$lstInfo = $mcOrder->SelectLst_ByUsrCustomerIDInfo($db, $usrcustomerid);
foreach($lstInfo as $rowOrder) {
    if (isset($rowOrder['productid']) && ($rowOrder['expirationdate'] >  date("Y-m-d H:i:s"))) {
        $claimed = array($rowOrder['productid']);
        $count_claimed = array(count($claimed));
        echo array_sum($count_claimed);
    }
}
5
  • 1
    $claimed = array($rowOrder['productid']); will also be an array of length one. You probably want to push it to an array instead of assign it to an array, then use count() alone (array_sum() will sum all values of an array consisting of many numbers). Commented Mar 19, 2019 at 6:09
  • 1
    You should show some more code - do you fetch this data from a query? If so, can you share that code? Commented Mar 19, 2019 at 6:12
  • updated my question with additional info Commented Mar 19, 2019 at 6:18
  • And how do you call SelectLst_ByUsrCustomerIDInfo? Show that code as well. Commented Mar 19, 2019 at 6:19
  • updated question again Commented Mar 19, 2019 at 6:38

2 Answers 2

2

Doing count($lstInfo) you will get the total number of fetched rows (PDOStatement::fetchAll() returns an array, and you just count the number of elements in it). Then you can loop the results and increment a variable called $claimed if the condition is true.

$lstInfo = $mcOrder->SelectLst_ByUsrCustomerIDInfo($db, $usrcustomerid);
$total = count($lstInfo);
$claimed = 0;
foreach($lstInfo as $rowOrder) {
    if (isset($rowOrder['productid']) && ($rowOrder['expirationdate'] >  date("Y-m-d H:i:s"))) {
         $claimed += 1;
    }
}
echo "Claimed $claimed of $total.";

Even better, you can do it in one query using a COUNT() and an added WHERE condition. This means you won't get the total, but that didn't seem to be the question to begin with either.

$stmt = $db->prepare("SELECT COUNT(productid) as cnt
                      FROM `order` o 
                      LEFT JOIN `product` p 
                           ON o.productid = p.productid
                      WHERE usrcustomerid = :usrcustomerid
                       AND expirationdate > NOW()
                      GROUP BY usrcustomerid");
$stmt->execute([":usrcustomerid" => $usrcustomerid]);
$result = $stmt->fetch();
echo "Claimed ".$result['cnt'];
Sign up to request clarification or add additional context in comments.

1 Comment

i see, i need to count inside the for loop, thanks for the help.
0

Try This,

$claimed = array();
foreach($products as $rowOrder){
    array_push($claimed,$rowOrder['productid']);
}
echo count($claimed);
echo array_sum($claimed);die;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.