Jump to content

How to loop this?


realjumper

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/14426-how-to-loop-this/
Share on other sites

Try the following:
[code]<?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());
      }
    }
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/14426-how-to-loop-this/#findComment-57018
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.