Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-27-2012, 06:53 PM   PM User | #1
nepoverljiv
New Coder

 
Join Date: Mar 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
nepoverljiv is an unknown quantity at this point
Question Php Array And Listing Description From Table

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:

PHP Code:
$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:

PHP Code:
$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?
nepoverljiv is offline   Reply With Quote
Old 12-27-2012, 07:37 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Use foreach:
PHP Code:
foreach($array as $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];   

//Do calculation to get Total Price for this Item

//E.g 
//Get price from DB in $price using Product ID like $product_array = mysqli_fetch_array(mysqli_query($db, "SELECT * FROM products WHERE product_id = $product_id")); $price = $product_array['price'];
$total_price[] = $price $product_quantity;



__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 12-27-2012 at 07:46 PM..
Redcoder is offline   Reply With Quote
Old 12-28-2012, 02:28 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I'm curious how you managed to get it into the x-y format in the first place. Wouldn't it be easier to simply store it with a key indicating it's id, and a value as the quantity in an array? If you do it that way, you can use an implode on your query to fetch what you need using an IN clause (in conjunction with a foreach).
PHP Code:
$sids implode(', 'array_keys($yourArray));
$aRecords = array();
$sQry "SELECT id, description, price FROM yourtable WHERE id IN ($sids)";
if (
$qry mysql_query($sQry)) // not sure if you are using the old mysql package or not
{
    while (
$row mysql_fetch_assoc($qry))
    {
        
$aRecords[$row['id']] = $row;
    }
}

foreach (
$yourArray AS $id => $qty)
{
    
printf('You have ordered %d of %s with a total cost of %0.2f' PHP_EOL$qty$aRecords[$id]['description'], ($qty $aRecords[$id]['price']));

For example.
Fou-Lu is offline   Reply With Quote
Old 12-28-2012, 03:48 PM   PM User | #4
nepoverljiv
New Coder

 
Join Date: Mar 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
nepoverljiv is an unknown quantity at this point
both answer don't work.

I try like this:

PHP Code:
$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?
nepoverljiv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:37 AM.


Advertisement
Log in to turn off these ads.