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
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 /> 
";
        }
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

 

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Hi  litebearer

 

I tried your solution but there seems to be syntax errors.

 

I replace the double quotes with single quotes and that did reduce the error but there is at lease one syntax error I am unable to fix.

 

Please double check

 

Thanks

 

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

<?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>";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.