realjumper Posted July 12, 2006 Share Posted July 12, 2006 The code below is returning the results that I expect. It is inefficient though and I wonder if I can loop my way through this. I've had a couple of attempts but I'm not quite sure whether looping it will work. Is it possible to shorten this code with a loop do you think?Thanks [code] if ($used4 !== '') { if ($row3[test_sent_1] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_1='$used4'"; mysql_query($query); } elseif ($row3[test_sent_2] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_2='$used4'"; mysql_query($query); } elseif ($row3[test_sent_3] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_3='$used4'"; mysql_query($query); } elseif ($row3[test_sent_4] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_4='$used4'"; mysql_query($query); } elseif ($row3[test_sent_5] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_5='$used4'"; mysql_query($query); } elseif ($row3[test_sent_6] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_6='$used4'"; mysql_query($query); } elseif ($row3[test_sent_7] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_7='$used4'"; mysql_query($query); } elseif ($row3[test_sent_8] == '') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_8='$used4'"; mysql_query($query); } elseif ($row3[test_sent_9] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_9='$used4'"; mysql_query($query); } elseif ($row3[test_sent_10] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_10='$used4'"; mysql_query($query); } elseif ($row3[test_sent_11] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_11='$used4'"; mysql_query($query); } elseif ($row3[test_sent_12] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_12='$used4'"; mysql_query($query); } elseif ($row3[test_sent_13] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_13='$used4'"; mysql_query($query); } elseif ($row3[test_sent_14] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_14='$used4'"; mysql_query($query); } elseif ($row3[test_sent_15] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_15='$used4'"; mysql_query($query); } elseif ($row3[test_sent_16] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_16='$used4'"; mysql_query($query); } elseif ($row3[test_sent_17] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_17='$used4'"; mysql_query($query); } elseif ($row3[test_sent_18] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_18='$used4'"; mysql_query($query); } elseif ($row3[test_sent_19] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_19='$used4'"; mysql_query($query); } elseif ($row3[test_sent_20] == '0') { $query="UPDATE kanji.$username SET test_attempts=$row3[test_attempts] +'1', test_sent_20='$used4'"; mysql_query($query); }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14426-how-to-loop-this/ Share on other sites More sharing options...
kenrbnsn Posted July 12, 2006 Share Posted July 12, 2006 Try the following:[code]<?phpif($used4 != '') for ($i=1;$i<21;$i++) { if ($row3['test_sent_' . $i] == '0') { $tmp = $row3['test_attempts'] + 1; $q = "UPDATE kanji.$username SET test_attempts=$tmp, test_sent_" . $i . "='$used4'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); } }?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14426-how-to-loop-this/#findComment-57018 Share on other sites More sharing options...
realjumper Posted July 12, 2006 Author Share Posted July 12, 2006 Okay, thank you....I'll try that. I was going about it the wrong way it seems.Neil Quote Link to comment https://forums.phpfreaks.com/topic/14426-how-to-loop-this/#findComment-57023 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.