Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am really struggling with a script to pull values out of a session array and then call information from a table in my database.

I can get the values to store in the session no problem and it looks like this when I print it:

Array ( [0] => 17 [1] => 18 [2] => 19 [3] => 20 [4] => 20 [5] => 19 [6] => 17 )

I then have the below PHP to pull the values out of the array and pull the records from my database but it appears to be breaking somewhere and unfortunately I am still quite a noob at PHP.

?php
$action = isset($_GET['action']) ? $_GET['action'] : "";

if($action=='removed'){
    echo "<div> was removed from cart.</div>";
}

if(isset($_SESSION['cart'])){
    $ids = "";
    foreach($_SESSION['cart'] as $id){
        $ids = $ids . $id . ",";
    }

    // remove the last comma
    $ids = rtrim($ids, ',');

    $query = "SELECT * FROM events WHERE EventID IN ({$ids})";
    $stmt = $con->prepare( $query );
    $stmt->execute();

    $num = $stmt->rowCount();

    if($num>0){
        echo "<table border='0'>";//start table

            // our table heading
            echo "<tr>";
                echo "<th class='textAlignLeft'>Product Name</th>";
                echo "<th>Price</th>";
                echo "<th>Action</th>";
            echo "</tr>";

            //also compute for total price
            $totalPrice = 0;

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                extract($row);

                $totalPrice += $EventCost;

                //creating new table row per record
                echo "<tr>";
                    echo "<td>{$EventID}</td>";
                    echo "<td class='textAlignRight'>{$EventCost}</td>";
                    echo "<td class='textAlignCenter'>";
                        echo "<a href='removeFromCart.php?id={$EventID} class='customButton'>";
                            echo "<img src='images/remove-from-cart.png' title='Remove from cart' />";
                        echo "</a>";
                    echo "</td>";
                echo "</tr>";
            }

            echo "<tr>";
                echo "<th class='textAlignCenter'>Total Price</th>";
                echo "<th class='textAlignRight'>{$totalPrice}</th>";
                echo "<th></th>";
            echo "</tr>";

        echo "</table>";
        echo "<br /><div><a href='#' class='customButton'>Checkout</a></div>";
    }else{
        echo "<div>No products found in your cart. :(</div>";
    }

}else{
    echo "<div>No products in cart yet.</div>";
}
?>

I am assuming its breaking around the query but again I'm still quite a noob at this.

I am trying to pull out all fields in the table but the Primary key I am trying to link to is called 'EventID'. The ID's in the array do actually match the records I am trying to pull out.

I have copied this code from another tutorial site which is why I'm struggling matching it to my own.

Thank you in advance for any help.

share|improve this question
    
foreach($_SESSION['cart'] as $k=>$id){ $ids = $ids . $id . ","; } –  Feroz Akbar May 17 at 16:42
    
You don't have session_start() anywhere... plus, your whole $ids loop is pointless: $ids = implode(',' $_SESSION['cart'])) will do the same thing much more efficiently. –  Marc B May 17 at 16:44

1 Answer 1

it is simple you just need to use serialize to save theme in database

http://www.php.net/manual/en/function.serialize.php

serialize will make your array as text,you can save it into database then when you want to use it , call unserialize to it change to array again

http://www.php.net/manual/en/function.unserialize.php

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.