Forum Moderators: coopster
I can successfully implode states in my table shown by a section of the following code but I am having trouble exploding and echo the values back.
GetSQLvalueString((is_array($_POST['state']))? implode(';',$_POST['state']):$_POST['state'], "text"));
mysql_select_db($database_employees, $employees);
$Result1 = mysql_query($insertSQL, $employees) or die(mysql_error());
}
mysql_select_db($database_employees, $employees);
$query_Recordset1 = "SELECT * FROM customers LEFT JOIN states ON customers.state_ID = states.state_ID ";
$Recordset1 = mysql_query($query_Recordset1, $employees) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
The below code of course only echoes back the first value of the array:
<?php echo $row_Recordset1['state'];?>
If any more code is needed I can post, I just didnt want to put too much.
Thanks
So you've got an array of states coming from a post?
I assume that's a select list
<select name="states[]">
<option value=1>State 1</option>
<option value=2>State 2</option>
</select>
And you want to implode the array to store as a text field?
<?php
if (isset($_POST['states'])
{
$states_imploded=implode(",",$_POST['states']);
}
?>
then add to db
<?php
mysql_query("INSERT INTO table SET States='".$states_imploded."');
?>
Then you want to fetch all the states stored in the db
<?php
$result=mysql_query("SELECT * FROM table ");
while($row=mysql_fetch_array($result))
{
$statesArray=explode(";",$row['States']);
foreach($statesArray as $value)
{
echo $value.'<br>';
}
}
any good?
?>
While Loop [us2.php.net]
while ($row = mysql_fetch_assoc($Recordset1)){
echo $row['state'];
}
it will not "fetch" any value other than the first one which in this example would be Alaska
This is the current code which is similar as suggested previously but it still only "fetches" the first value in the array.
<?php $statesArray = explode(';',$row_Recordset1['state']); foreach($statesArray as $value) { echo $value.'<br>';
}?>
I already have the while loop.