1 Replies - 2831 Views - Last Post: 19 June 2012 - 07:35 AM Rate Topic: -----

#1 ibnmvungi  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 20-April 12

Call to a member function cart() on a non-object in E:\wamp\

Posted 19 June 2012 - 06:34 AM

Hallo ther i have two functions cart and total, and i have the variable $total in the function cart,and i want this variable to be passed to the function total.here is what i did



function cart(){

  $total=3;


}

function total($total){

return $total;
}





but i get these errors when the codes excutes "Call to a member function cart() on a non-object in E:\wamp\" and "Undefined variable: total in E:\wamp\wamp\www\udomsa\myshoppingcart\billing.php on line 42"



here is the full codes of the two functions
<?php
function cart(){
   ?>
     
				 <table align="center" border="0">
				 <div>
	                <tr style="font-weight:bold; background:#666600;">
					
					  
       					  Your Shopping Cart
				       <th>Name</th><th>Quantity</th><th>Price</th><th>Total Amount</th><th>Options</th>
					  									     </tr>
				<?php
					
                      global  $total;				
               foreach($_SESSION as $name=> $value){
			   
			   if($value>0)
			   {
			   global $sub;
		
			    if(substr($name,0,5)=='cart_')
				{
				 $id=substr($name,5,(strlen($name))-5);
				 $get=mysql_query("SELECT serial,name,description,price FROM products   WHERE serial=".mysql_real_escape_string((int)$id)." ");
				 ?>
				 	 <?php
				 while($get_row=mysql_fetch_assoc($get))
			{
				 $sub=$get_row['price']*$value;
				    ?>
				 <tr>
				 
				 <td>	<?php echo $get_row['name']?></td>
				 <td><input type="text" name="<?php echo $value?>" value="<?php echo $value ?>" maxlength="3" size="2"" /></td>
				 <td>	<?php echo '&pound'.number_format($get_row['price'],2)?></td>
				 <td>	<?php echo '&pound'.number_format($sub,2)?></td>
				 <td><a href="shoppingcart.php?Delete=<?php echo $id?>" style="text-decoration:none"/><input height="45" width="50" type="button" name="butt" value="Remove" /><a href="shoppingcart.php?add=<?php echo $id?>" style="text-decoration:none"/><input height="45" width="50" type="button" name="butt" value="add" /></a><a href="shoppingcart.php?remove=<?php echo $id?>" style="text-decoration:none"/><input height="45" width="50" type="button" name="butt" value="Reduce" /></a>
					</td>
					</tr><br/>
				 <?php
				 	
										 
			}
			
				     
				 
				
				}
				
			   
			$total=$total+$sub;	 
				
		}
			   }
			    
			   			   ?>
			   <tr>
			       <td>&nbsp;</td><td>&nbsp;</td><td>Total :</td><td>
				   <?php  
				   if($total>0){
			          echo $total;
					}
			   else{
			    echo "";
				total($total);
			   }
			  //session_destroy();

				   
				
				   ?>
				   </td><td>&nbsp;</td>
				     </tr>
					 </div>
			       </table><br/><br/>
			   
			   <?php
			   //$_SESSION['TOTAL']=$total;
			   echo "<center><a href='index.php' style='text-decoration:none;'>Back shopping</a>  |   <a href='index.php?action=billing' style='text-decoration:none;'>Continue to checkout!</a></center> ";
    }
	
	function total($total){
	
	  
	   return  $total;
	
	}
?>




Is This A Good Question/Topic? 0
  • +

Replies To: Call to a member function cart() on a non-object in E:\wamp\

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Call to a member function cart() on a non-object in E:\wamp\

Posted 19 June 2012 - 07:35 AM

Hey, you must not mix HTML and PHP like that especially within a function. Makes it twice as confusing to follow. You should also refrain from making variables global.

To create HTML such as generate a table PHP provides objects such as DOMDocument and SimpleXML to do HTML\XML manipulation. It is also far neater to use this objects. Here's an example of creating a table with a THEAD, TBODY and TFOOT using DOMdocument.
$dom = new DOMDocument();
$table = $dom->appendChild(new DOMElement('table'));

$thead = $table->appendChild(new DOMElement('thead'));
$tbody = $table->appendChild(new DOMElement('tbody'));
$tfoot = $table->appendChild(new DOMElement('tfoot'));

echo $dom->saveHTML();



Adding multiple TR and TD elements is just as easy. You can also use a template engine to generate your HTML and still avoid mixing your HTML and PHP. Check out Smarty Template Engine.

Your function total() must accept a parameter. Basically you didn't specify a default value for the $total parameter.
function foo($bar = 10) //default value specified here
{
    return $bar;
}

echo foo(); //will output 10
echo foo(15); //will output 15


function foo($bar) //no default value specified
{
    return $bar;
}

echo foo(); //produces an error. foo function requires a parameter



Practice code separation. It will make debugging a lot easier for you and for us when you need help with something. Have a look at this tutorial: Code Separation

This post has been edited by codeprada: 19 June 2012 - 07:36 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1