Home > Blockchain >  how to count the number of item in a loop
how to count the number of item in a loop

Time:08-31

I have a command variable with items

and I wish I could count how many items I have for item 77777;

also for 88888;

also for 99999

and also count all those who do not have these numbers

I have a code that doesn't seem to work

$data = $commande;
$decode = json_decode($data);
$mailData1 = "";
$mailData2 = "";
$mailData3 = "";
$mailData4 = "";
foreach ($decode  as $curr_element) {
    $fieldName = $curr_element->nom;
    $fieldQty = $curr_element->quantite;
    $fieldPrice = $curr_element->prix;
    $fieldimg = $curr_element->url;
    if ( $fieldQty != 0 ) {
        if ($fieldPrice != 0) {
            if ($fieldName == 77777) {
                $input1 =   '<div style ="border:solid;"> 
           <div style ="float: left;width: 50%;"> voici le nom</div >
           <div style =""> ' . $fieldName . '</div>
            <div style ="float: left;width: 50%;"> qanditée</div >
           <div style =""> ' . $fieldQty . '</div >
                 </div><br><br><br>';
                $mailData1 .= $input1;
            }
            else if ($fieldName == 88888) {
                $input2 =   '<div style ="border:solid;"> 
           <div style ="float: left;width: 50%;"> voici le nom</div >
           <div style =""> ' . $fieldName . '</div>
            <div style ="float: left;width: 50%;"> qanditée</div >
           <div style =""> ' . $fieldQty . '</div >
                 </div><br><br><br>';
                $mailData2 .= $input2;
            }
            else if ($fieldName == 99999) {
                $input3 =   '<div style ="border:solid;"> 
            <div style ="float: left;width: 50%;"> qanditée</div >
           <div style =""> ' . $fieldQty . '</div >
           <div style ="float: left;width: 50%;"> voici le nom</div >
           <div style =""> ' . $fieldName . '</div>
                 </div><br><br><br>';
                $mailData3 .= $input3;
            }
            else if ($fieldName == ) {
                $input4 =   '<div style ="border:solid;"> 
            <div style ="float: left;width: 50%;"> qanditée</div >
           <div style =""> ' . $fieldQty . '</div >
           <div style ="float: left;width: 50%;"> voici le nom</div >
           <div style =""> ' . $fieldName . '</div>
                 </div><br><br><br>';
                $mailData4 .= $input4;
            }
        } 
    }
    var_dump ($mailData1);
    var_dump ($mailData2);
    var_dump ($mailData3);
    var_dump ($mailData4);

CodePudding user response:

Use an associative array whose keys are the item numbers.

Also, the last else if should just be else to handle all other values.

$data = $commande;
$decode = json_decode($data);
$mailData1 = "";
$mailData2 = "";
$mailData3 = "";
$mailData4 = "";
$counts = [];
foreach ($decode  as $curr_element) {
    $fieldName = $curr_element->nom;
    $fieldQty = $curr_element->quantite;
    $fieldPrice = $curr_element->prix;
    $fieldimg = $curr_element->url;
    if ( $fieldQty != 0 ) {
        if ($fieldPrice != 0) {
            if ($fieldName == 77777) {
                $input1 =   '<div style ="border:solid;"> 
           <div style ="float: left;width: 50%;"> voici le nom</div >
           <div style =""> ' . $fieldName . '</div>
            <div style ="float: left;width: 50%;"> qanditée</div >
           <div style =""> ' . $fieldQty . '</div >
                 </div><br><br><br>';
                $mailData1 .= $input1;
                @$counts[$fieldName]  ;
            }
            elseif ($fieldName == 88888) {
                $input2 =   '<div style ="border:solid;"> 
           <div style ="float: left;width: 50%;"> voici le nom</div >
           <div style =""> ' . $fieldName . '</div>
            <div style ="float: left;width: 50%;"> qanditée</div >
           <div style =""> ' . $fieldQty . '</div >
                 </div><br><br><br>';
                $mailData2 .= $input2;
                @$counts[$fieldName]  ;
            }
            elseif ($fieldName == 99999) {
                $input3 =   '<div style ="border:solid;"> 
            <div style ="float: left;width: 50%;"> qanditée</div >
           <div style =""> ' . $fieldQty . '</div >
           <div style ="float: left;width: 50%;"> voici le nom</div >
           <div style =""> ' . $fieldName . '</div>
                 </div><br><br><br>';
                $mailData3 .= $input3;
                @$counts['other']  ;
            }
        } 
    }
}
var_dump ($mailData1);
var_dump ($mailData2);
var_dump ($mailData3);
var_dump ($mailData4);
var_dump ($counts);
  •  Tags:  
  • php
  • Related