Xtremer360 Posted February 8, 2009 Share Posted February 8, 2009 What I want to do is add a part to the if statement that if there isn't a record present in the database then it displays "There are no open weekly events.". How do I do this? $query = "SELECT * FROM events ORDER BY `showname`"; $result = mysql_query ( $query ); // Run The Query if ($result) { // Fetch and print all records. $i = 0; while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) { $sClass = 'row2'; if ($i ++ & 1) { $sClass = 'row1'; } printf ( "<tr class=\"%s\">", $sClass ); print "<td valign=\"top\" align=\"center\" width=\"30\"><a href=\"#\" onclick=\"editbooker('editbooker', 'content' , '". $row ['showname'] ."'); return false;\">Edit</a></td>"; printf ( "<td valign=\"top\">%s</td>", $row [showname] ); printf ( "<td align=\"center\" width=\"80\">%s</td>", $row [airs] ); printf ( "<td align=\"center\" width=\"50\">%s</td>", $row [matches] ); printf ( "<td align=\"center\" width=\"100\">%s</td>", $row [status] ); print '</tr>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/144364-adding-else-to-if-statement/ Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 $result = mysql_query ( $query ); // Run The Query $rows = mysql_num_rows($result); if ($rows > 0) { }else { echo 'No rows to display.'; } Quote Link to comment https://forums.phpfreaks.com/topic/144364-adding-else-to-if-statement/#findComment-757549 Share on other sites More sharing options...
Xtremer360 Posted February 8, 2009 Author Share Posted February 8, 2009 so it should be: $query = "SELECT * FROM events WHERE `type` = 'Pay Per View' ORDER BY `showname`"; $result = mysql_query ( $query ); // Run The Query if ($result) { // Fetch and print all records. $i = 0; while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) { $sClass = 'row2'; if ($i ++ & 1) { $sClass = 'row1'; } printf ( "<tr class=\"%s\">", $sClass ); print "<td valign=\"top\" align=\"center\" width=\"30\"><a href=\"#\" onclick=\"editbooker('editbooker', 'content' , '". $row ['showname'] ."'); return false;\">Edit</a></td>"; printf ( "<td valign=\"top\">%s</td>", $row [showname] ); printf ( "<td align=\"center\" width=\"80\">%s</td>", $row [airs] ); printf ( "<td align=\"center\" width=\"50\">%s</td>", $row [matches] ); printf ( "<td align=\"center\" width=\"100\">%s</td>", $row [status] ); print '</tr>'; } if ($rows > 0) { }else { echo 'No rows to display.'; } } Quote Link to comment https://forums.phpfreaks.com/topic/144364-adding-else-to-if-statement/#findComment-757553 Share on other sites More sharing options...
premiso Posted February 8, 2009 Share Posted February 8, 2009 so it should be: umm no...did you not read how I wrote it? $query = "SELECT * FROM events WHERE `type` = 'Pay Per View' ORDER BY `showname`"; $result = mysql_query ( $query ); // Run The Query $rows = mysql_num_rows($result); if ($rows > 0) { // Fetch and print all records. $i = 0; while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) { $sClass = 'row2'; if ($i ++ & 1) { $sClass = 'row1'; } printf ( "<tr class=\"%s\">", $sClass ); print "<td valign=\"top\" align=\"center\" width=\"30\"><a href=\"#\" onclick=\"editbooker('editbooker', 'content' , '". $row ['showname'] ."'); return false;\">Edit</a></td>"; printf ( "<td valign=\"top\">%s</td>", $row [showname] ); printf ( "<td align=\"center\" width=\"80\">%s</td>", $row [airs] ); printf ( "<td align=\"center\" width=\"50\">%s</td>", $row [matches] ); printf ( "<td align=\"center\" width=\"100\">%s</td>", $row [status] ); print '</tr>'; } }else { echo 'No rows to display.'; } Quote Link to comment https://forums.phpfreaks.com/topic/144364-adding-else-to-if-statement/#findComment-757561 Share on other sites More sharing options...
Xtremer360 Posted February 8, 2009 Author Share Posted February 8, 2009 This is what it should be and I have also included the full function when it displays the message if there is no rows then there shouldn't even be a table either: function booker() { print '<script language="JavaScript" src="ts_picker.js"></script>'; print '<h1 class="backstage">Event Booking Management</h1><br />'; print "<h2 class=\"backstage\">Weekly Events :: <a href=\"#\" onclick=\"ajaxpage('booklineup', 'content'); return false;\">Book Lineup</a></h2>"; print '<br />There are no open weekly events.<br /><br />'; print "<h2 class=\"backstage\">PPV Events :: <a href=\"#\" onclick=\"ajaxpage('booklineup', 'content'); return false;\">Book Lineup</a></h2><br />"; print '<table width="100%" class="table1">'; print '<tr class="rowheading">'; print '<td align="center" border="0" width="1"> </td>'; print '<td>Show Name</td>'; print '<td width="80" align="center">Airs</td>'; print '<td width="50" align="center">Matches</td>'; print '<td width="100" align="center">Status</td>'; print '</tr>'; $query = "SELECT * FROM events WHERE `type` = 'Pay Per View' ORDER BY `showname`"; $result = mysql_query ( $query ); // Run The Query $rows = mysql_num_rows($result); if ($rows > 0) { // Fetch and print all records. $i = 0; while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) { $sClass = 'row2'; if ($i ++ & 1) { $sClass = 'row1'; } printf ( "<tr class=\"%s\">", $sClass ); print "<td valign=\"top\" align=\"center\" width=\"30\"><a href=\"#\" onclick=\"editbooker('editbooker', 'content' , '". $row ['showname'] ."'); return false;\">Edit</a></td>"; printf ( "<td valign=\"top\">%s</td>", $row [showname] ); printf ( "<td align=\"center\" width=\"80\">%s</td>", $row [airs] ); printf ( "<td align=\"center\" width=\"50\">%s</td>", $row [matches] ); printf ( "<td align=\"center\" width=\"100\">%s</td>", $row [status] ); print '</tr>'; } }else { print '<br /><span>There are no open weekly events.</span><br /><br />'; } print '</table><br />'; print '<h2 class="backstage"><input type="button" value="Return to Main Menu" class="button200"></form></h2>'; } Quote Link to comment https://forums.phpfreaks.com/topic/144364-adding-else-to-if-statement/#findComment-757573 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.