Jump to content

Last Row Of Data Won't Print Out Using While Loop


Recommended Posts

Hi all,

This has just been bugging me forever, I actually just hardcoded the line it in years ago to just be done with it.  But when I use the while loop to pull up the data for this table, it prints out every row but the last one.  I've wracked my brain for years trying to fix it, and just cannot find the solution.  Below is the entire page code I'm using and you can see at the bottom what I have sadly hardcoded in to make the last row display.  That's the row that will not show up.  If you want to see the actual live page it's here:

https://www.lakerstats.com/numerical-roster/

The UNK line at the end if the one that never prints out that I hardcoded.

Page code as mentioned above. Appreciate any help!

[php]
$db = mysql_connect("localhost", "xxxx", "xxxx");

mysql_select_db("lakerstats",$db);
echo "<table class=numeric WIDTH=100% cellpadding=0 cellspacing=0 border=0>

<tr><td class=left colspan=\"2\" height=10><font size=\"2\" color=#000000>Current players are listed in ALL CAPS.<br />Retired uniforms are listed in <b><font color=\"#FF0000\">red</font></b>.<br /></font></td></tr><tr>";

$uniforms = "SELECT p.fname, p.lname, p.playerid, u.number, p.current_roster, u.retired, u.retired_player, p.profile_name, p.uniform_1, p.uniform_2 FROM player p, uniforms u WHERE u.number = p.uniform_1 or u.number = p.uniform_2 or u.number = p.uniform_3 or u.number = p.uniform_4 ORDER BY u.number+0, p.years, p.lname, p.fname";

$uniresult = mysql_query($uniforms) or die ("Query failed");
$numofrows = mysql_num_rows($uniresult);

//let's get the number of rows in our result so we can use it in a for loop

$temp = "0";
$sHTMLTemp = "";
while ($unirow = mysql_fetch_array($uniresult)) // Where $result is your result set
{
if ($temp != $unirow['number'])
  {
    $temp = $unirow['number'];
    if ($unirow['number'] == "100") {
    $unirow['number'] = "0";
    } else if ($unirow['number'] == "101") {
    $unirow['number'] = sprintf("%02d", 0);
    } else if ($unirow['number'] == "102") {
    $unirow['number'] = "UNK";
    } else {    
    $unirow['number'] = $unirow['number'];
    }

    $sHTMLTemp = preg_replace("/(.+),/","\\1",$sHTMLTemp);
    print $sHTMLTemp ;
    print "</tr>";

     $sHTMLTemp = "";


 if ($unirow[retired] == "yes") {
    echo "<tr><td class=left height=10><font size=\"2\" color=\"#FF0000\"><b>#" . $unirow[number] . "</b></font><font size=\"2\"> - ";
} else {
    echo "<tr><td class=left height=10><font size=\"2\" color=\"#000000\">#" . $unirow[number] . "</font><font size=\"2\"> - ";
}

}

 if ($unirow[current_roster] == "yes") {
$unirow[fname] = strtoupper($unirow[fname]);
$unirow[lname] = strtoupper($unirow[lname]);
} else {
$unirow[fname] = $unirow[fname];
$unirow[lname] = $unirow[lname];
}

 if ($unirow[retired_player] == $unirow[playerid]) {
$sHTMLTemp .= "<b><a href=\"/player-stats-bio/?pname=".$unirow['profile_name']."\"><font color=\"#FF0000\">" . $unirow['fname'] . " " . $unirow['lname'] . "</font></a></b>, ";
} else {
$sHTMLTemp .= "<a href=\"/player-stats-bio/?pname=".$unirow['profile_name']."\">" . $unirow['fname'] . " " . $unirow['lname'] . "</a>, ";
}

}


echo "<a href=\"/player-stats-bio/?pname=jim_fritsche\">Jim Fritsche</a>, <a href=\"/player-stats-bio/?pname=bobby_watson\">Bobby Watson</a>";

echo "</font></td></tr>";

echo "</font></td></tr></table>";

mysql_close($db);
[/php]

 

Edited by ban5150

Classic "Buffering" problem: 

Hugely simplifying your code, note what happens with/to the sHTMLTemp variable: 

$sHTMLTemp = "";
while ($unirow = mysql_fetch_array($uniresult)) // Where $result is your result set
{
  if ($temp != $unirow['number'])
  {
    $sHTMLTemp = preg_replace("/(.+),/","\\1",$sHTMLTemp);
    print $sHTMLTemp ;
    print "</tr>";

    $sHTMLTemp = "";
  }
  
  /* Lots of putting stuff into sHTMLTemp */ 

} /* -- END WHILE -- */ 

On change of "number", you print out the sHTMLTemp variable and clear it down, ready to be loaded with the next Player's data. 

When you get to the end of the loop, though, It's all set up with the last player's data ...   but you don't actually print it out!

Regards, 
   Phill  W.

 

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.