如何在while循环中只显示一次

时间:2010-06-02 21:19:42

标签: php mysql

我运行了一个mysql查询并检查迭代器是否等于1,然后显示这个div标题......

if ($this->dliterator == 1) {echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";};

问题是,dl迭代器可能不一定从1开始(它与数据库中的downloadid直接相关)。

如何才能在循环 ONLY 中首次显示此内容?

while ($row = mysql_fetch_assoc($result)) { 

   if ($row['download'] != null){
    if ($this->dliterator == 1) {echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";};


    if ($editDownload == 1) {
     echo "<div class='clientlink' style='margin-top: 15px;'>";
     echo "<input name='downloads[$this->dliterator][name]' type='text' id='download$this->dliterator' value='" . $row['download'] . "'/>";
     echo "<input name='downloads[$this->dliterator][title]' type='text' id='downloadtitle$this->dliterator' value='" . $row['downloadtitle'] . "'/>";
     echo "<img class='removelink' src='/images/deletelink.png' width='15' />";
     echo "<input id='downloadid' name='downloads[$this->dliterator][id]' type='hidden' value='".$row['downloadid']."' style='display: none'/>";
     echo "<br/><img id='uploaddownload$uploaditerator' class='uploaddownload' src='../images/upload.png' width='80'/>"; 
     echo "</div>";


    };

   };

   $this->dliterator++;

   $uploaditerator++;
};

感谢所有答案!这是我的工作解决方案,感谢Zuul:

if ($row['download'] != null){
if (($this->dliterator != null ) && ($check ==0)) {echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";
$check++;};

4 个答案:

答案 0 :(得分:10)

试试这个:

$check=0;

if (($this->dliterator == 1) && ($check==0)) {
  echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";
  $check++;
};

如果您的ifdliterator==1未更改,则会输入$check声明... 在内部之后,它将更改$check,从而阻止if语句再次运行!

希望它有所帮助!

答案 1 :(得分:2)

在false $firstLineFetched = false;的循环中放置一个标记,如果为false,则将其设置为true并执行第一行处理

答案 2 :(得分:0)

$row = mysql_fetch_assoc($result);

if ($row && $row['download'] != null) {

    echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";
}

while ($row) {

    // ...

    $row = mysql_fetch_assoc($result);
}

答案 3 :(得分:0)

您可以将循环外的计数器设置为0并执行以下操作:

if($i == 0)
  printf("<div class=........>\n");

然后一定要在while()的末尾递增$ i($ i ++),然后通过循环它会直接通过printf()语句。