unknown87 Posted October 25, 2008 Share Posted October 25, 2008 for($i = 0; $i < 100; $i++) { $create = rand(1,100); if ($create <= $val1) { $object= "obj1"; } if ($create >= $val2 && $create <= $val3) { $object= "obj2"; } if ($create >= $val4 && $create <= $val5) { $object= "obj3"; } if ($create >= $val6 && $create <= $val7) { $object= "obj4"; } if ($create >= $val8 && $create <= $val9) { $object= "obj5"; } if ($create >= $val10 && $create <= $val11) { $object= "obj6"; } if ($create >= $val12) { $object= "obj7"; } $query = "INSERT INTO objects (items) VALUES ('$object')"; $result = mysql_query($query, $connection); if (!$result) { die("Database query failed: " . mysql_error()); } } How do i make the loop stop when lets say obj7 is outputed 10 times? Thanks in advance Link to comment https://forums.phpfreaks.com/topic/130042-forloops/ Share on other sites More sharing options...
Barand Posted October 25, 2008 Share Posted October 25, 2008 $val = array(1=>10,11,20,21,30,31,40,41,60,61,80,81); for($i = 0, $k7 = 1; $i < 100, $k7 < 10; $i++) { $create = rand(1,100); if ($create <= $val[1]) { $object= "obj1"; } if ($create >= $val[2] && $create <= $val[3]) { $object= "obj2"; } if ($create >= $val[4] && $create <= $val[5]) { $object= "obj3"; } if ($create >= $val[6] && $create <= $val[7]) { $object= "obj4"; } if ($create >= $val[8] && $create <= $val[9]) { $object= "obj5"; } if ($create >= $val[10] && $create <= $val[11]) { $object= "obj6"; } if ($create >= $val[12]) { $object= "obj7"; $k7++; } $query = "INSERT INTO objects (items) VALUES ('$object')"; $result = mysql_query($query, $connection); if (!$result) { die("Database query failed: " . mysql_error()); } } Link to comment https://forums.phpfreaks.com/topic/130042-forloops/#findComment-674251 Share on other sites More sharing options...
unknown87 Posted October 25, 2008 Author Share Posted October 25, 2008 Great - Thank you Link to comment https://forums.phpfreaks.com/topic/130042-forloops/#findComment-674281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.