0

i have a multiple select drop down menu and i want to implode all the selections that were chosen. can any one offer some assistance on how to do this please.
this is what i have so far

<?php


function form()
 {
$hostname = "localhost";
$username = "root";
$password = "hayden";
$database = "ecng3020";


$courses_options = "";
$con = mysql_connect("$hostname","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("$database", $con);
  
  $courses_options .= "<option value=\"NULL\">Course :</option>\n";
	$try="Select course_code,course_name FROM courses";
   $rsrcResult = mysql_query($try);
$courses_options .= "<select name=coursecode_1[]  size=4 multiple>";
   while($row = mysql_fetch_row($rsrcResult)) {
     
		$strA=$row[0];
		$strB=$row[1];
	 
	 
	

	 $courses_options .= "<option value=\"$strA\">$strB</option>\n";
   }

   $courses_options .= "</select>";


 echo <<<EOB
 <form method=post action=''>
 <p>$courses_options</p>
 
 
  <p>
  	<input type="submit" name="submit" value="Submit" />
		<input type="reset" name="Reset" value="Cancel" />
  </p>
</form>
EOB;

}
?>
<?php
@session_start();




form();


if ($_POST['submit'] == 'Submit')
{

$hostname = "localhost";
$username = "root";
$password = "hayden";
$database = "ecng3020";

	$con = mysql_connect("$hostname","$username","$password");
	if (!$con)
 	 {
  		die('Could not connect: ' . mysql_error());
  	}

	mysql_select_db("$database", $con);
					
					//$id=$_SESSION['username'];
					@$code= $_POST['coursecode_1'];
					$_SESSION['courses']=$_POST['coursecode_1'];
					
					//print_r($_SESSION['courses']);
					
					
					if( is_array($code))
					{
							//$sum= array();
							
						while (list ($key, $val) = each ($code)) 
						{
							echo $val;
							$c=implode(',',$val);
							echo "$c<br>";
						}
					}
}
?>

when i run this code i am getting an error that says
Warning: implode() [function.implode]: Invalid arguments passed in.......
i am guessing that i need to have another argument for this to work. but how do i get that when all the variables are in an array from the multiple select drop down list
thanks in advance

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by conord
0

Try removing everything in the while loop and use:

$c = implode(',',$code);

and see if that works. Implode looks to take an array and returns a string of the array's values separated by the delimiter you provide.

By looping through the code array the way you are, $val is a string, hence the error you're getting. Another way to get the comma-delimited list you're looking for would be to:

$c='';
while (list ($key, $val) = each ($code))
{
    echo $val;
    $c .= $val.",";
    echo "$c<br>";
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.