简体   繁体   中英

PHP For Loop only running once

I have a for loop to cycle through and array, run a database query in relation to each element, then call a function that prints out something in relation to it. The array is 12 elements long but the for loop never gets past element 0. It doesn't error or fail it just doesn't do anything after the first element. I verified that by putting the echo $x; and echo $vendorsname[$x]; at the start of each loop cycle and sure enough it only ever echo's 0 out to the page.

$continuetill = count($vendorsname);

for ($x = 0; $x < $continuetill; $x++)
{

echo $x;
echo $vendorsname[$x];

$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor = ".$x." ORDER BY id DESC LIMIT 1";

if ($result=mysqli_query($conn,$sql))
  {
  // Fetch one and one row
  while ($row=mysqli_fetch_row($result))
    {
      $low = $row[0];
      $mid = $row[1];
      $high = $row[2];
      $verlow = $row[3];
      $vermid = $row[4];
      $verhigh = $row[5];



      if(($low > $mid) && ($low > $high))
      {
        likely295Message($vendorsname[$x]);
      }
      elseif (($high > $low) && ($high > $mid) && ($high < 15))
      {
        possibly300Message($vendorsname[$x]);
      }
      elseif (($high > $low) && ($high > $mid) && ($high >= 15))
      {
        likely300Message($vendorsname[$x]);
      }
      elseif (($mid > $low) && ($mid > $high))
      {
        likely296Message($vendorsname[$x]);

      }else
      {
        unknownMessage($vendorsname[$x]);
      }


      if(($verlow != 0) || ($vermid != 0) || ($verhigh != 0))
      {
        if(($verlow > $vermid) && ($verlow > $verhigh))
        {
          verified295Message($vendorsname[$x]);
          changeBackgroundBack($vendorsname[$x]);
          changeImage($vendorsname[$x]);

        }
        elseif (($verhigh > $verlow) && ($verhigh > $vermid))
        {
          verified300($vendorsname[$x]);
          changeBackground($vendorsname[$x]);
          changeImage($vendorsname[$x]);
        }
        elseif (($vermid > $verlow) && ($vermid > $verhigh))
        {
          verified296($vendorsname[$x]);
          changeBackgroundBack($vendorsname[$x]);
          changeImage($vendorsname[$x]);
        }
      }

    }

  mysqli_free_result($result);
}
}

Make sure you have error displaying turned on. Add at the beginning of your script:

ini_set('display_errors', 1);

to make sure you don't have any errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM