Jump to content

PHP if else and while loop


jamjam

Recommended Posts

I want to control the output of php code with if else statement like this.

 

 

 

<?php if($row_director['director'] == "") 
{echo "";} 
else {echo " 
<table> 
<th> 
<h4>Directed by</h4> 
</th> 
<td> 
<h5> 
$row_director['director'] 
</h5> 
<br /> 
</td> 
</table> 
<br /> 
";}?>

 

 

 

I also want to loop through the results contained in $row_director['director']

 

Having tried various ways to achieve this, I have now given up.

 

 

Can someone please help me.

Link to comment
https://forums.phpfreaks.com/topic/218579-php-if-else-and-while-loop/
Share on other sites

I assume row_director is one row from the mysql query result $sql_result

 

<?php
while ($row_director = mysql_fetch_assoc($sql_result)) {
       if($row_director['director'] == "") {
              echo "";
       } else {
              echo " 
<table> 
<th> 
<h4>Directed by</h4> 
</th> 
<td> 
<h5> 
{$row_director['director']}
</h5> 
<br /> 
</td> 
</table> 
<br /> 
";
        }
}
?>

Hello

 

Thanks for the quick reply.

 

Your solution looks good but it don't work.

 

I want to echo all the results in {$row_director['director']}.

 

But this is only one result showing in the browser.

 

The database table contains two rows and I wanted to output both to show the while is working.

 

Thanks again

 

 

If your referend to the database connection etc litebearer

 

here it is

 

mysql_select_db($database_main, $main);
$query_director = "SELECT information.title, director FROM information INNER JOIN director ON information.title = director.title";
$director = mysql_query($query_director, $main) or die(mysql_error());
$row_director = mysql_fetch_assoc($director);
$totalRows_director = mysql_num_rows($director);

 

HTML

 

<?php if($row_director['director'] == "") 
{echo "";} 
else {echo " 
<table> 
<th> 
<h4>Directed by</h4> 
</th> 
<td> 
<h5> 
$row_director['director'] 
</h5> 
<br /> 
</td> 
</table> 
<br /> 
";}?>

 

 

 

I can echo all results contained in the $director['director'] quite easily,  the problem is that I want place these results inside an if else statment.

 

This way if $director['director'] is empty then the html is not displayed.

 

 

Hope that makes sense

 

Perhaps...

 

mysql_select_db($database_main, $main);
$query_director = "SELECT information.title, director FROM information INNER JOIN director ON information.title = director.title";
$director = mysql_query($query_director, $main) or die(mysql_error());
$totalRows_director = mysql_num_rows($director);
if($totalRows>0) {
echo "<table>
while($row_director = mysql_fetch_array($director)) {
	if($row_director['director'] == "") {
		?>
		echo "<tr><td>" .  "Directed by: </td><td>" . $row_director['director'] . "</td></tr>";
	}
}
echo "</table>";
}

made a small modification

Edited to fix 'indentation malfunction' . . .

<?php
mysql_select_db($database_main, $main);
$query_director = "SELECT information.title, director FROM information INNER JOIN director ON information.title = director.title";
$director = mysql_query($query_director, $main) or die(mysql_error());
$totalRows_director = mysql_num_rows($director);
if($totalRows>0) {
    echo "<table>";
    while($row_director = mysql_fetch_array($director)) {
        if($row_director['director'] == "") {
            echo "<tr><td>" .
            "Directed by: </td><td>" . $row_director['director'] . "</td></tr>";
        }
    }
    echo "</table>";
}
?>

Hi Pikachu2000

 

When I tried your solution I got this error

 

Notice: Undefined variable: totalRows in F:\wamp\www\includes\info\credits.php on line 6

 

I think I fixed by changing totalRows to totalRows_director

 

Having done this, I get blank results with this code. Even though the director field contains at least 2 rows of data.

 

<?php
mysql_select_db($database_main, $main);
$query_director = "SELECT information.title, director FROM information INNER JOIN director ON information.title = director.title";
$director = mysql_query($query_director, $main) or die(mysql_error());
if( mysql_num_rows($director) > 0 ) {
echo "<table>";
while($row_director = mysql_fetch_array($director)) {
	echo empty($row_director['director']) ? '' : "<tr><td>Directed by: </td><td>{$row_director['director']}</td></tr>\n";
	}
echo "</table>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.