Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.