Hi, the following code is supposed to display the contents of the shopping cart in a table, however it is not working. all i am getting as output is the header 'your shopping basket'. before i added in the code which gets the unit price, it was all working.
The code is:-
#!/usr/local/bin/php
<?php
session_start();
//connecting to database
$conn = mysql_connect("mysql", "***", "***");
//selecting the database
mysql_select_db("***", $conn);
$display_block = "<h3>Your Shopping Basket";
//check for basket items based on user session id
$get_basket = "SELECT id, sel_frame_id, sel_frame_size, sel_quantity, date_added FROM order_track WHERE session_id = '$PHPSESSID'";
$get_basket_res = mysql_query($get_basket) or die(mysql_error());
if (mysql_num_rows($get_basket_res) < 1) {
//print message
$display_block .= "
You have no items in your basket.
Please continue to shop!
";
}else{
$get_unit_price = "SELECT UnitPrice FROM main WHERE frame_id = '".$sel_frame_id."' AND size = '".$sel_frame_size."'";
$get_unit_price_res = mysql_query($get_unit_price) or die(mysql_error());
if (mysql_num_rows($get_unit_price_res) == 1 ){
//get info and build table
$display_block .= "
Frame ID |
Size |
Unit Price |
Quantity |
Total Price |
Action |
";
while ($basket = mysql_fetch_array($get_basket_res)) {
$id = $basket['id'];
$sel_frame_id = $basket['sel_frame_id'];
$sel_frame_size = $basket['sel_frame_size'];
$sel_quantity = $basket['sel_quantity'];
$unit_price = $basket['unit_price'];
$total_item_price = sprintf("%.02f", $unit_price * $sel_quantity);
$display_block .="
$sel_frame_id
|
$sel_frame_size
|
$unit_price
|
$sel_quantity
|
$total_item_price |
remove |
";
}
$display_block .= "
";
}
}
?>
P & M TRADING & SONS
h1 {text-align: center;
font-size: 36pt}
<? print $display_block; ?>
Comments
I'm not sure where you get all your syntax from but I think there is a problem with your second sql statement specified as:
$get_unit_price = "SELECT UnitPrice FROM main WHERE frame_id = '".$sel_frame_id."' AND size = '".$sel_frame_size."'";
I'm not sure that sytax such as '".$sel_frame_id."' is valid. It should be '$sel_frame_id'. Also, maybe even more important, you never defined the $sel_frame_id variable in your source code. Just because it was a field name defined in a previous sql statement does not mean it is defined at the php level. You will need to do a mysql_fetch_array() of your previous sql call and then define $sel_frame_id using the fetched array contents such as:
$sel_frame_id = $basket['sel_frame_id'];
As your $get_unit_price sql statement currently stands I believe it will always return zero rows. Since all the rest of your code then depends on the test: if (mysql_num_rows($get_unit_price_res) == 1 ) none of the rest of the code will be executed.
: Hi, the following code is supposed to display the contents of the shopping cart in a table, however it is not working. all i am getting as output is the header 'your shopping basket'. before i added in the code which gets the unit price, it was all working.
:
: The code is:-
: #!/usr/local/bin/php
: <?php
: session_start();
:
: //connecting to database
: $conn = mysql_connect("mysql", "***", "***");
:
: //selecting the database
: mysql_select_db("***", $conn);
:
: $display_block = "<h3>Your Shopping Basket";
:
: //check for basket items based on user session id
: $get_basket = "SELECT id, sel_frame_id, sel_frame_size, sel_quantity, date_added FROM order_track WHERE session_id = '$PHPSESSID'";
:
: $get_basket_res = mysql_query($get_basket) or die(mysql_error());
:
: if (mysql_num_rows($get_basket_res) < 1) {
: //print message
: $display_block .= "
You have no items in your basket.
";: Please continue to shop!
:
: }else{
:
: $get_unit_price = "SELECT UnitPrice FROM main WHERE frame_id = '".$sel_frame_id."' AND size = '".$sel_frame_size."'";
:
: $get_unit_price_res = mysql_query($get_unit_price) or die(mysql_error());
:
:
: if (mysql_num_rows($get_unit_price_res) == 1 ){
:
: //get info and build table
: $display_block .= "
:
:
:
:
:
: while ($basket = mysql_fetch_array($get_basket_res)) {
: $id = $basket['id'];
: $sel_frame_id = $basket['sel_frame_id'];
: $sel_frame_size = $basket['sel_frame_size'];
: $sel_quantity = $basket['sel_quantity'];
: $unit_price = $basket['unit_price'];
:
: $total_item_price = sprintf("%.02f", $unit_price * $sel_quantity);
:
: $display_block .="
:
:
: }
:
: $display_block .= "
: }
: }
:
: ?>
:
:
:
P & M TRADING & SONS
::
: h1 {text-align: center;
: font-size: 36pt}
:
: <? print $display_block; ?>
:
:
:
:
: