Php 为什么这个for循环不起作用(为什么最后一个索引没有显示值)

Php 为什么这个for循环不起作用(为什么最后一个索引没有显示值),php,mysql,sql,Php,Mysql,Sql,所以我设置了这个代码,在一张桌子上按过期日期显示食物条目,从上到下,从最早到最晚。如果在当前日期的三天内,文本将为黄色,如果在这之后的任何时间,文本将为红色 无论如何,我最近正在测试并意识到,如果两个条目具有相同的日期,按照我的代码设置方式,它将只显示其中一个条目,但过期日期在表中的次数(添加该日期的第一个条目是准确的) 所以我设置了一个for循环,使用一些查询来检查是否有多个条目,然后依次显示每个条目。问题是,我得到了循环的最终索引,而与该日期关联的名称将不会显示。代码如下: <?php

所以我设置了这个代码,在一张桌子上按过期日期显示食物条目,从上到下,从最早到最晚。如果在当前日期的三天内,文本将为黄色,如果在这之后的任何时间,文本将为红色

无论如何,我最近正在测试并意识到,如果两个条目具有相同的日期,按照我的代码设置方式,它将只显示其中一个条目,但过期日期在表中的次数(添加该日期的第一个条目是准确的)

所以我设置了一个for循环,使用一些查询来检查是否有多个条目,然后依次显示每个条目。问题是,我得到了循环的最终索引,而与该日期关联的名称将不会显示。代码如下:

<?php


      $sql1=mysql_query("SELECT email from loggedin WHERE session_id='$userid'");
      $sess=mysql_fetch_array($sql1);
      $newValue=$sess['email'];
      $sqlLength = mysql_query("SELECT * FROM food WHERE OwnerEmail='$newValue'"); 
      ?>


      <div class="table-responsive">
      <h2 class="sub-header">Expiration Dates</h2>

        <table class="table table-striped">
          <thead>
            <tr>
              <th>Food Name</th>
              <th>Brand Name</th>
              <th>Food Group</th>
              <th>Location</th>
              <th>Expiration Date</th>
            </tr>

          </thead>
          <tbody>
 <?php
    $i = 0;    
 while($test = mysql_fetch_array($sqlLength)){
    $dates[$i] = $test['ExpirationDate'];
    $i++;
 }
 $length=count($dates);

 for($x=0;$x<$length;$x++){
     $year[$x]=substr($dates[$x],6,4);
     $month[$x] = substr($dates[$x],3,2);
     $day[$x] = substr($dates[$x],0,2);
 }



for($x=0;$x<$length-1;$x++){
    for($y=$x+1;$y<$length;$y++){
        if($year[$x] > $year[$y]){
            $temp=$year[$x];
            $year[$x]= $year[$y];
            $year[$y]=$temp;

            $temp=$month[$x];
            $month[$x]= $month[$y];
            $month[$y]=$temp;

            $temp=$day[$x];
            $day[$x] = $day[$y];
            $day[$y] = $temp;
        }


        elseif($year[$x] == $year[$y]){
            if($month[$x] > $month[$y]){
                $temp=$month[$x];
                $month[$x]= $month[$y];
                $month[$y]=$temp;

                $temp=$day[$x];
                $day[$x] = $day[$y];
                $day[$y] = $temp;
            }

            elseif($month[$x] == $month[$y]){
                if($day[$x] > $day[$y]){
                    $temp=$day[$x];
                    $day[$x] = $day[$y];
                    $day[$y] = $temp;

                }

            }

        }

    }
}



for($x=0;$x<$length;$x++){
    $orderedDates[$x]= "$day[$x]-$month[$x]-$year[$x]"; 
}

$currentYear=date("Y");
$currentMonth=date("m");
$currentDay=date("d");


$multiple="false";
for($x=0;$x<$length;$x++){
    $class='';
 if($multiple=="false"){

    $sql2 = mysql_query("SELECT * FROM food WHERE OwnerEmail='$newValue' AND ExpirationDate='$orderedDates[$x]'") or die(mysql_error());
    $rows = mysql_fetch_array($sql2);
    if(count($rows) > 7){
        $y=1;
        $multiple="true";
        $count=count($rows)/7;
    }
 }

 else{
    $rows = mysql_fetch_array($sql2);
    $y++;

    //$x++;
 }



        $year=substr($rows['ExpirationDate'],6,4);
        $month=substr($rows['ExpirationDate'],3,2);
        $day=substr($rows['ExpirationDate'],0,2);

        if($year==$currentYear){
            if($month==$currentMonth){
                if(($day - $currentDay) <= 3){
                    $class='expBad';
                }
            }
        }

        if($currentYear > $year){
            $class='expGross';
        }

        elseif($currentYear == $year){
            if($currentMonth > $month){
                $class = 'expGross';
            }
            elseif($currentMonth == $month){
                if($currentDay > $day){
                    $class='expGross';
                }
            }
        }

?> 

            <tr>
              <td><b><?php echo $rows['Name'] ?></td>
              <td><b><?php echo $rows['Brand'] ?></td>
              <td><b><?php echo $rows['Type'] ?></td>
              <td><b><?php echo $rows['Container'] ?></td>
              <td><b><?php echo "<span class=\"$class\">".$rows['ExpirationDate']."</span>" ?></td>
            </tr>

             <?php

             if($y == $count){

                 $multiple="false";

             }
    }
    /*
    $er=mysql_query("SELECT Name FROM food WHERE ExpirationDate='$orderedDates[3]'");
    $der = mysql_fetch_array($er);
    $mer=$der['Name'];
    echo $mer;
    */
?>

         </tbody>
        </table>
我只得到前3个条目,然后第4行是空的

其他两列是id和OwnerEmail

结果是count()函数返回的数字与我想象的不同

也就是说,我认为$count对于具有唯一过期日期的值等于1。 但是它是2,这导致它比我想要的多运行一次mysql_fetch_数组函数,使其指向空值

相反,我使用以下代码来获取具有相同日期的食品的数量:

$sql2 = mysql_query("SELECT Name FROM food WHERE OwnerEmail='$emailname' AND ExpirationDate='$orderedDates[$x]'") or die(mysql_error());
    $count2=0;
    while($rows2 = mysql_fetch_array($sql2)){
    $count2++;
    }
$sql2 = mysql_query("SELECT Name FROM food WHERE OwnerEmail='$emailname' AND ExpirationDate='$orderedDates[$x]'") or die(mysql_error());
    $count2=0;
    while($rows2 = mysql_fetch_array($sql2)){
    $count2++;
    }