Jump to content

Need help wraping the body of my shopping cart in an email


Agreaves

Recommended Posts

The url for this page is localhost/cart/index.php. Let me post the whole code cuz maybe im doing something wrong further in the script. This is the whole script for the entire shopping cart.

$product_id = array_key_exists('id', $_GET) ? $_GET['id']: NULL;	

$action =  array_key_exists('action',$_GET) ? $_GET['action']: "empty";
?>
<?php
//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) 
{
  	die("Product Doesn't Exist");
}
	switch($action) { //decide what to do 

    case "add":
         $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id 
    break;

    case "remove":
         $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
        if($_SESSION['cart'][$product_id] === 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise it will show zero, then -1, -2 etc when the user keeps removing items. 
    break;

    case "empty":

default:
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
    break;	 
} 
?>
<?php			 			 			
if(isset($_SESSION['cart'])) { //if the cart isn't empty
//show the cart
	echo "<form method=\"post\" name=\"body\" action=\"$_SERVER[php_SELF]\">";
	echo "<table align=\"center\" id=\"tbl_bdr\" width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">
<tr align=\"left\">
<td align=\"left\"><b>Name:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"name\"/></td>
<td align=\"left\"><b>Email:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"email\"/></td>
  	</tr>
<tr align=\"left\">
<td align=\"left\"><b>Address:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"address1\"/></td>
<td align=\"left\"><b>Address(ext):</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"address2\"/></td>
  	</tr>
<tr align=\"left\">
<td align=\"left\"><b>Telephone:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"telephone\"/></td>
  	</tr>
<tr><td height=\"10\"></td></tr>
</table>";
    echo "<table id =\"cart_bdr\" align =\"center\" border=\"0\" padding=\"15\" width=\"950\">"; //format the cart using a HTML table
echo "<tr id=\"hdg_bkgrnd\" height=\"40\"><td width=\"237\" align=\"center\"><b>Thumbnail</b></td><td width=\"237\" align=\"center\"><b>Id</b></td><td width=\"237\" align=\"center\"><b>Name</b></td><td width=\"237\" align=\"center\"><b>Quantity</b></td><td width=\"237\" align=\"center\"><b>Amount</b></td></tr>";

$total = 0;
    //iterate through the cart, the $product_id is the key and $quantity is the value
foreach ($_SESSION['cart'] as $product_id => $quantity) {

//get the name, price and pix from the database - this will depend on your database implementation.
    //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
    $sql = sprintf("SELECT Id, Name, Price, Pix FROM products WHERE Id = %d;", $product_id);

    $result = mysqli_query($con,$sql) or die("Couldnt run query");

$row = mysqli_num_rows($result);		

// Smarty variables application

// Initialize the array for the results
$record = Array();  

$sql2 = sprintf("SELECT Id, Name, Price, Pix FROM products WHERE Id = %d;", $product_id);

    $result2 = mysqli_query($con,$sql2) or die("Couldnt run query");

$rows = mysqli_fetch_assoc($result2);

while ($rows = mysqli_fetch_assoc($result2))
{	
$record[] = $rows;
}
// Assign Smarty variables
$smarty->assign('record',$record);

//Only display the row if there is a product (though there should always be as we have already checked)
if($row > 0) {

list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result);

    $line_cost = $Price * $quantity; //work out the line cost

$total += $line_cost;

    echo "<tr height=\"55\">";
echo "<td align=\"center\"><img id=\"thumb_bdr\" src=\"$Pix\" width=\"50\" height=\"50\"/></td>";
echo "<td align=\"center\" id=\"cols_bkgrnd\"><div name=\"id\">$Id</div></td>";
    //show this information in table cells
    echo "<td align=\"center\" id=\"cols_bkgrnd\">$Name</td>";
    //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current 			product
    echo "<td align=\"center\" id=\"cols_bkgrnd\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>";
    echo "<td align=\"right\" id=\"cols_bkgrnd\">$$line_cost</td>";
    echo "</tr>";
}
}
//show the total
    echo "<tr id=\"hdg_bkgrnd\" height=\"40\">";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
    echo "<td align='right'><div id=\"total\"><b>Total</b></div></td>";
    echo "<td align='right'>$$total</td>";
    echo "</tr>";
echo "</table>";

    //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<table width=\"950\" align=\"center\">";
    echo "<tr>"; 
   	echo "<td width=\"237\"></td>";
echo "<td width=\"237\"></td>";
echo "<td width=\"237\"></td>";
echo "<td width=\"237\" align=\"right\"><input id=\"submit_btn\" value=\"Send\" type=\"submit\"/></td>"; 
    echo "<td width=\"237\"><input id=\"cart_btn\" type=\"button\" value=\"Empty\" onclick=\"window.location.href='$_SERVER[php_SELF]?action=empty'\" /></td>";
    echo "</tr>";
    echo "</table>";
echo "</form>";

} else
{

//otherwise tell the user they have no items in their cart 
    echo "You have no items in your shopping cart.";
echo $message;
}

//function to check if a product exists
function productExists($product_id) {
	global $product_id;
	global $con;
	global $host;
	global $user;
	global $pass;
	global $dbase;
		//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
		$sql = sprintf("SELECT * FROM products WHERE id = %d;", $product_id);

		$con = mysqli_connect($host,$user,$pass,$dbase) or die("couldnt connect to server");

		$result = mysqli_query($con,$sql) or die ('Could not run query');

		$row = mysqli_num_rows($result);

		return $row > 0;
}

Link to comment
Share on other sites

If the URL doesn't have any parameters, your $_GET array will be empty.

 

I misunderstood the purpose of your "empty" string, change the last case to

    case "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
    break;
default:
break;

Link to comment
Share on other sites

took this from the PHP manual:

Modification of the array during listexecution (e.g. using list($a, $b) = $b) results in undefined behavior.

 

change:

list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result);

 

to:

 


while(list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result)) { //code here
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.