...

View Full Version : explode problem



Kal
03-06-2007, 12:52 PM
Hi guys

i am trying to explode a comma seperated list of IDs, however sometimes there may only be one ID.

once i have the ID(s) i need to be able to run a query and select the NAME related to the ID and echo them.

i have the following buyt doesn't seem to work.

any help would be great. thanks




if(!empty($row['bas']))
{
echo '<TR>';
echo '<TD class="subheading">BAS</TD>';
echo '<TD class="maintext">'

$bas = ($row["bas"]);
$bas_array = explode(",",$bas);
foreach ($bas_array as $key)
{
$sql3 = "SELECT bas,bas_name FROM tools.tools1 WHERE bas='$key'";
$rs3 = mysql_query ($sql3) or die (mysql_error());
if (mysql_num_rows($rs3)>0)
{
while($row=mysql_fetch_array($rs3))
{
echo $row['bas_name'];
}
}

}

'</TD>';
echo '</TR>';
}
else
{
'';
}

chump2877
03-06-2007, 01:28 PM
Can you be more specific about the problem you are having....

right off the bat I'm noticing that you have more than one array with the name $row....each $row array is probably referencing a different MysQL result source, so you will have to give them different names, i.e.: $row1 and $row2

aedrin
03-06-2007, 05:00 PM
If you're selecting multiple names by ID, it is better to do it like this (assuming $bas is a comma separated list of IDs):



$sql = "SELECT name FROM table WHERE id IN (" . $bas . ")";

$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
echo $row['name'];
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum