timmah1 Posted December 19, 2008 Share Posted December 19, 2008 When I use this, I just get Array printed out $sportPackage = $_POST['sport']; $sportPackages = explode(", ", $sportPackage); for($i = 0; $i < count($sportPackages); $i++){ echo $sportPackages[$i]; } When I user this, it prints out every sport there is, not just the ones checked $sportPackage = $_POST['sport']; $sportPackages = implode(", ", $sportPackage); for($i = 0; $i < count($sportPackages); $i++){ echo $sportPackages[$i]; } My input buttons are like this <input name="sport[]" type="hidden" value="ncaab" /> <input name="sport[]" type="hidden" value="ncaaf" /> <input name="sport[]" type="hidden" value="nba" /> Can someone tell me the correct way? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/ Share on other sites More sharing options...
trq Posted December 19, 2008 Share Posted December 19, 2008 foreach ($_POST['sport'] as $sport) { echo $sport; } Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720040 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 This prints out every checkbox that is on the page. I'm only checking two, so those two should be the only one showing correct? Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720041 Share on other sites More sharing options...
timmah1 Posted December 19, 2008 Author Share Posted December 19, 2008 And another question, foreach ($_POST['sport'] as $sport) { echo $sport; } why does this code work in FF, but in IE I get Warning: Invalid argument supplied for foreach() in /home/vegas/public_html/core/billinginfo.php on line 249 Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720042 Share on other sites More sharing options...
premiso Posted December 20, 2008 Share Posted December 20, 2008 And another question, foreach ($_POST['sport'] as $sport) { echo $sport; } why does this code work in FF, but in IE I get Warning: Invalid argument supplied for foreach() in /home/vegas/public_html/core/billinginfo.php on line 249 Should work in both. If it does not chances are it is something with your code or you are not posting the variables right. PHP knows nothing about the browser. It could be a missed tag in your HTML somewhere, and FF parses it right, but IE likes to make a fuss. Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720046 Share on other sites More sharing options...
.josh Posted December 20, 2008 Share Posted December 20, 2008 You get that warning if an array is not posted as the first part of the expression in the foreach. foreach ($array as [$key => ] $val) {...} // key is optional For instance, if you don't check any options, $_POST['sport'] will not exist, and you will get that error. The foreach loop will cycle through all of the values that are checked in your form. It will not cycle through the ones you did not check, because they weren't sent to the script. As far as the script is concerned, the unchecked ones don't exist. All that aside, I'm finding it kind of hard to believe that you're checking hidden input fields. Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720057 Share on other sites More sharing options...
timmah1 Posted December 20, 2008 Author Share Posted December 20, 2008 ok, here's a partial part of my code Notice how the sport field is labeled sport[] <?php if($NFLactive == "1") { ?> <!--start NFL table--> <table width="100%" cellpadding="5" border="1" cellspacing="0" height="175"> <tr> <td align="center" height="15" bgcolor="#000000" class="orderHeader">NFL Pro Football Specials</td> </tr> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="0"> <?php $sql = "SELECT * FROM nfl WHERE active = '1' ORDER BY id"; $q = mysql_query($sql); while($a = mysql_fetch_assoc($q)){ if($a['price'] == "n/a") { $price = "N/A"; } else { $price1 = number_format ($a['price'], 2); $price = "$".$price1; } $packID = $a['id']; ?> <tr> <td height="25" align="left" valign="middle"><?php if($packID === "1"){ $expDate = date("Y-m-d 23:59:59"); } elseif($packID === "2"){ $expDate = date("Y-m-d 23:59:59", strtotime('+1 week')); } elseif($packID === "3"){ $expDate = date("Y-m-d 23:59:59", strtotime('+30 days')); } elseif($packID === "4"){ $expDate = date("Y-m-d 23:59:59", strtotime($stopDate4)); } elseif($packID === "5"){ $expDate = date("Y-m-d 23:59:59", strtotime($stopDate5)); } elseif($packID === "6"){ $expDate = date("Y-m-d 23:59:59", strtotime($stopDate6)); } ?> <input name="sport[]" id="sport[]" type="text" value="nfl" /> <input name="package_[<?=$a[name]; ?>]" type="checkbox" value="<?=$price;?>,<?=$expDate;?>" /> <span class="purchase"> <?=$a[name]; if($a[hint] == "") { } else { ?> <a href="#" class="hintanchor" onMouseover="showhint('<?=$a[hint]; ?>', this, event, '200px')">[?]</a ><?php }?> </span> </td> <td align="right" valign="middle"><span class="purchase"> <?=$price;?></span></td> </tr> <?php }; ?> </table> </td> </tr> </table> <br /> <!--end NFL table--> <?php } ?></td> </tr> <tr> <td align="center" valign="top" colspan="2"><?php if($MLBactive == "1") { ?> <!--start Ultimate table--> <table width="400" border="1" align="center" cellpadding="5" cellspacing="0" height="175"> <tr> <td align="center" height="15" bgcolor="#000000" class="orderHeader">Vegas D Sport Featured Sports Specials</td> </tr> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="0"> <?php $sql = "SELECT * FROM ult WHERE active = '1' ORDER BY id"; $q = mysql_query($sql); while($a = mysql_fetch_assoc($q)){ if($a['price'] == "n/a") { $price = "N/A"; } else { $price1 = number_format ($a['price'], 2); $price = "$".$price1; } ?> <tr> <td height="25" align="left" valign="middle"><input name="sport[]" id="sport[]" type="text" value="ult" /> <input name="package_[<?=$a[name]; ?>]" type="checkbox" value="<?=$price;?>" /> <span class="purchase"> <?=$a[name]; if($a[hint] == "") { } else { ?> <a href="#" class="hintanchor" onMouseover="showhint('<?=$a[hint]; ?>', this, event, '200px')">[?]</a ><?php }?> </span></td> <td align="right" valign="middle"><span class="purchase"> <?=$price;?></span></td> </tr> <?php }; ?> </table></td> </tr> </table> <!--End MLB table--> <?php } ?> Now, when I post to the next page, I need it to show all of the sports that were selected. Using what thrope said foreach($_POST['sport'] as $sport); // $sport = explode(", ", $_POST['sport']); echo $sport; No matter what I check, nfl is always the sport, and if I check 3 boxes, it only shows one, nfl Can you see what's wrong with the code? Thank you again in advane Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720058 Share on other sites More sharing options...
timmah1 Posted December 20, 2008 Author Share Posted December 20, 2008 oh I see Crayon, I never thought about that. The reason it's showing all the fields is because the fields are hidden and have value, so naturally they would be showing How can I get around that? Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720059 Share on other sites More sharing options...
.josh Posted December 20, 2008 Share Posted December 20, 2008 I don't know. Rename your hidden fields to something else. Take them out. Who knows? You haven't really been all that clear about your intentions. Sometimes I get the impression that you just kind of dove right into the deep end of this coding business and now you're struggling to stay afloat. I think you should back up a few steps, read some tutorials on basic principles and syntax, as well as how to debug and ask questions properly. Not saying that to be mean; just think you'd save yourself a lot of headaches if you do. Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720062 Share on other sites More sharing options...
timmah1 Posted December 20, 2008 Author Share Posted December 20, 2008 I appreciate your response Crayon, I just stepped into something over my head, and unfortunately, I have to have it done tonight. After this, I'm going to back off of coding for awhile. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/#findComment-720076 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.