Web Hosting Talk







View Full Version : Using foreach and explode to select stuff from the DB


lexington
05-11-2008, 01:10 AM
Hello, I read the php tutorials but none of them really provided examples on how to do the following. I am trying to upgrade a badly written script from years back. The original author threw a bunch of IDs into one DB table row (I know that is bad but the way it is written it would take too long to undo that system). Anyway, he has a DB row named "radio_songs" and inside it contains the IDs of each song separated by semicolons. Example:

373;372;374;458;1862;1082;3048;9077;4916;4182;5488;2332;4743;2308;4358;2340;21519;4701;2702;2705;2131;1403;17957

I would like to break up each number into it's own row so I could make each one a link. I already have this code so could you please provide a working example?

$sql = "SELECT radio_songs FROM blah_radio WHERE radio_id = '$radio_id'";
$row = mysql_fetch_array(mysql_query($sql));

Thanks a lot :)

Jatinder
05-11-2008, 03:04 AM
Use this:

$songs = explode(';', $row[0]);
foreach($songs as $song) {
//do something with $song
}

lexington
05-11-2008, 06:01 AM
Thanks a lot that works :)