Jump to content

Adding Else To If Statement


Xtremer360

Recommended Posts

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>';
	}
}

Link to comment
https://forums.phpfreaks.com/topic/144364-adding-else-to-if-statement/
Share on other sites

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.';
}
}

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.';
}

 

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:

 

help.jpg

 

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>';
}

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.