Page 1 of 1

Help: how to make session array be printed?

Posted: Sat Apr 12, 2008 9:37 pm
by yetchen
Hi,all

I am learnning PHP with doing a simple cart function.But, I have a question about it.

the query result can be sent to $_SESSION['cart'] which is applied to include all the selected items, but I cannt print the 'cart' on screen. The code is here:

this is a list.php which is used to show a details of a product.

Code: Select all

 
<?php
session_start();
include "function_search.php";
 
//get the variable which was sent from letf_frame.php;
$proID=$_GET["var"];
 
//call the function select_product(); which is in the function_search.php file;
$a_row = mysql_fetch_assoc(select_product($proID)); 
 
print "<table border = '1'>";
print "<tr> <td width=100px>Product ID</td>
            <td width=170px>Prodcut Name</td>
            <td width=40px>Price</td>
            <td width=80px>Quantity</td>
            <td width=80px>in Stock</td>";
 
print "<tr> <td width=100px>$a_row[product_id]</td>
            <td width=170px>$a_row[product_name]</td>
            <td width=40px>$a_row[unit_price]</td>
            <td width=80px>$a_row[unit_quantity]</td>
            <td width=80px>$a_row[in_stock]</td>";
print "</table>";
 
//form is appled for sending product info to cart.php;
print "<form method='get' target='cart' action='cart.php'>";
print   "<input type='hidden' name='item' value='$a_row'>";
print   "<input type='submit' value='Add'>";
print "</form>";
 
mysql_close($link);
?>
 
In this part, the product's details can be print on screen no problems.

In last part of above code:

Code: Select all

print "<form method='get' target='cart' action='cart.php'>";
print   "<input type='hidden' name='item' value='$a_row'>";
print   "<input type='submit' value='Add'>";
print "</form>";
I use this part to sent $a_row whose value is given by using

Code: Select all

$a_row = mysql_fetch_assoc(select_product($proID));
but I am not very sure about writing value='$a_row' ?

The following part is cart.php

Code: Select all

 
<?php
session_start();
 
if(!empty($_REQUEST['item']))
{
    if(!isset($_SESSION['cart']))
    {
        $_SESSION['cart'][0]=$_REQUEST['item'];
    }
    else
    {
        $_SESSION['cart'][]=$_REQUEST['item'];
    }
    print "<table border = '1'>";
    print "<tr><td width=100px>Product ID</td>
            <td width=170px>Prodcut Name</td>
            <td width=40px>Price</td>
            <td width=80px>Quantity</td>
            <td width=80px>in Stock</td></tr>";
    foreach ( $_SESSION['cart'] as $new)
    {
    print "<tr><td width=100px>$new</td></tr>";
/*<td width=170px>$new[product_name]</td>
<td width=40px>$new[unit_price]</td>
<td width=80px>$new[unit_quantity]</td>
<td width=80px>$new[in_stock]</td></tr>";*/
    }
    print   "</table>";
}
 
?>
I have not complete this cart.php file because I am trying how to print $_SESSION['cart'] on the scrren.

in the mysql , table is like
prodcut_id prodcut_name price
1001 milk 4.30

so the $a_row is a associate array because of "$a_row = mysql_fetch_assoc(select_product($proID)); "

and I think "foreach ($_SESSION['cart'] as $new)" could make $new become each member of $_SESSION['cart'] , in other words , each member should be each item which is add to cart

but when I use "print" to print them , just "A" appears in the table.....not the item's details which was print in list.php.

I have no idea about where is the problem....in the process of transfer $a_row or printing $_SESSION['cart'] :banghead: :banghead: :banghead: