Jump to content

Php Array And Listing Description From Table


nepoverljiv

Recommended Posts

Hi,

 

I make some code and almost finish my shopping cart on website. Just need finally touch :).

 

Ok, I have problem on last page, problem is like this. When someone click to order what he putted in shopping cart, on one page I wanna list everything what he ordered an send on e-mail. Trick is I don't have only id in array, I have almost quantity. For example, if someone order article with id=1 and quantity 3, and article with id=2 and quantity 4, and article with id=3 and quantity 2. On my page I get all that in array like this:

 

$array=$_POST['id'];

 

When I print that I get this:

 

1-3,2-4,3-2

 

How to I get from my table "description", list of every article in this example and immediately multiply them price with quantity?

 

I try something with:

 

$id_quantity_pair = explode("-", $array); // Uses Hyphen(-) as delimiter to separate product ID from its quantity
   $product_id = $id_quantity_pair[0]; // Get the product ID
   $product_quantity = $id_quantity_pair[1];

 

But I dont know how to put that in list, I know only to working with array with one value.

 

Anyone to help me?

Link to comment
Share on other sites

I try like this:

 

$array=$_POST['pid'];
$id_str_array = explode(",", $array); // Uses Comma(,) as delimiter(break point)
$fullAmount = 0;
foreach ($id_str_array as $key => $value) {

   $id_quantity_pair = explode("-", $value); // Uses Hyphen(-) as delimiter to separate product ID from its quantity
   $product_id = $id_quantity_pair[0]; // Get the product ID
   $product_quantity = $id_quantity_pair[1]; // Get the quantity
   $sql = mysql_query("SELECT * FROM mytable WHERE id='$product_id' LIMIT 1");
   while($row = mysql_fetch_array($sql)){
    $product_price = $row["price"];
    echo $product_price; //show all price
   }
   $product_price = $product_price * $product_quantity;
   $fullAmount = $fullAmount + $product_price;
} 

 

Where I wrote show all price, I printing all price, but if I add also id to print, he will print me only id for every article or only price for each article depends what I first echo. Can anyone help me with this, because I am close to finish?

Link to comment
Share on other sites

I think you've made this quite a lot more complicated than what you needed, thus you've created a lot of extra problems for yourself.

Biggest complication is the format you've decided to return the item IDs and quantity in, which means you'll have to add a lot of code just to parse the logic of it. It would have been much better to simply return that data in an array, which would allow you to either directly access the Items or loop through them. Without all of that extra code.

 

Also, you should never run a DB query inside a loop: Nothing kills performance quite like that, and it's quite unnecessary as well. Especially in this case, where you could just fetch all the items with one query, using the IN() operator to match all IDs at once.

 

I would recommend you to read through this article on creating a shopping cart, and then rethinking your approach to this. Keep it simple, and it'll be simple to work with. ;)

Though, to be able to keep it simple, you have to understand the problem and solution fully, before starting to write the code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.