Jump to content

returning var to display message


mike12255

Recommended Posts

So i got two files one productdetial.php and another cart-functions.php. Product detail is going to a function (addtocart()) and i've tried to set it so if their is less then what the user has, then return $error saying that. the function kind of works, it dosnt add anymore to the cart after the user has more then whats in stock. But apparently error is not being returned because it keeps saying undifiened variable $error. Ill bold the part in cart-function.php that i added. And if anyone can figure out how to get this returning the error messaging working - ill be forever happy :D

productdetail.php:

<?php
require_once 'library/cart-functions.php';
if (!defined('WEB_ROOT')) {
exit;
}?>


<style type="text/css">
<!--
.boldsmall {
font-weight: bold;
font-size: xx-small;
}
.name{
padding: 8px 0px 6px 10px; 
background:url(../images/content_top.png) no-repeat;
}
-->
</style>

<?php
$product = getProductDetail($pdId, $catId);

// we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url
extract($product);
?> 
<table width="100%" border="0" cellspacing="0" cellpadding="10">
   <?php 
  if(isset($_POST['submit'])){
   if ($error){
   echo '<tr>';
echo '<td><span class="style11">$error</span></td>';
echo '</tr>';
}
}?>

<td style="padding: 8px 0px 6px 10px;"><?php echo $pd_name; ?><br></td>
<tr> 
  <td align="center"><img src="<?php echo $pd_image; ?>" border="0" alt="<?php echo $pd_name; ?>"></td>
  <td valign="middle">

Price : <?php echo displayAmount($pd_price); ?><br>
<?php
// if we still have this product in stock
// show the 'Add to cart' button
if ($pd_qty > 0) {
?>
<input type="button" name="submit" id= "submit" value="Add To Cart >" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton">
<?php
//die($cart_url);
} else {
echo 'Out Of Stock';
}
?>
  </td>
</tr> 
<tr align="left"> 
  <td colspan="2"><?php 
  $amount = getqty();
  $tempn = $pd_name . "(s)";
  echo "<span style='color: #000; font-weight: bold;  font-size: 12;'> You currently have $amount of $tempn in your cart, <a href=cart.php> view cart</a></span>"; ?></td>
</tr>

<tr align="left"> 
  <td colspan="2"><?php echo $pd_description; ?></td>
</tr>

</table>

 

cart-functions.php:

<?php
require_once 'config.php';

/*********************************************************
*                 SHOPPING CART FUNCTIONS 
*********************************************************/

function addToCart()
{
// make sure the product id exist
if (isset($_GET['p']) && (int)$_GET['p'] > 0) {
	$productId = (int)$_GET['p'];
} else {
	header('Location: index.php');
}

// does the product exist ?
$sql = "SELECT pd_id, pd_qty, pd_popular
        FROM tbl_product
		WHERE pd_id = $productId";
$result = dbQuery($sql);

if (dbNumRows($result) != 1) {
	// the product doesn't exist
	header('Location: cart.php');
} else {
	// how many of this product we
	// have in stock
	$row = dbFetchAssoc($result);
	$currentStock = $row['pd_qty'];

	$pop = $row['pd_popular'];
	$pop = $pop++;
	$update = "UPDATE `schoolw1_niftys`.`tbl_product` SET `pd_popular` = '1' WHERE `tbl_product`.`pd_id` =  $productId LIMIT 1";
	mysql_query($update)or die (mysql_error());




	if ($currentStock == 0) {
		// we no longer have this product in stock
		// show the error message
		setError('The product you requested is no longer in stock');
		header('Location: cart.php');
		exit;
	}

}		

// current session id
$sid = session_id();

// check if the product is already
// in cart table for this session
$sql = "SELECT *
        FROM tbl_cart
		WHERE pd_id = $productId AND ct_session_id = '$sid'";
$result = dbQuery($sql);
[b]$row = dbFetchAssoc($result);
if ($currentStock < $row['ct_qty']){
$error = "No more of this in stock";
$stop = "yes";
return $error;
}else{
$stop = "no";
}
if($stop != "yes"){[/b]
if (dbNumRows($result) == 0) {
	// put the product in cart table
	$sql = "INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ct_date)
			VALUES ($productId, 1, '$sid', NOW())";
	$result = dbQuery($sql);
} else {
	// update product quantity in cart table
	$sql = "UPDATE tbl_cart 
	        SET ct_qty = ct_qty + 1
			WHERE ct_session_id = '$sid' AND pd_id = $productId";		

	$result = dbQuery($sql);		
}	
}else{
$error = "";
return $error;
}

$row = dbFetchAssoc($result);

if ($currentStock < $row['ct_qty']){

$error = "No more of this in stock";

$stop = "yes";

return $error;

}else{

$stop = "no";

}

if($stop != "yes"){

Link to comment
https://forums.phpfreaks.com/topic/150533-returning-var-to-display-message/
Share on other sites

Replace this:

<?php
if(isset($_POST['submit'])){
   if ($error){
?>

with this:

<?php
if(isset($_POST['submit'])){
   $error=addToCart();
   if ($error!=''){
?>

Didn't want to go through all of your code, but more than likely this call to addToCart() will have to replace the call that you have already made or will make to that function.

Your problem was that $error is defined with in that function, therefore it does not exist outside of that function, but the function will still return the value of $error so you have to catch that return and assign it to a variable. So in order to learn from this you have to understand that the $error with in the function addToCart() is not the same variable as the $error that I defined outside of the function. This is known as variable Scope.

not 100% sure but i thought that <

input type="button" name="submit" id= "submit" value="Add To Cart >" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton"> 

was some how activating the function - since the function kind of works just not the return

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.