Hello,i m newer to the php.
my problem is...
i have one form with textfield and multiple selection listbox. i select multiple values form listbox, implode it with comma and put all values in single database field. its working.
But, i cant retrieve them with explode function . i want to put all array's values in listbox , but with selected, so that the user can get an idea about previous selection.
Could anyone please help me?

Following code may help you

<html>
<body>
<form>
<!-- copy following php code at the place where you are populating your list box-->
<?php


/*
set $mywholearray  from database
this array contains all master values to be shown in listbox 
	$mywholearray[0]['id']='pkvalue',
	$mywholearray[0]['desc']='description',
*/
/*set $myselarray from database
this array contains only those values which are selected and stored in database
	$myselarray[0]['id']='pkvalue'

*/
$elementstring ="\n<SELECT id='listbox'  size=10 name='listbox' multiple>";			
for ($i=0;$i<count($mywholearray);$i++)
{

	 $seltext="";
	 for($j=0;$j<count($myselarray);$j++)
	 {
	    if ($mywholearray[$i]['id']==$myselarray[$j])
                   {
		  	$seltext=" selected ";
		break;	
                     } 
	 }

	 $elementstring .= "\n";
     $elementstring .=  "<OPTION value='{$mywholearray[$i]['id']}' {$seltext}>";
     $elementstring .="{$mywholearray[$i]['desc']}</OPTION>";
}

$elementstring .="\n</SELECT>";
echo $elementstring;
	     
?>

</form>
</body>
</html>

Hi,I am trying to solve following php prob. from three days, but it fails.Please help me.

I have one php form with Name(textbox) and multivalue listbox with values mumbai, pune, nasik, surat, dhule. It work when user submits name and multiple cities as follows,

//insert.php

<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('state');
$name=$_POST['name'];
//$city=$_POST['city'];
$abc=$_POST['city'];
for ($i=0; $i<count($abc); $i++)
{
$city = $city." ".$abc[$i];
}
$submit=$_POST['submit'];
if(!empty($submit))
{
	
	$query=mysql_query("insert into city(name,city) values('$name','$city')");
	}

?>

Above code is working. But the problem is that, I want listbox on another page, this listbox should display the name of all cities, but should show all that cities which the selected while submitting the form with selected.

//show.php

<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('state');
$query=mysql_query("select city from city where id=7");
$row=mysql_fetch_array($query);
$one=$row["city"];
 
 $pieces = explode(" ", $one);

//echo $pieces[0]; // piece1

?>

<body>
<select name="city[]" size=5 multiple="multiple">
<option value="pune"<?php if ($pieces[1] == 'pune')
{ 
echo
'selected="selected"';
} ?>>pune</option>
<option value="mumbai"<?php if ($pieces[2] == 'mumbai') { echo
'selected="selected"'; } ?>>mumbai</option>
<option value="nasik"<?php if ($pieces[3] == 'nasik') { echo
'selected="selected"' ;} ?>>nasik</option>
<option value="surat"<?php if ($pieces[4] == 'surat') { echo
'selected="selected"'; } ?>>surat</option>
<option value="dhule"<?php if ($pieces[5] == 'dhule') { echo
'selected="selected"'; } ?>>dhule</option>

</select>                     
</body>

It doen't work.Could anyone tell me that what should I do?

<?  $cat=$city;  //fetch $city according to ur query.
$catrr=explode(",",$cat); //explode.
//echo $catrr;
?>
<select name="t_city[]" size="5" multiple="multiple" class="normal_text" id="t_class" style="width: 150px;">
			<?
			if(!$_SESSION[user])
			{
			?>
<option selected="selected" value="select">Select Class</option>
				<?
				}
				?>     
               
<? $cat_query=mysql_query("select * from city ");
while($cat_data=mysql_fetch_array($cat_query)) { 
?>
<option value="<?=$cat_data['city'] ?>"<?if(in_array($cat_data['city'],$catrr)) {?> selected="selected" <? } ?>><?=ucfirst($cat_data['city'])?></option>
				  <? } ?></select>

//insert.php
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('state');
$name=$_POST;
//$city=$_POST;
$abc=$_POST;
for ($i=0; $i<count($abc); $i++)
{
$city = $city." ".$abc[$i];
}
$submit=$_POST;
if(!empty($submit))
{

$query=mysql_query("insert into city(name,city) values('$name','$city')");
}

?>

Above code is working. But the problem is that, I want listbox on another page, this listbox should display the name of all cities, but should show all that cities which the selected while submitting the form with selected.

//show.php
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('state');
$query=mysql_query("select city from city where id=7");
$row=mysql_fetch_array($query);
$one=$row["city"];

$pieces = explode(" ", $one);

//echo $pieces[0]; // piece1

?>

<body>
<select name="city[]" size=5 multiple="multiple">
<option value="pune"<?php if ($pieces[1] == 'pune')
{
echo
'selected="selected"';
} ?>>pune</option>
<option value="mumbai"<?php if ($pieces[2] == 'mumbai') { echo
'selected="selected"'; } ?>>mumbai</option>
<option value="nasik"<?php if ($pieces[3] == 'nasik') { echo
'selected="selected"' ;} ?>>nasik</option>
<option value="surat"<?php if ($pieces[4] == 'surat') { echo
'selected="selected"'; } ?>>surat</option>
<option value="dhule"<?php if ($pieces[5] == 'dhule') { echo
'selected="selected"'; } ?>>dhule</option>

</select>
</body>

It doen't work.Could anyone tell me that what should I do?

use in_array like this

<?if(in_array(pune, $pieces)) {?> selected="selected" <? } ?>

use in_array like this

<?if(in_array(pune, $pieces)) {?> selected="selected" <? } ?>
<select name="t_city[]" size="5" multiple="multiple" id="t_class" style="width: 150px;">
			       <?
						if(!$_SESSION[user])
						{
						?>
                <option selected="selected" value="pune">mumbai</option>
				<?
				}
				?>     
 
				 <? $cat_query=mysql_query("select * from city ");
				  while($cat_data=mysql_fetch_array($cat_query)) { 
						?>
				   <option value="<?=$cat_data['city'] ?>"<?if(in_array(pune, $pieces)) { selected="selected" <? } ?></option>
                    <option value="<?=$cat_data['city'] ?>"<?if(in_array(mumbai, $pieces)) { selected="selected" <? } ?></option>
                     <option value="<?=$cat_data['city'] ?>"<?if(in_array(nasik, $pieces)) { selected="selected" <? } ?></option>
                      <option value="<?=$cat_data['city'] ?>"<?if(in_array(surat, $pieces)) { selected="selected" <? } ?></option>
                       <option value="<?=$cat_data['city'] ?>"<?if(in_array(dhule, $pieces)) { selected="selected" <? } ?></option>
				  <? } ?></select>

Am I doing right?
But it doesn't work.

hey,
wat you are doing ? totally you mixed code of pushpa.

I am totally confuse guys!
I don't have exact solution yet.
Please help me.:?:

hey,
wat you are doing ? totally you mixed code of pushpa.

<? 
$query=mysql_query("select city from city where id=7");
$row=mysql_fetch_array($query); 
$cat=$row[city];
$catrr=explode(",",$cat);
?>
 <select name="t_city[]" size="5" multiple="multiple" class="normal_text" id="t_class" style="width: 150px;">
<?
if(!$cat)
		{
?>
 <option selected="selected" value="select">Select city</option>
	<?
	}
	?>     
               
	<? $cat_query=mysql_query("select * from citynames"); //create citynames which you are using other wise use array
while($cat_data=mysql_fetch_array($cat_query)) { 
?>
				   <option value="<?=$cat_data['city'] ?>"<? if(in_array($cat_data['city'],$catrr)) {?> selected="selected" <? } ?>><?=ucfirst($cat_data['city'])?></option>
				  <? } ?></select>

hi ,
first store the cities in database separeted with cammas(",");
and check the multiful values are stored in database or not.

<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('state');
$name=$_POST['name'];
//$city=$_POST['city'];
$abc=$_POST['city'];
for ($i=0; $i<count($abc); $i++)
{
$city = $city.",".$abc[$i];
}
$submit=$_POST['submit'];
if(!empty($submit))
{

$query=mysql_query("insert into city(name,city) values('$name','$city')");
}

?>

and explode the cities with cammas(",");
check with echo's the values display or not.

<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('state');
$query=mysql_query("select city from city where id=7");
$row=mysql_fetch_array($query);
$one=$row["city"];

$pieces = explode(",", $one);

//echo $pieces[0]; // piece1

?>

<body>
<select name="city[]" size=5 multiple="multiple">
<option value="pune"<?php if ($pieces[1] == 'pune')
{
echo
'selected="selected"';
} ?>>pune</option>
<option value="mumbai"<?php if ($pieces[2] == 'mumbai') { echo
'selected="selected"'; } ?>>mumbai</option>
<option value="nasik"<?php if ($pieces[3] == 'nasik') { echo
'selected="selected"' ;} ?>>nasik</option>
<option value="surat"<?php if ($pieces[4] == 'surat') { echo
'selected="selected"'; } ?>>surat</option>
<option value="dhule"<?php if ($pieces[5] == 'dhule') { echo
'selected="selected"'; } ?>>dhule</option>

</select>
</body>

try like this.

I am not exploding $one and using our own code with minor changes. I am only changing condition and echoing only selected instead of selected="selected"

<body>
<select name="city[]" size=5 multiple="multiple">
<option value="pune"<?php if (substr_count($one, 'pune')  > 0  )
{ 
echo
'selected';
} ?>>pune</option>
<option value="mumbai"<?php if (substr_count($one, 'mumbai')  > 0) { echo
'selected'; } ?>>mumbai</option>
<option value="nasik"<?php if (substr_count($one, 'nasik')  > 0) { echo
'selected' ;} ?>>nasik</option>
<option value="surat"<?php if (substr_count($one, 'surat')  > 0) { echo
'selected'; } ?>>surat</option>
<option value="dhule"<?php if (substr_count($one, 'dhule')  > 0) { echo
'selected'; } ?>>dhule</option>

</select>                     
</body>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.