Jump to content

Converting An Array


ImJustBrndn

Recommended Posts

I have a shopping cart set up to push the session (where all the products are stored) into a print and it displays the results like this:

Array
(
  [1] => Array
      (
          [ 0] => 1
          [1] => 1
      )

  [3] => Array
      (
          [ 0] => 3
          [1] => 2
      )

  [5] => Array
      (
          [ 0] => 5
          [1] => 1
      )

  [6] => Array
      (
          [ 0] => 6
          [1] => 1
      )

)

The inital block in front of the Array is the product id and the last number after the => is the quantity of that product. Is there anything I can route this too or better yet some defining factor I can put in the page before I have it submit them in a email form to display the product name instead of id so for example it says oh hey thats a 6 product id that Trash Cans and then do the quantity the same way. Thanks for the help.

B
Link to comment
Share on other sites

Assume your session array is called 'cart'
[code]<?php
$product_ids = array_keys($_SESSION['cart']);
$product_
$qry "SELECT `prod_id`,`prod_name` FROM `products` WHERE `prod_id` IN (" . implode(',',$product_ids) . ")";
$qry = mysql_query($qry);
$db_prod = array();
while ($row = mysql_fetch_assoc($qry))
{

  $db_prod['name'][$row['prod_id']] = $row['prod_name'];

}
...

?>

[/code]

then as you loop through your session var you simply replace the id with the corresping value in teh $db_prod array.

eg 3 would be $db_prod['name'][3]


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.