Jump to content

Recommended Posts

My function for showCart:

function showCart() {
global $db;

echo "<pre><div style=\"text-align: left;\">";
print_r ($_SESSION['cart']);
echo "</div></pre>";




if (isset($_SESSION['cart'])) {

      	  print '<form action="index.php?page=cart&action=update" method="post" id="cart">';

  foreach ($_SESSION['cart'] as $id => $array2){
  foreach ($array2 as $material => $array3){

$result = mysql_query("SELECT * FROM p_products WHERE id='$id'") or DIE(mysql_error());

	while($r=mysql_fetch_array($result))
	{

	$partnumber=$r["partnumber"];
	$partname=$r["partname"];
	$description=$r["description"];
	$category=$r["category"];
	$price=$r["price"];

	}




echo "
<table width=\"590\" style=\"border-top: 1px solid #000;\">
<tr>
<td width=\"100\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" /></td>
<td width=\"350\">
$partname<br />
$partnumber<br />
$price / each<br />
Material: $material
</td>
<td width=\"60\"><input type=\"text\" name=\"qty$id\" value=\"{$array3['qty']}\" size=\"3\" maxlength=\"3\" /></td>
<td width=\"80\">$($price * {$array3['qty']})</td>
</tr>





</table>

";


    }
}


     	 print '</form>';
} else {
     	 print '<p>You shopping cart is empty.</p>';
}


}

 

As you can see right now I am using echo/print to get the info out, but that's putting the information above all of my content of my website, when I want it to go under "Shopping Cart" where I have the variable $showcart at..

Return values are great. Let's say you want to do a math calculation. This would be an ideal way to do it:

 

<?php
$num1 = 5;
$num2 = 6;
$num3 = 3;

$returnVal = add($num1, $num2);
echo $num1 . " + " . $num2 . " = " . $returnVal . "<br />";
$returnVal2 = sub($returnVal, $num3);
echo $num3 . " - " . $returnVal . " = " . $returnVal2 . "<br />";

function add($val1, $val2) {
    return ($val1 + $val2);
}

function sub($val1, $val2) {
    return ($val1 - $val2);
}
?>

 

Hopefully that will help you understand the return. It allows the data to be assigned to a variable.

Return ends the script, which causes everything that should be displayed after that function to not exist.. So I am thinking this isn't what I'm looking for? Here is what I have:

 

 

function in my functions.php


function showCart() {
global $db;






if (isset($_SESSION['cart'])) {

      	  print '<form action="index.php?page=cart&action=update" method="post" id="cart">';

  foreach ($_SESSION['cart'] as $id => $array2){
  foreach ($array2 as $material => $array3){

$result = mysql_query("SELECT * FROM p_products WHERE id='$id'") or DIE(mysql_error());

	while($r=mysql_fetch_array($result))
	{

	$partnumber=$r["partnumber"];
	$partname=$r["partname"];
	$description=$r["description"];
	$category=$r["category"];
	$price=$r["price"];

	}




return "
<table width=\"590\" style=\"border-top: 1px solid #000;\">
<tr>
<td width=\"100\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" /></td>
<td width=\"350\">
$partname<br />
$partnumber<br />
$price / each<br />
Material: $material
</td>
<td width=\"60\"><input type=\"text\" name=\"qty$id\" value=\"{$array3['qty']}\" size=\"3\" maxlength=\"3\" /></td>
<td width=\"80\">$($price * {$array3['qty']})</td>
</tr>





</table>

";


    }
}


     	 print '</form>';
} else {
     	 print '<p>You shopping cart is empty.</p>';
}


}

 

 

 

my cart.php


$showCart = showCart(0);

$content .= "<div id=\"full_content\">

Shopping Cart<br />

$showCart

</div>";


It does not end the script persay, it just exits the function. If you want the function to continue, this would work and is actually better, imo, than just printing data onto the screen.

 

function showCart() {
global $db;

$output = "";
if (isset($_SESSION['cart'])) {

         $output .= '<form action="index.php?page=cart&action=update" method="post" id="cart">';

  foreach ($_SESSION['cart'] as $id => $array2){
  foreach ($array2 as $material => $array3){

$result = mysql_query("SELECT * FROM p_products WHERE id='$id'") or DIE(mysql_error());

      while($r=mysql_fetch_array($result))
      {

      $partnumber=$r["partnumber"];
      $partname=$r["partname"];
      $description=$r["description"];
      $category=$r["category"];
      $price=$r["price"];

      }

         $output .= "
<table width=\"590\" style=\"border-top: 1px solid #000;\">
<tr>
<td width=\"100\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" /></td>
<td width=\"350\">
$partname<br />
$partnumber<br />
$price / each<br />
Material: $material
</td>
<td width=\"60\"><input type=\"text\" name=\"qty$id\" value=\"{$array3['qty']}\" size=\"3\" maxlength=\"3\" /></td>
<td width=\"80\">$($price * {$array3['qty']})</td>
</tr>
</table>";
    }
   }


         $output .= '</form>';
} else {
         $output .= '<p>You shopping cart is empty.</p>';
}

return $output;

}

 

You would be fine ;)

You're making this more complicated then it should be.  Returns are a basic concept of most languages.  In your function have one variable called $return_string.  Just concatenate everything to it and return it at the end.  Then when you want to display everything just do:

 

echo showCart();

 

Just like premiso exemplified...

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.