rn14 Posted February 7, 2007 Share Posted February 7, 2007 My problem is whenever I run the code below I get the following error Warning: Invalid argument supplied for foreach() in /home/seachange/domains/sea-change.ie/public_html/cart1/show_cart.php on line 122 However when I just run the php part of this code on its own it runs perfectly and executes everything. As you prob can see Im a beginner but if their is a fix to this please let me know <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <!-- TemplateBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- TemplateEndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- TemplateBeginEditable name="head" --> <!-- TemplateEndEditable --> <style type="text/css"> .newStyle1 { border-right-style: dotted; border-right-width: thin; border-right-color: #0000FF; } .newStyle2 { border: thin solid #0000FF; } .style4 { border: 0px solid #0000FF; } .style6 { border: 1px solid #0000FF; } .newStyle3 { border-width: thin; border-color: #0000FF; } .style10 { color: #0000FF; } .style16 { text-decoration: none; } .newStyle4 { background-color: #3366FF; } .newStyle5 { background-color: #FFFFFF; } </style> </head> <body class="newStyle4"> <div align="center"> <table width="51%" border="1" class="newStyle5"> <tr> <td><table width="51%" height="689" border="0" align="center" class="newStyle5"> <tr> <td height="685" valign="top"> <table width="100%" border="0"> <tr> <td width="61%"><img src="seachangenewlogo1.jpg" width="300" height="166"></td> <td width="39%"><p><img src="Untitled-2.png" width="280" height="90"></p> </td> </tr> </table> <hr class="style10"> <table width="100%" border="0"> <tr> <td width="17%" valign="top"> <table width="136" border="0" class="style4" style="width: 95px"> <tr class="newStyle2"> <td width="128" class="style6" style="width: 87px" valign="top"> <a href="index.html" class="style16"> Home</a></td> </tr> <tr> <td class="style6" style="width: 87px"> <a href="index.html" class="style16"> About Us</a></td> </tr> <tr> <td class="style6" style="width: 87px"> <a href="index.html" class="style16"> Our Events</a></td> </tr> <tr> <td class="style6" style="width: 87px"> <a href="index.html" class="style16"> Newsletter</a></td> </tr> <tr> <td class="style6" style="width: 87px"> <a href="index.html" class="style16"> Donate Online</a></td> </tr> <tr> <td class="style6" style="width: 87px"> <a href="index.html" class="style16"> Our Work</a></td> </tr> <tr> <td class="style6" style="width: 87px"> <a href="index.html" class="style16"> Contact Us</a></td> </tr> </table></td> <td width="83%" valign="top"> <div align="left"> <p align="left </p><?session_start(); ?> "><? include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one if($new) { //new item selected if(!session_is_registered("cart")) { $cart = array(); session_register("cart"); $items = 0; session_register("items"); $total_price = "0.00"; session_register("total_price"); } if($cart[$new]) $cart[$new]++; else $cart[$new] = 1; $total_price = calculate_price($cart); $items = calculate_items($cart); } if($save) { foreach ($cart as $isbn => $qty) { if($$isbn=="0") unset($cart[$isbn]); else $cart[$isbn] = $$isbn; } $total_price = calculate_price($cart); $items = calculate_items($cart); } do_html_header("Your shopping cart"); if($cart&&array_count_values($cart)) display_cart($cart); else { echo "<p>There are no items in your cart"; echo "<hr>"; } $target = "index.php"; // if we have just added an item to the cart, continue shopping in that category if($new) { $details = get_book_details($new); if($details["catid"]) $target = "show_cat.php?catid=".$details["catid"]; } display_button($target, "continue-shopping", "Continue Shopping"); $path = $PHP_SELF; $path = str_replace("show_cart.php", "", $path); display_button("https://".$SERVER_NAME.$path."checkout.php", "go-to-checkout", "Go To Checkout"); do_html_footer(); ?> </p> <p><br> </p> </div> </td> </tr> </table> <hr> <p><font color="#0000FF" size="2">©2007 Seachange</font></p></td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 7, 2007 Share Posted February 7, 2007 Before the foreach on line 20, check that the array being sent has values. if(count($array) > 0){ //foreach }else{ //oops! } Quote Link to comment Share on other sites More sharing options...
rn14 Posted February 7, 2007 Author Share Posted February 7, 2007 Sorry Im really a beginner where exactly will I put this? Please be patient! I think it doesnt pass them when I have the html code but when I just hve the php code its all good Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 7, 2007 Share Posted February 7, 2007 Before the foreach on line 20 Are you saving it as a .php file? Quote Link to comment Share on other sites More sharing options...
rn14 Posted February 7, 2007 Author Share Posted February 7, 2007 Yes Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 7, 2007 Share Posted February 7, 2007 You posted way too much code, and didn't use the code tags, so it's annoying to try to edit it. As I said twice, before the foreach on the line which is causing problems, add another if statment to check the values of the array. It seems the array is empty. Quote Link to comment Share on other sites More sharing options...
rn14 Posted February 7, 2007 Author Share Posted February 7, 2007 Added the php code on its own. no its not receiving values. When I use this php code on its own it works perfectly <?session_start(); ?> "><? include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one if($new) { //new item selected if(!session_is_registered("cart")) { $cart = array(); session_register("cart"); $items = 0; session_register("items"); $total_price = "0.00"; session_register("total_price"); } if($cart[$new]) $cart[$new]++; else $cart[$new] = 1; $total_price = calculate_price($cart); $items = calculate_items($cart); } if($save) { foreach ($cart as $isbn => $qty) { if($$isbn=="0") unset($cart[$isbn]); else $cart[$isbn] = $$isbn; } $total_price = calculate_price($cart); $items = calculate_items($cart); } do_html_header("Your shopping cart"); if($cart&&array_count_values($cart)) display_cart($cart); else { echo "<p>There are no items in your cart"; echo " -------------------------------------------------------------------------------- "; } $target = "index.php"; // if we have just added an item to the cart, continue shopping in that category if($new) { $details = get_book_details($new); if($details["catid"]) $target = "show_cat.php?catid=".$details["catid"]; } display_button($target, "continue-shopping", "Continue Shopping"); $path = $PHP_SELF; $path = str_replace("show_cart.php", "", $path); display_button("https://".$SERVER_NAME.$path."checkout.php", "go-to-checkout", "Go To Checkout"); do_html_footer(); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 7, 2007 Share Posted February 7, 2007 Your original post doesn't have session_start() on the top. Please use the code tags! Highlight your code and hit the # button. Quote Link to comment Share on other sites More sharing options...
rn14 Posted February 7, 2007 Author Share Posted February 7, 2007 This is the code that runs the script correctly. Whenever i add any html to the script it produces that error <?session_start(); ?> "><? include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one if($new) { //new item selected if(!session_is_registered("cart")) { $cart = array(); session_register("cart"); $items = 0; session_register("items"); $total_price = "0.00"; session_register("total_price"); } if($cart[$new]) $cart[$new]++; else $cart[$new] = 1; $total_price = calculate_price($cart); $items = calculate_items($cart); } if($save) { foreach ($cart as $isbn => $qty) { if($$isbn=="0") unset($cart[$isbn]); else $cart[$isbn] = $$isbn; } $total_price = calculate_price($cart); $items = calculate_items($cart); } do_html_header("Your shopping cart"); if($cart&&array_count_values($cart)) display_cart($cart); else { echo "<p>There are no items in your cart"; echo " -------------------------------------------------------------------------------- "; } $target = "index.php"; // if we have just added an item to the cart, continue shopping in that category if($new) { $details = get_book_details($new); if($details["catid"]) $target = "show_cat.php?catid=".$details["catid"]; } display_button($target, "continue-shopping", "Continue Shopping"); $path = $PHP_SELF; $path = str_replace("show_cart.php", "", $path); display_button("https://".$SERVER_NAME.$path."checkout.php", "go-to-checkout", "Go To Checkout"); do_html_footer(); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 7, 2007 Share Posted February 7, 2007 If you're only going to read half of my post each time this is going to take forever. Your original post doesn't have session_start(); When you add your HTML, add it AFTER your session_start(); Try it then. Where does $cart get defined? Quote Link to comment Share on other sites More sharing options...
rn14 Posted February 7, 2007 Author Share Posted February 7, 2007 That fixed it. Sometimes I wonder about myself!!! Just looked at your site you seem to be a bit of a legend with a very good attitude. Im from Ireland fairly big into rugby so look after myself too. If u have any tips on learning php or just in general please let me know!! Thanks again for your help! Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 8, 2007 Share Posted February 8, 2007 Wow, I think you may be the first person EVER to say I have a good attitude I've found the best way to learn is to code as much as possible, so just keep working on it. Find fun projects to do. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.