Jump to content

[SOLVED] Explode - Implode - Which to use??


Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/137762-solved-explode-implode-which-to-use/
Share on other sites

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

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.

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.

 

 

 

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

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. 

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.