jamjam Posted November 13, 2010 Share Posted November 13, 2010 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. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 13, 2010 Share Posted November 13, 2010 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 /> "; } } ?> Quote Link to comment Share on other sites More sharing options...
jamjam Posted November 13, 2010 Author Share Posted November 13, 2010 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 Quote Link to comment Share on other sites More sharing options...
litebearer Posted November 13, 2010 Share Posted November 13, 2010 please show the ENTIRE code you are trying sans passwords Quote Link to comment Share on other sites More sharing options...
jamjam Posted November 13, 2010 Author Share Posted November 13, 2010 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2010 Share Posted November 13, 2010 You only get one record from the database because you don't have mysql_fetch_array() within a while() loop. Quote Link to comment Share on other sites More sharing options...
jamjam Posted November 13, 2010 Author Share Posted November 13, 2010 That's what I am trying to do but I don't how to. Can you show me how achieve this? Quote Link to comment Share on other sites More sharing options...
litebearer Posted November 13, 2010 Share Posted November 13, 2010 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 Quote Link to comment Share on other sites More sharing options...
jamjam Posted November 13, 2010 Author Share Posted November 13, 2010 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 13, 2010 Share Posted November 13, 2010 please post syntax error. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2010 Share Posted November 13, 2010 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
litebearer Posted November 13, 2010 Share Posted November 13, 2010 Thanks, Pika Sometimes I type too slow/fast for my own good Quote Link to comment Share on other sites More sharing options...
jamjam Posted November 13, 2010 Author Share Posted November 13, 2010 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2010 Share Posted November 13, 2010 <?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>"; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2010 Share Posted November 13, 2010 Thanks, Pika Sometimes I type too slow/fast for my own good I know the feeling . . . Quote Link to comment Share on other sites More sharing options...
jamjam Posted November 13, 2010 Author Share Posted November 13, 2010 YES Finally this works. But the my original HTML formatting has been lost. So I have to figure how to but this back. Thanks for all your help guys. Quote Link to comment Share on other sites More sharing options...
jamjam Posted November 13, 2010 Author Share Posted November 13, 2010 YES Finally it worked. However the HTML formatting got lost along the way. So have to figure that out now. Thanks for all your help guys. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.