Hi,
Im trying to have a section that shows the Total and Quantity at the top after customers are logged in and if they dont have anything in their baskets/cart then show Total = 0.00 and Quantity = 0.

The Codes work fine when the customer already has something in the Basket/Cart but if nothing has been written to the query or inserted it just writes Total: and Quantity: ( Leaves the total and Quantity Blank instead of saying 0 or 0.00)

<?php
		   if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) 
{
	//Adding the Your Shopping Cart - # of Items and Total HERE
	
		$custsql = "SELECT id, status FROM lgmsevze_passion.orders WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status < 2;";
      	$custres = mysql_query($custsql);
      	$custrow = mysql_fetch_assoc($custres);
	
			
	 	$sql = "SELECT SUM(quantity) as quantity FROM lgmsevze_passion.orderitems WHERE order_id=" . $custrow['id'] . ";";   
		$result = mysql_query($sql);
	
	if (mysql_num_rows($result) > 0) 
	{
		$query_data=mysql_fetch_array($result);
		$qty= $query_data["quantity"];
	}
 	else
	{
		$qty = "0";
	}
 		
  		$totalsql = "SELECT total FROM lgmsevze_passion.orders WHERE customer_id=" . $_SESSION['SESS_USERID'] . "AND status < 2;";
 		 $resultsql = mysql_query($totalsql);
	if ($resultsql && mysql_num_rows($resultsql) > 0) 
	{
			$fetch_data = mysql_fetch_array($resultsql);
			$order_total = sprintf('%.2f', $fetch_data["total"]);

	}
	else
	{
		$order_total = "0.00";
	}
        
	echo "<td align='left' valign='top' class='login_td'><p>Hi, <strong>" . ucwords($_SESSION['SESS_USERNAME']) . "</strong> [<a href='logout.php'>Logout</a>]<br/></p><p>"; 
		echo "<div><strong>Your Shopping Cart</strong>";
		echo "<br />";
		echo "<strong>Items:</strong> " . $qty . " <strong>Total:</strong> $". $order_total ."</div>";
    echo "</p></td>";
}
	  else {
	  		echo "<td align='left' valign='top' class='login_td'><p>
      <form action=\"login2.php\" method='post'><div class='login'><input type='text' value='Username' onclick=\"if(this.value=='Username')this.value='';\" onblur=\"if(this.value=='')this.value='Username';\" name=\"userBox\" class='login_input' /><div class='login'><input type='password' value='Password' onclick=\"if(this.value=='Password')this.value='';\" onblur=\"if(this.value=='')this.value='Password';\" name=\"passBox\" class='login_input' /><div><input style='background: url(img/login_btn.jpg) no-repeat; border:0;height:22px;width:56px;margin-left:75px;' value='' type='submit' name='submit'></div></div></div></form></p></td>";
	  }
		?>

But the Query of course dies at the start b.c nothing is written in the database yet so it wont print out the Qty Else or the Order_Total Else statement.

How to fix this issue. Please Help.

Thanks in advance

Hi,
Im trying to have a section that shows the Total and Quantity at the top after customers are logged in and if they dont have anything in their baskets/cart then show Total = 0.00 and Quantity = 0.

The Codes work fine when the customer already has something in the Basket/Cart but if nothing has been written to the query or inserted it just writes Total: and Quantity: ( Leaves the total and Quantity Blank instead of saying 0 or 0.00)

<?php
		   if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) 
{
	//Adding the Your Shopping Cart - # of Items and Total HERE
	
		$custsql = "SELECT id, status FROM lgmsevze_passion.orders WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status < 2;";
      	$custres = mysql_query($custsql);
      	$custrow = mysql_fetch_assoc($custres);
	
			
	 	$sql = "SELECT SUM(quantity) as quantity FROM lgmsevze_passion.orderitems WHERE order_id=" . $custrow['id'] . ";";   
		$result = mysql_query($sql);
	
	if (mysql_num_rows($result) > 0) 
	{
		$query_data=mysql_fetch_array($result);
		$qty= $query_data["quantity"];
	}
 	else
	{
		$qty = "0";
	}
 		
  		$totalsql = "SELECT total FROM lgmsevze_passion.orders WHERE customer_id=" . $_SESSION['SESS_USERID'] . "AND status < 2;";
 		 $resultsql = mysql_query($totalsql);
	if ($resultsql && mysql_num_rows($resultsql) > 0) 
	{
			$fetch_data = mysql_fetch_array($resultsql);
			$order_total = sprintf('%.2f', $fetch_data["total"]);

	}
	else
	{
		$order_total = "0.00";
	}
        
	echo "<td align='left' valign='top' class='login_td'><p>Hi, <strong>" . ucwords($_SESSION['SESS_USERNAME']) . "</strong> [<a href='logout.php'>Logout</a>]<br/></p><p>"; 
		echo "<div><strong>Your Shopping Cart</strong>";
		echo "<br />";
		echo "<strong>Items:</strong> " . $qty . " <strong>Total:</strong> $". $order_total ."</div>";
    echo "</p></td>";
}
	  else {
	  		echo "<td align='left' valign='top' class='login_td'><p>
      <form action=\"login2.php\" method='post'><div class='login'><input type='text' value='Username' onclick=\"if(this.value=='Username')this.value='';\" onblur=\"if(this.value=='')this.value='Username';\" name=\"userBox\" class='login_input' /><div class='login'><input type='password' value='Password' onclick=\"if(this.value=='Password')this.value='';\" onblur=\"if(this.value=='')this.value='Password';\" name=\"passBox\" class='login_input' /><div><input style='background: url(img/login_btn.jpg) no-repeat; border:0;height:22px;width:56px;margin-left:75px;' value='' type='submit' name='submit'></div></div></div></form></p></td>";
	  }
		?>

But the Query of course dies at the start b.c nothing is written in the database yet so it wont print out the Qty Else or the Order_Total Else statement.

How to fix this issue. Please Help.

Thanks in advance

Hi,

here's something that may act like a quick-fix (or maybe even a final solution)

Adding:

$qty = "0";
$order_total = "0.00";

to the very beginning of your script should solve this instantly,
and if you wanna get further into it, I'd look for a catch in those
two "if" statements, obviously they're not formulated right, as "else" never
occurs.

That may just work... lol... then the code will be overwritten once the IF statements are executed CORRECT???

Exactly. And if IF's don't occur, variables will keep their 0 and 0.00 values, so your cart will display 0 an 0.00

thanks! =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.