Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    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;



    Last edited by Redcoder; 12-27-2012 at 08:46 PM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #4
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •