首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个for循环不工作(为什么最后一个索引没有显示值)

问为什么这个for循环不工作(为什么最后一个索引没有显示值)
EN

Stack Overflow用户
提问于 2014-04-07 06:52:54
回答 1查看 71关注 0票数 0

因此,我将此代码设置为在表中按过期日期显示食品条目,从上到下,从最早到最晚。如果他们在当前日期的三天内,文本将是黄色的,如果是在这之后的任何时间,文本将是红色的。

无论如何,我最近正在测试并意识到,如果两个条目具有相同的日期,就像我的代码设置一样,无论过期日期在表中出现多少次,它都将仅显示其中一个条目(确切地说,添加了该日期的第一个条目)。

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

代码语言:javascript
复制
<?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>

一些信息需要知道,我的食物表有7列。

orderedDates给了我一个排序正确的数组(我知道有更好的方法可以做到这一点,但是当我写这个的时候,我刚刚开始使用php )

有4个食物条目。

这就是问题所在,当我得到$x = 3时,由于某种原因,它没有显示该条目的任何列值。

下面是表中的条目应该是什么样子。

代码语言:javascript
复制
what    s   Grains  Pantry  12-02-2012
why     s   Grains  Pantry  12-02-2012
who     s   Grains  Pantry  08-04-2014
where   s   Grains  Pantry  10-04-2014

我只得到了前3个条目,然后第4行是空的。

另外两列是id和OwnerEmail

EN

回答 1

Stack Overflow用户

发布于 2014-04-07 10:43:49

结果发现count()函数返回的数字和我想的不一样。

也就是说,对于具有唯一截止日期的值,我认为$count应该等于1。但是它是2,这导致它比我想要的多运行一次mysql_fetch_array函数,使它指向一个空值;

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

代码语言:javascript
复制
$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++;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22901011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档