Forum Moderators: coopster

Message Too Old, No Replies

explode problem

Having trouble trying to explode array

         

bbunny

5:19 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



Hi,

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

justgowithit

5:36 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



I'm not following you.

Why are you trying to explode data in order to echo it when you've already got an array from your query results?

bbunny

5:39 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



Sorry for the confusion. Basically I just want to be able to echo the array but I am unable to get anything other than the first value of the array.

hughie

6:21 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



I'm not with that either.

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?
?>

justgowithit

8:21 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



I think I see what you're after. You want to echo each row from db results, right?

While Loop [us2.php.net]

while ($row = mysql_fetch_assoc($Recordset1)){
echo $row['state'];
}

bbunny

8:38 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



Thanks in advance for all the help. Ill try and explain best I can.

I can "implode" fine. I get semicolon separated values in my table of states. 1;2;3;4 etc... These values are state id's related to another table. So the above example should return Alaska;Alabama;Arkansas;Arizona but....

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.

Tommybs

10:15 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



Hi,

Would it not be possible to do this without the explode but just with a foreach loop?

e.g
foreach($statesArray as $key=>$value) {
echo $value;
}

Sorry just noticed its the semi-colon not a comma so the above probably wouldnt work

[edited by: Tommybs at 10:19 pm (utc) on June 27, 2007]

Tommybs

10:01 am on Jun 28, 2007 (gmt 0)

10+ Year Member



Hi,

Just had another thought on this. Why don't you use array_push to insert the data into the array instead of implode, then you can use the foreach loop just to echo the array?

bbunny

5:08 pm on Jun 28, 2007 (gmt 0)

10+ Year Member



It appears as though the explode works fine, but it is the JOIN in the query that is not returning anything but the first value.

Ex:

Alasks, Alabama, Arkansas, Arizona in the field returns as

Alaska, , , ,

justgowithit

6:42 pm on Jun 28, 2007 (gmt 0)

10+ Year Member



What do you get if you reference the tables instead of using a left join?

like.....
SELECT * FROM customers, states WHERE customers.state_ID = states.state_ID