Jump to content

Archived

This topic is now archived and is closed to further replies.

ImJustBrndn

Converting An Array

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

Share this post


Link to post
Share on other sites
You'd have to run an SQL query to fetch the name while looping through your array.

Share this post


Link to post
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]


Share this post


Link to post
Share on other sites

×

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.