Jump to content


Photo

Converting An Array


  • Please log in to reply
2 replies to this topic

#1 ImJustBrndn

ImJustBrndn
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 21 August 2006 - 05:28 AM

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

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 21 August 2006 - 02:34 PM

You'd have to run an SQL query to fetch the name while looping through your array.

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 04:19 PM

Assume your session array is called 'cart'
<?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'];

}
 ...

?>


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]



follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users