sparhawks Posted October 17, 2009 Share Posted October 17, 2009 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 https://forums.phpfreaks.com/topic/178010-comma-seperated-implode-modifying-returned-string/ Share on other sites More sharing options...
Warz Posted October 17, 2009 Share Posted October 17, 2009 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 https://forums.phpfreaks.com/topic/178010-comma-seperated-implode-modifying-returned-string/#findComment-938565 Share on other sites More sharing options...
sparhawks Posted October 17, 2009 Author Share Posted October 17, 2009 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 https://forums.phpfreaks.com/topic/178010-comma-seperated-implode-modifying-returned-string/#findComment-938576 Share on other sites More sharing options...
Warz Posted October 17, 2009 Share Posted October 17, 2009 Yes you have, I thought you ment textfield... you mean checkbuttons right? Then it's a lot more simple... <?php $arr=$_POST['text']; // implode: $list = implode(",", $arr); ?> Link to comment https://forums.phpfreaks.com/topic/178010-comma-seperated-implode-modifying-returned-string/#findComment-938577 Share on other sites More sharing options...
Dorky Posted October 17, 2009 Share Posted October 17, 2009 $arr[]=$_POST['text']; otherwise it will only grab the first value and stop there Link to comment https://forums.phpfreaks.com/topic/178010-comma-seperated-implode-modifying-returned-string/#findComment-938668 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.