Eliasen Posted March 12, 2011 Share Posted March 12, 2011 Hi Everyone, I have this piece of code: $getinfo = mysql_query("SELECT * FROM animals WHERE type='reptile'") or die(mysql_error()); $numRowsAge = mysql_num_rows($getinfo); $age = mysql_fetch_assoc($getinfo); while($age) { foreach(???????){ $date1 = $age['birthdate']; $date2 = time(); $seconds_between = $date2 - $date1; $minutes_between = $seconds_between/60; $hours_between = $minutes_between/60; $days_between = $hours_between/24; if ($hours_between >= 15 && $age['lifecycle_stage'] == "egg"){ $animal = array(); $animal['lifecycle_stage'] = "Baby"; $animal['age_minutes'] = round($minutes_between,1); $animal['age_hours'] = round($hours_between,1); $animal['age_days'] = round($days_between,1); $ins = sql_update('items', $animal, $age['id']); mysql_query($ins); } elseif ($days_between >= 7 && $age['lifecycle_stage'] == "Baby"){ $animal = array(); $animal['lifecycle_stage'] = "Adult"; $animal['age_minutes'] = round($minutes_between,1); $animal['age_hours'] = round($hours_between,1); $animal['age_days'] = round($days_between,1); $ins = sql_update('items', $animal, $age['id']); mysql_query($ins); } elseif ($days_between >= $age['max_age']){ $del="DELETE FROM items WHERE id='$age[id]'"; $died = mysql_query($del); } else { $animal = array(); $animal['age_minutes'] = round($minutes_between,1); $animal['age_hours'] = round($hours_between,1); $animal['age_days'] = round($days_between,1); $ins = sql_update('items', $animal, $age['id']); mysql_query($ins); } } } I want to calculate the animals age, and put it into the database (Do not mind the sql_update script, it is custom and it works). Right now it only worked with the first row in the database, when I used: for($count = 1; $count <= $numRowsSnakes; $count++) { instead of the while and foreach. How do I run this for each of the animals in the database? I believe the problem is at the top bit (the while and foreach), but I couldn't find a solution or a way of doing this. The foreach has just been left as ?????, as I have no idea of what to do here. Anyone able to enlighten me on how to use the while/foreach to solve my problem? Thanks!! Link to comment https://forums.phpfreaks.com/topic/230446-how-to-update-each-row/ Share on other sites More sharing options...
flolam Posted March 12, 2011 Share Posted March 12, 2011 you don't need a foreach. just remove this line: $age = mysql_fetch_assoc($getinfo); and change the while statement to this: while ($age = mysql_fetch_assoc($getinfo)) { Link to comment https://forums.phpfreaks.com/topic/230446-how-to-update-each-row/#findComment-1186732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.