shyam13 Posted August 7, 2012 Share Posted August 7, 2012 How do I insert data which I retrieved from the database using a select statement and then using a for each loop insert the data into separate divs, so far I have used a select statement to get the data but I am struggling to use the for each loop to insert the data into separate divs. Any ideas anyone? Link to comment https://forums.phpfreaks.com/topic/266769-how-to-insert-data-into-a-div-using-a-foreach-statement/ Share on other sites More sharing options...
Psycho Posted August 7, 2012 Share Posted August 7, 2012 while($row = mysql_fetch_assoc($result)) { echo "<div>\n"; echo "{$row['field_name1']}<br>\n"; echo "{$row['field_name2']}<br>\n"; echo "{$row['field_name3']}<br>\n"; echo "</div>\n"; } Link to comment https://forums.phpfreaks.com/topic/266769-how-to-insert-data-into-a-div-using-a-foreach-statement/#findComment-1367489 Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 You need to use a while() loop. A foreach() loop is used to iterate through an array, and would only work if you only have one result to loop through. $query = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_assoc($query)) { echo '<div>' . $row['column'] . '</div>'; } Link to comment https://forums.phpfreaks.com/topic/266769-how-to-insert-data-into-a-div-using-a-foreach-statement/#findComment-1367490 Share on other sites More sharing options...
shyam13 Posted August 7, 2012 Author Share Posted August 7, 2012 If I use this method, Would it be able to create a new div for each element? Link to comment https://forums.phpfreaks.com/topic/266769-how-to-insert-data-into-a-div-using-a-foreach-statement/#findComment-1367548 Share on other sites More sharing options...
scootstah Posted August 7, 2012 Share Posted August 7, 2012 Why don't you try it and see for yourself? The best way to learn something in programming is by experimenting. Link to comment https://forums.phpfreaks.com/topic/266769-how-to-insert-data-into-a-div-using-a-foreach-statement/#findComment-1367550 Share on other sites More sharing options...
Psycho Posted August 7, 2012 Share Posted August 7, 2012 If I use this method, Would it be able to create a new div for each element? Did you even look at the code we provided? What we both provided was very, very simple logic. Link to comment https://forums.phpfreaks.com/topic/266769-how-to-insert-data-into-a-div-using-a-foreach-statement/#findComment-1367588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.