简体   繁体   English

如果变量为空,如何跳过while循环中的项?

[英]How do I skip an item in while loop if variable empty?

I'm trying to figure out how to skip printing a row from a MySQL table if the variable is empty. 我试图弄清楚如果变量为空,如何跳过从MySQL表中打印一行。 For instance, I have a table full of information. 例如,我有一张充满信息的表格。 I have a while loop that echos the result. 我有一个while循环,回应结果。 How can I skip an entry if a variable is empty? 如果变量为空,如何跳过条目?

For instance, can I cancel the echo if 'tweet1' is empty in the row? 例如,如果'tweet1'在行中为空,我可以取消回声吗?

mysql_connect ($DBsever, $DBusername, $DBpass) or die ('I cannot connect to the database becasue: '.mysql_error());
mysql_select_db ("$DBname");

$query = mysql_query("SELECT * FROM $DBtable ORDER BY time");

while ($row = mysql_fetch_array($query)) {
    echo "<br /><strong>".$row['time']." ".$row['headline']."</strong><br/>".$row['description']."<br />".$row['story1']." <a href=".$row['link1']." target='_blank'>".$row['link1']."</a> ".$row['tweet1']."<br />";}

You can use continue control structure for skip an iteration. 您可以使用continue控制结构来跳过迭代。 Please read the docs 请阅读文档

Example: 例:

if(!$row['tweet']) {
  continue;
}
if (empty($row['tweet'])) {
    continue;
}

你也可能不会在tweet1没有信息的情况下返回行,这会使php检查tweet1中的数据变得不必要。

$query = mysql_query("SELECT * FROM $DBtable WHERE tweet1 IS NOT NULL ORDER BY time");

Try: 尝试:

while ($row = mysql_fetch_array($query)) {
if ($row['tweet1']) 
    echo "<br /><strong>".$row['time']." ".$row['headline']."</strong><br/>".$row['description']."<br />".$row['story1']." <a href=".$row['link1']." target='_blank'>".$row['link1']."</a> ".$row['tweet1']."<br />";
}
while ($row = mysql_fetch_array($query)) {
  if (!empty($row['tweet1'])    {
    echo "<br /><strong>".$row['time']." ".$row['headline']."</strong><br/>".$row['description']."<br />".$row['story1']." <a href=".$row['link1']." target='_blank'>".$row['link1']."</a> ".$row['tweet1']."<br />";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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