0

I am having a problem to convert my $quantity_total which is as example (113) from 3 different products. I want it to be in a table like below.

I have been trying to use chunk_split and explode but if i was able to succeed in that. I wouldn't be able to make it dynamic.

<table>
  <tr>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>1</td>
  </tr>
  <tr>
    <td>1</td>
  </tr>
  <tr>
    <td>3</td>
  </tr>
</table>

$total=0;
$item_count=0;
$arr = array();
$quantity_all = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    extract($row);
        $arr[] = $row;

    $_SESSION['cart-checkout'] = $arr;

    $quantity=$_SESSION['cart'][$id]['quantity'];

    $quantity_all .=$quantity;

    $sub_total=$price*$quantity;


    echo "<div class='cart-row'>";
        echo "<div class='col-md-8'>";

            echo "<div class='product-name m-b-10px'><h4>{$name}</h4></div>";
            echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";

        echo "</div>";

        echo "<div class='col-md-4'>";
            echo "<h4>&#36;" . number_format($price, 2, '.', ',') . "</h4>";
        echo "</div>";
    echo "</div>";


    $item_count += $quantity;
    $total+=$sub_total;

    $_SESSION['total'] =  $total;
    $_SESSION['item-count'] =  $item_count;

}
$_SESSION['quantity-all'] = $quantity_all;

Is this possible? And i need it to be dynamic. So if it were 10 different quantities. It would make 10 table rows.

I hope someone can help me, would really appreciate it a lot! It's the last thing to finish my e-commerce webshop.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Please put more samples for $qauntity_total, first we need to calculate all after that we can make table. – Badrinath Jun 24 '19 at 17:52
  • If you have 3 different products would they be in an array or what? https://www.php.net/manual/en/control-structures.foreach.php – AbraCadaver Jun 24 '19 at 17:54
  • This isn't a good approach for a few reasons but the main is, what does `11` mean? 1 row with a value of _11_ or 2 rows each with a value of _1_? You'll only be able to handle values 0-9. Is this what you want? – waterloomatt Jun 24 '19 at 17:59
  • @Badrinath I updated my code. –  Jun 24 '19 at 18:19
  • @AbraCadaver No they are not in an array, see the updated code above. –  Jun 24 '19 at 18:20
  • @waterloomatt I take the values of all the products and it comes as a string like (113) So product number 1 has 1 item, number 2 has 1 item but number 3 has 3 items(quantity). Check my updated code above. –  Jun 24 '19 at 18:26
  • Don't concatenate/split the quantities. Instead, on the page where you want to display your cart perform a query to retrieve the cart quantities which will return a list of quantities. Take that list and loop over it, outputting the quantities. – waterloomatt Jun 24 '19 at 18:34
  • @waterloomatt Yes i thought about that, i will see what i am going to do, thanks for your time –  Jun 24 '19 at 18:35

2 Answers2

0

As mentioned in my comment, I don't think this is good solution... but the function you're looking for is str_split. https://stackoverflow.com/a/9814389/296555

http://sandbox.onlinephpfunctions.com/code/0bbee53cafafc0d5e8954e07d0abc2c86c6c89a8

<?php
$rows = '156165165489465131';

echo '<table>';
echo '<tr><th>Quantity</th></tr>';

foreach (str_split($rows) as $row) {

    echo "<tr><td>$row</td></tr>";
}

echo '</table>';
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
0

I dont know if this Is what you want, but you can try something like:

 <table>
   <tr>
      <td>Quantity</td>
   </tr>
    <?php

    $characters = str_split( (string) $quantity_total);

    foreach($characters as $char){
          echo "
              <tr>
                  <td> $char </td>
             </tr>
           ";
    }

    ?>
   </table>
  • Works thank you, the comment before you is accepted because it was the fastest but yours works also perfect. Thank you for you time! –  Jun 24 '19 at 18:34