aebstract Posted February 3, 2009 Share Posted February 3, 2009 I have this: $showCart = showCart(); $content .= "<div id=\"full_content\"> Shopping Cart<br /> $showCart </div>"; How can I have lines of code in my showCart(); ehcho out in to my div where I have $showCart displayed? Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/ Share on other sites More sharing options...
DeanWhitehouse Posted February 3, 2009 Share Posted February 3, 2009 like $showCart(0); or use a foreach loop Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753776 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 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.. Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753779 Share on other sites More sharing options...
DeanWhitehouse Posted February 3, 2009 Share Posted February 3, 2009 Try $content .= "<div id=\"full_content\"> Shopping Cart<br /> ".showCart()." </div>"; Or the best way would be to remake the function Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753785 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 That still displays it above everything instead of inside that div... which I'm thinking I need to use something other than echo to get this information out, anyone? Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753790 Share on other sites More sharing options...
DeanWhitehouse Posted February 3, 2009 Share Posted February 3, 2009 You can use return (in the function) Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753795 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 I'm looking at what return does and I don't really see how that would do anything, mind showing a small example of this? Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753805 Share on other sites More sharing options...
DeanWhitehouse Posted February 3, 2009 Share Posted February 3, 2009 function returning() { return "Code"; } function echoing() { echo "Code"; } echo echoing()." <br> ".returning(); Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753810 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Hmm, return seems weird to me.. but it worked Thanks Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753814 Share on other sites More sharing options...
premiso Posted February 3, 2009 Share Posted February 3, 2009 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. Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753822 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 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>"; Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753827 Share on other sites More sharing options...
premiso Posted February 3, 2009 Share Posted February 3, 2009 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 Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753834 Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Ah, I get it Works like a charm. Thank you Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753838 Share on other sites More sharing options...
Maq Posted February 3, 2009 Share Posted February 3, 2009 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... Link to comment https://forums.phpfreaks.com/topic/143662-solved-echo-to-specific-location-from-function/#findComment-753839 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.