Page 1 of 1

Won't Return Value If Null

Posted: Sun Aug 24, 2008 9:15 am
by CoolAsCarlito
How do I make the most successful defenses and longest reign if they are null return and place in there that "No champion has been crowned" I thought I had it right as below with the rsort setup but I still get the following error message:

No champions have been crowned yet!
Warning: rsort() expects parameter 1 to be array, null given in /home/content/y/a/n/yankeefaninkc/html/titlehistory.php on line 86

Warning: rsort() expects parameter 1 to be array, null given in /home/content/y/a/n/yankeefaninkc/html/titlehistory.php on line 87

Code: Select all

 
<?php echo "<body bgcolor=\"black\" text=\"white\" link=\"red\" vlink=\"red\">"; ?>
 
<?php
function dateConvert($date)
{
    $eDat = explode("-",$date);
    $year = $eDat[0];
    $nMon = $eDat[1];
    $day  = $eDat[2];
    switch ($nMon)
    {
        case 1:     $mont = "January"; break;
        case 2:     $mont = "February"; break; 
        case 3:     $mont = "March"; break;
        case 4:     $mont = "April"; break;
        case 5:     $mont = "May"; break;
        case 6:     $mont = "June"; break;
        case 7:     $mont = "July"; break;
        case 8:     $mont = "August"; break;
        case 9:     $mont = "September"; break;
        case 10: $mont = "October"; break;
        case 11: $mont = "November"; break;
        case 12: $mont = "December"; break;    
    } 
    $out = $mont . " " . $day . ", " . $year;
    return $out;
}
?>
 
<?php
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());
 
if (isset($_GET['id']))
{    
    // First, we need to get the champs, so do some mysql stuffs.
    $qry0 = "SELECT * FROM `titlechamps` WHERE `title` = '".addslashes($_GET['id'])."';";
    $res0 = mysql_query($qry0) or die('Qry zero error!');
    if(!mysql_num_rows($res0)) echo "No champions have been crowned yet!";
    else
    {
        while($list0 = mysql_fetch_assoc($res0))
        {
            // We have at least one champ.
            // First, we have to do a little math. We need to determine 
            // how many days have elapsed between the date won and today.
            $today = explode("-",date("Y-m-d", time()));
            $yesterday = explode("-",$list0['date']);
            $hi = gregoriantojd($today[1],$today[2],$today[0]);
            $lo = gregoriantojd($yesterday[1],$yesterday[2],$yesterday[0]);
            $days = round($hi - $lo);
            
            // We will add the days and name to an array for the header.
            $longestR[] = $days . " - " . $list0['name'];
            // We also add the most successful defences to another array.
            $sucessfulD[] = $list0['defenses'] . " - " . $list0['name'];
            
            if($list0['news'] != null)
            $body .= "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tbody><tr><td colspan=\"2\" bgcolor=\"#e0e0e0\" valign=\"top\" width=\"100%\"><i>".$list0['news']."</i></td></tr></tbody></table>\n";
            
            $body .= 
            "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tbody>\n".
                "<tr>\n".
                "<td width=\"115\" valign=\"top\" align=\"center\"><img src=\"".$list0['pic']."\" /></td>".
                "<td>".
                "<table width=\"100%\" cellspacing=\"1\" cellpadding=\"2\" border=\"0\"><tbody><tr>".
                    "<td width=\"25\" bgcolor=\"#e0e0e0\" align=\"center\"><b>".$list0['num']."</b></td>".
                    "<td bgcolor=\"#e0e0e0\"><font color=\"#ff0000\">".$list0['name']."</font></td>".
                "</tr></tbody></table>".
                "<table width=\"100%\" cellspacing=\"1\" cellpadding=\"2\" border=\"0\"><tbody>".
                    "<tr><td align=\"center\" bgcolor=\"#e0e0e0\" width=\"50%\"><b>Successful Defenses</b></td><td align=\"center\" bgcolor=\"#e0e0e0\"><b>Event</b></td></tr>".
                    "<tr><td align=\"center\">".$list0['defenses']."</td><td align=\"center\">".$list0['event']."</td></tr>".
                    "<tr><td align=\"center\" bgcolor=\"#e0e0e0\"><b>Reign</b></td><td align=\"center\" bgcolor=\"#e0e0e0\"><b>Date Won</b></td></tr>".
                    "<tr><td align=\"center\">$days</td><td align=\"center\">".dateConvert($list0['date'])."</td></tr>".
                    "<tr><td colspan=\"2\"><i>".$list0['blurb']."</i></td></tr>".
                "</tbody></table>".
                "</td></tr>".
            "</tbody></table>\n";
            
        }
        
    }
    
    // Now, determine the longest reign and most defenses
    $longestReign = (rsort($longestR))?$longestR[0]:'No champion has been crowned';
    $successDefense = (rsort($sucessfulD))?$sucessfulD[0]:'No champion has been crowned';
 
 
//next, we need to assemble the header of the page.
    $query = "SELECT *, DATE_FORMAT(`datecreated`, '%M %e, %Y') AS datecreated FROM titlehistory WHERE id='".addslashes($_GET['id'])."';";
 
    if ($r = mysql_query ($query)){ // Run the query. 
        while ($row = mysql_fetch_array ($r)){
 
 
            $head .= '<table border=0 cellspacing="0" cellpadding=3 width=575>';
            $head .='<tr><td background="images/bg_bar4.gif" height=35>&nbsp;<font color="white">Title Histories</font></td></tr>';
            $head .='<tr><td></td></tr>';
            $head .='</table>';
            $head .='<table border=0 cellspacing="0" cellpadding=3 width=575>';
            $head .='<tr><td background="images/bg_bar3.gif" height=35 colspan=2>&nbsp;<font color=black>KOW '.$row['titlename'].'</font></td></tr>';
            $head .='<tr><td width=200><img src="/images/' . $row['titleimage'] . '" width=208px height=156px border=0 alt="View KOW '.$row['titlename'].' History"></td><td valign=top>';
            $head .='<table cellpadding="2" cellspacing="0" border="0" width=100%>';
            $head .='<tr><td align=center bgcolor=#E0E0E0><b><font color="#000000">Date Created</font></b></td></tr><tr><td align=center>'.$row['datecreated'].'</td></tr>';
            $head .='<tr><td bgcolor=#E0E0E0 align=center><b><font color="#000000">Status</font></b></td></tr>';
            $head .='<tr><td align=center>'.$row['status'].'</td></tr>';
            $head .='<tr><td bgcolor=#E0E0E0 align=center><b><font color="#000000">Longest Reign</font></b></td></tr>';
            $head .='<tr><td align=center>'.$longestR[0].'</td></tr>';
            $head .='<tr><td bgcolor=#E0E0E0 align=center><b><font color="#000000">Most Successful Defenses</font></b></td></tr>';
            $head .='<tr><td align=center>'.$sucessfulD[0].'</td></tr>';
            $head .='</table>';
        }
    }
}
    else
    {
        print '<table border=0 cellspacing="0" cellpadding=3 width=575>';
        print '<tr><td background="images/bg_bar4.gif" height=35>&nbsp;<font color="white">Title Histories</font></td></tr>';
        print '</table>';
        print '<table border=0 cellspacing="0" cellpadding=3 width=575>';
        print '<tr><td background="images/bg_bar3.gif" height=35 colspan=4>&nbsp;<font color="#000000">Active Titles</font></td></tr>';
        print '<tr>';
 
        //Define the query
        $query = "SELECT * FROM titles WHERE status = 'Active'"; // Active rows
 
        if ($r = mysql_query ($query)){ // Run the query. 
 
            while ($row = mysql_fetch_array ($r)){
    
                print '<td><a href="titlehistory.php?id=' . $row['id'] . '" title="View KOW '.$row['titlename'].' History">';
                print '<img src="/images/' . $row['titleimage'] . '" border=0 alt="View KOW '.$row['titlename'].' History" height="115px" width="115px"></a></td>';
             }
 
        } 
        else {
                die ('<p>Could not retrieve the data because <b>' . mysql_error() . '</b>. The query was $query.</p>');
        }//End of query IF 
 
        print '</tr>';
        print '</table>';
        print '<img src=images/spacer.gif><br>';
        print '<table border=0 cellspacing=0 cellpadding=3 width=575><tr><td background="images/bg_bar3.gif" height=35 colspan=2>&nbsp;<font color="#000000">Inactive Titles</font></td></tr>';
        print '<tr>';
 
        //Define the query
        $query = "SELECT * FROM titles WHERE status = 'Inactive'"; // Inactive Rows
 
        if ($r = mysql_query ($query)){ // Run the query. 
 
            while ($row = mysql_fetch_array ($r)){
    
                print '<td><a href="titlehistory.php?id=' . $row['id'] . '" title="View KOW '.$row['titlename'].' History">';
                print '<img src="/images/' . $row['titleimage'] . '" border=0 alt="View KOW '.$row['titlename'].' History" height="115px" width="115px"></a></td>';
    
            }
 
        } 
        else {
            die ('<p>Could not retrieve the data because <b>' . mysql_error() . '</b>. The query was $query.</p>');
        } //End of query IF 
    }
print '</tr>';
print '</table>'; 
 
?> 
 

Re: Won't Return Value If Null

Posted: Sun Aug 24, 2008 12:18 pm
by Corvin
Put $longestR = array() above your while loop (line 44) and then use count() instead of rsort().

Re: Won't Return Value If Null

Posted: Sun Aug 24, 2008 3:44 pm
by CoolAsCarlito
Tried it and no good. Okay what I'm wanting is it to show up like this for example:

http://epw.e-wrestling.org/titlehistories.php?id=1

I don't have of course any champions crowed yet so when it comes back from getting the rows and sees that there is not champs so the most successful defenses and longest reigns are void it would say "No champions crowned yet"

Re: Won't Return Value If Null

Posted: Sun Aug 24, 2008 7:32 pm
by Bill H
Notice you are getting the echo resulting from the !mysql_num_rows() call on line 41.
Your "else" that is run when results are found begins on line 42 and ends on line 84.
Then on lines 86 and 87 you rsort() arrays that were created in the else block that was never executed, which is what is triggering the messages.

Re: Won't Return Value If Null

Posted: Sun Aug 24, 2008 8:50 pm
by CoolAsCarlito
Okay thanks however what do I need to do to correct it because I'm clueless right now.

Re: Won't Return Value If Null

Posted: Sun Aug 24, 2008 8:55 pm
by Bill H
Well, expanding the "else" block to include the offending lines would jump into my mind.

Re: Won't Return Value If Null

Posted: Sun Aug 24, 2008 9:03 pm
by CoolAsCarlito
So what you mean is just moving down the } bracket?

Re: Won't Return Value If Null

Posted: Mon Aug 25, 2008 12:30 am
by Bill H
Do you know anything about programming in PHP?

Re: Won't Return Value If Null

Posted: Mon Aug 25, 2008 10:17 pm
by CoolAsCarlito
I've worked with this code for too long now and cant' figure out how to reorganize the if statement can someone help me.