Jump to content

Comma seperated implode & modifying returned string.


sparhawks

Recommended Posts

I have a PHP post form that posts up to 4 variables. I need whatever variables are passed to be combined into one comma seperated string (eg variable1,variable2,variable3,variable4). This i can do with the code below.

 

$combo = array($one, $two, $three, $four);
$list = implode(",", $combo);

 

my problem is that the form fields are optional by design so that the user doesn't always post all 4 variables. Sometimes they will post 1, sometimes they will post all 4. So if a user only selects two fields and submits the form i will end up with a string looking like:

variable1,variable2,,

when what i really want is (note the removal of the trailing commas at the end of the string):

variable1,variable2

 

Can anyone point me towards a possible solution for this?

Link to comment
Share on other sites

You could make arrays in your form like this:

<input type="text" name="text[1]" />
<input type="text" name="text[2]" />
<input type="text" name="text[3]" />
<input type="text" name="text[4]" />

 

...and do like this:

<?php

$arr=$_POST['text'];
$i=0;
foreach ($arr as $key => $value) {
      if ($value != "")
      {
            $variables[$i] = $value;
      }
$i++;
}
// implode:
$list = implode(",", $variables);
?> 

Link to comment
Share on other sites

Thanks, I gave it a try, but i get two warnings.

 

Warning: Invalid argument supplied for foreach() on line 12

 

Warning: implode() [function.implode]: Invalid arguments passed in on line 20

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TEST</title>
</head>
<body>
<?php
$arr=$_POST['text'];
$i=0;
foreach ($arr as $key => $value) {
      if ($value != "")
      {
            $variables[$i] = $value;
      }
$i++;
}
// implode:
$list = implode(",", $variables);
?>
LIST: <? echo $list; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <input type="checkbox" name="text[1]" value="1"/>
  Calm<br/>
  <input type="checkbox" name="text[2]" value="2"/>
  Bourke<br/>
  <input type="checkbox" name="text[3]" value="3"/>
  Playful<br/>
  <input type="checkbox" name="text[4]" value="4"/>
  Shopping
  <input type="submit" name="btnSendForm" value="Submit" />
</form>
</body>
</html>

 

Have i missed anything?

Link to comment
Share on other sites

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.