How to skip an element during a loop if the variable is empty?

I am trying to figure out how to skip printing a row from a MySQL table if the variable is empty. For example, I have a table full of information. I have a while loop that gives the result. How can I skip a record if the variable is empty?

For example, is it possible to cancel the echo if "tweet1" is empty in the line?

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 />";} 
+6
source share
5 answers

You can use the continue control structure to skip the iteration. Please read the docs

Example:

 if(!$row['tweet']) { continue; } 
+11
source
 if (empty($row['tweet'])) { continue; } 
+6
source

You also cannot return rows without information in tweet1 , this would make php checking for data in tweet1 unnecessary.

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

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 />"; } 
+1
source
 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 />"; } } 
0
source

Source: https://habr.com/ru/post/902430/


All Articles