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? Quote Link to comment 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"; } Quote Link to comment 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>'; } Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.