Here is my code:-
//choose the table and return records
$result = mysql_query("SELECT id, state,country_id FROM state ORDER BY country_id");
$string .= '"'.$val.'", ';
$country = null;
//get group
while($row = mysql_fetch_assoc($result)) {
if ($row['country_id'] != $country) {
echo '<br>'.'<br>'."[".($row['country_id'])."] = parentarray [";
$country = $row['country_id'];
}
// get list
echo "'".$row['id']."|".
$row['state']."',";
}
//end of group
$db = null;
echo "<br>Fetched data successfully\n<br>";
mysql_close($conn);
?>
This creates an output like so:-
[2] = parentarray ['136|Balkhs','481|Herat','564|Kabol','979|Qandahar',
[3] = parentarray ['167|Benguela','503|Huambo','705|Luanda','823|Namibe',
[5] = parentarray ['1208|Tirana',
Which is fine is almost what I want, but I am having trouble finishing off each line I want to remove the last comma and replace it with a closed bracket like so:-
[2] = parentarray ['136|Balkhs','481|Herat','564|Kabol','979|Qandahar']
[3] = parentarray ['167|Benguela','503|Huambo','705|Luanda','823|Namibe']
[5] = parentarray ['1208|Tirana']
Placing an echo "]";
anywhere after the //get list region results in
['136|Balkhs'],'481|Herat'],'564|Kabol'],'979|Qandahar']
and that is not what I want. I need to remove the trailing comma and replace it with the bracket for the last item returned on that line. I've been googling for days but haven't really seen any examples that apply to what I am trying to achieve, so any pointers would really be helpful
Thanks guys