Jump to content


Photo

How to loop this?


  • Please log in to reply
2 replies to this topic

#1 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 12 July 2006 - 11:15 PM

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


    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);
       }



#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 July 2006 - 11:27 PM

Try the following:
<?php
if($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());
       }
    }
?>

Ken

#3 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 12 July 2006 - 11:34 PM

Okay, thank you....I'll try that. I was going about it the wrong way it seems.

Neil




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users