0

Not sure how to title this properly but here's the issue I am running into currently. I built a cart and checkout system and it loads all the data into a database when it finalized the order. To save some space, I stored just the item IDs originally but then I ran into the issue of if I deleted the item from the database (because it was discontinued or whatever) then it wouldn't return the info I needed. And if they ordered more then 1 item the database record would be wrong. So I stored the data like so:

Itemid:Quantity:Price:name, itemid2:quantity2:price2:name2
OR
1:3:20.00:Flower Hat, 2:1:17.75:diamonds

The issue I have right now that I need help with is this. I need to seperate the four values into variables like $price, $item, $id, $ammount so I can display them on the order history page and I need to loop through all items on the array so I can print a row for each item with all four fields respective to that item.

I use strpos already to get the shipping info from the same database field which is formatted as METHOD:Price but since I have 3 :'s on my string I'm not sure how to go through each one. Thanks.

1

4 Answers 4

1

Here's a function

function parseItems($dbjunk){
    $cart = array();
    $items = explode(",",$dbjunk);
    foreach($items as $i){
        $chunks = explode(":", $i);
        $cart[] = array(
            "ItemID" => $chunks[0] ,
            "Quantity" => $chunks[1] ,
            "Price" => $chunks[2] ,
            "name" => $chunks[3]
        );
    }
    return $cart;
}

Example usage:

$dbjunk = "Itemid:Quantity:Price:name, itemid2:quantity2:price2:name2";
$parsed = parseItems($dbjunk);
print_r($parsed);

See: https://3v4l.org/rBkXF

If you need variables instead of an array you can use list(), like this..

$dbjunk = "Itemid:Quantity:Price:name, itemid2:quantity2:price2:name2";
$parsed = parseItems($dbjunk);
foreach($parsed as $p){
    list($itemID, $Quantity, $Price, $name) = array_values($p);
    var_dump($itemID, $Quantity, $Price, $name);
}

see: https://3v4l.org/l4vsn

Sign up to request clarification or add additional context in comments.

1 Comment

I used this function and it solved my problem right away. Thanks for the responses guys.
0

You should not physically delete items from your database. Instead, put a new column named 'is_active' or something like that to indicate whether the product is active/non-deleted. Answering your question, here is my suggestion:

$orderString = '1:3:20.00:Flower Hat, 2:1:17.75:diamonds';
$items = array();
foreach(explode(', ', $orderString) as $itemString) {
    $itemData = explode(':', $itemString);
    $items[] = array(
        'id' => $itemData[0],
        'amount' => $itemData[1],
        'value' => $itemData[2],
        'description' => $itemData[3]
    );
}

with this code, you will obtain an array with the data of all the items in the string, no matter how much items are in the string

Comments

0

try something like

$data = 1:3:20.00:Flower Hat, 2:1:17.75:diamonds
list($price, $item, $uid, $id, $ammount) = explode(":", $data);
echo $user; 
 echo $item; 

Comments

0

Read about First Normal Form. Basically, you want to store one value in one field. So, instead of this:

shipping = "method:price"

You want something like this:

shipping_method = "method"
shipping_price = "price"

Don't concern yourself with space -- it's essentially free nowadays.

Regarding your deleted items dilemma, your initial implementation was the way to go:

I stored just the item IDs originally

In addition to reverting to this technique, I would recommend two things:

  • Add a boolean field to your item table to represent if the item is currently available or not. This gives you the additional feature of being able to toggle items on/off without having to delete/insert records and change ids.
  • Before deleting an item, check to see if it's ever been ordered. If not, it's ok to delete. If so, instead just deactivate it.

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.