PDA

View Full Version : Looping over a list


digiteyes
11-08-2005, 04:26 PM
Hi all,

This should be a quick and easy response i presume. I havent coded PHP in a long while so i am a bit rusty. I have a database field dev_list that contains a list of developer names. I want to loop over this field and output each of those developer names to the screen. I know how to loop over an array.. but i dont seem to be getting the same results when i try the same technique after retrieving dev_list from the database. Please help.

Brandoe85
11-08-2005, 05:15 PM
Hi,

Might be a few typos in here, but I believe this is what you're looking for:

<?php

$con = mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db("database",$con) or die(mysql_error());

$query = "select dev_list from table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
echo $row['dev_list'] . "<br>";
}

?>


Good luck;

digiteyes
11-08-2005, 05:36 PM
Thanks Brando, but that code doesnt work for me.. the value of dev_list in the database is 21,22,60,68 when i use ur code it prints it out exactly that way. I am trying to split that list into each individual number so it should print out

21
22
60
68

Hope this makes sense..Thanks

Brandoe85
11-08-2005, 05:51 PM
OK, after you read it into your variable, you can use explode() (http://us2.php.net/explode) to split the value off of the comma.

Good luck;

digiteyes
11-08-2005, 06:14 PM
Yupp.. I was just looking through the PHP manual online and came accross that function.. thanks for your help.