简体   繁体   中英

Point of Sale System

I am developing an web-based Point-of-Sale system for a client. The system is nearly finished but I am experiencing a couple of small issues.

The order items are stored in a array, which is stored in a session variable, which looks like this:

[items] => Array
    (
        [0] => Array
            (
                [id] => 15
            )

        [2] => Array
            (
                [id] => 16
            )

        [3] => Array
            (
                [id] => 15
            )

        [4] => Array
            (
                [id] => 16
            )

    )

This code displays the items:

Note: For each ID in the $items variable I do a lookup in the database to extract addition information about that item.

  foreach($items as $item) {
    $cc++;
    $id = $item['id'];
    $item_db = $db->query("SELECT * FROM inventory WHERE id='$id' LIMIT 1")->fetch_array();
    $subtotal += $item_db['price'];

    $quantity = 1;

    if(!empty($item_db['name'])) {
    ?>
    <tr style="visibility: hidden">
    <td> <a href="?deleteItem=<?=$cc?>" style="color:#000"><i class="fa fa-trash"></i></a> </td>
    <td> <?=$item_db['name']?> </td>
    <td> <?=$quantity?> </td>
    <td> <?=$currency?><?=round($item_db['price'],2)?> </td>
    </tr>
    <?
    }
    }
    ?>

How would I group the items so instead of displaying the same item multiple times with quantity of 1, group the entries and display the total quantity of all entries (which are the same)?

For example, instead of:

Laptop 1
Laptop 1

I want to display:

Laptop 2
  • First create an array to hold the results of counting the ids.
  • Then loop over items, with each array get the value of the subarray id.
    • If the counter array does not have that id as a key, add it with id as key and value 1.
    • If the counter array does have that id as key, increase the value of that id with 1.

Now you have the id of products as keys with the count of products as value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM