Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

probs displayin items in shopping cart

sb3sb3 Posts: 4Member
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 .= "
Frame ID Size Unit Price Quantity Total Price Action
$sel_frame_id
$sel_frame_size
$unit_price
$sel_quantity
$total_item_price remove
";
}
}

?>

P & M TRADING & SONS


h1 {text-align: center;
font-size: 36pt}

<? print $display_block; ?>




Comments

  • mainewoodsmainewoods Posts: 18Member

    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 .= "
    Frame IDSizeUnit PriceQuantityTotal PriceAction
    $sel_frame_id $sel_frame_size $unit_price $sel_quantity $total_item_priceremove
    ";
    : }
    : }
    :
    : ?>
    :
    :
    :

    P & M TRADING & SONS

    :
    :
    : h1 {text-align: center;
    : font-size: 36pt}
    :
    : <? print $display_block; ?>
    :
    :
    :
    :
    :

Sign In or Register to comment.