Jump to content

Not sure how to position these tables


adamgonge
Go to solution Solved by Psycho,

Recommended Posts

Hi! I wrote this script that takes a nflstats.csv file reads it and then does some stuff to show the player name  and corresponding pass rating. when I put it into tables the tables show up really far down the page and Im not sure why. If I remove the tables the info is displayed at the top but for some reason the tables move it down drastically. Here is my code:

<?php
// this script reads data from a text file

// open file and read data
 $fp=fopen("nflstats.csv", "rw");
if (!$fp){
        echo "Could not open the file!";
exit();
}
//read the first line
$line1 = fgets($fp, 256);

 displayData($fp);

function displayData($fp){

$ratings = array();


while(!feof($fp)){ // as long as it is not the end of the file.
        //read one line at a time
        $line= fgets($fp, 256 );
        $total= 0;
       //Split the data using explode() function
    if($line !=""){
       $info=explode(",", $line);
   $player_name = $info[0];


        $c_value = ($info[2]*100/$info[3] - 30)/20;
        $y_value = ($info[4]/$info[3] - 3)/4;
        $t_value = $info[5]*20/$info[3];
        $i_value = 2.375 - $info[6]*35/$info[3];
        $pass_rating = ($c_value + $y_value + $t_value + $i_value)*100/6;
        $ratings[$player_name]= $pass_rating;

}}


 echo "<h2>"."NFL Player Pass Ratings"."</h2>";
 arsort($ratings);
 echo"<table>";

   foreach ($ratings as $player_name=>$pass_rating){
   echo "<tr><td>Name: </td><td>".$player_name."</td><td> Pass Rating: </td><td>".round($pass_rating,2)."</td></tr><br/>";
   }
 echo"</table>";
 echo "<h2>"."NFL Player Pass Ratings: Great, Good, Mediocre"."</h2><br/>";
  arsort($ratings);
   echo"<table>";
   foreach ($ratings as $player_name=>$pass_rating){

        // display name and team
   if($pass_rating > 85&& $pass_rating <=90)
   echo "<tr><td>Name: </td><td>".$player_name."</td><td>Pass Rating: </td><td>".round($pass_rating, 2)."<b> --Mediocre</b></td></tr><br/>";
   elseif ($pass_rating > 90 && $pass_rating <=95)
   echo "<tr><td>Name: </td><td> ".$player_name."</td><td> Pass Rating: </td><td>".round($pass_rating, 2)."<b> --Good</b></td><td><br/>";
        elseif($pass_rating > 95)
        echo "<tr><td>Name: </td><td>".$player_name."</td><td> Pass Rating: </td><td>".round($pass_rating, 2)."<b> --Great</b></td></tr><br/>";        
}
         echo"</table>";
}
?>

Link to comment
Share on other sites

Make absolutely sure you have the <tr>, <td>, </td>, and </tr> sequence correct.

 

Also, browsers know to start another row when the sequence </tr><tr> is found.

 

A few browsers (some versions of IE in particular) will collect all extraneous HTML that should not be between the table parts close/open tag sequences, and show them above the table. Thus, all the <br/> tags, being outside of cells, will be collected and 'displayed' above the table.

Link to comment
Share on other sites

  • Solution

<?php
 
function displayData($fp)
{
    $ratings = array();
    while(!feof($fp))
    {
        // as long as it is not the end of the file.
        //read one line at a time
        $line = fgets($fp, 256);
        //Split the data using explode() function
        if($line !="")
        {
            $info = explode(",", $line);
            $player_name = $info[0];
            $c_value = ($info[2]*100/$info[3] - 30)/20;
            $y_value = ($info[4]/$info[3] - 3)/4;
            $t_value = $info[5]*20/$info[3];
            $i_value = 2.375 - $info[6]*35/$info[3];
            $pass_rating = ($c_value + $y_value + $t_value + $i_value)*100/6;
            $ratings[$player_name] = round($pass_rating, 2);
        }
    }
    arsort($ratings);
    
    echo "<h2>NFL Player Pass Ratings</h2><br/>\n";
    echo"<table>\n";
    echo "<tr><th>Name</th><th>Pass Rating</th></tr>\n";
    foreach ($ratings as $player_name => $pass_rating)
    {
        echo "<tr><td>{$player_name}</td><td>{$pass_rating}</td></tr>\n";
    }
    echo"</table>\n";
    
    echo "<h2>NFL Player Pass Ratings: Great, Good, Mediocre</h2><br/>\n";
    echo"<table>\n";
    echo "<tr><th>Name</th><th>Pass Rating</th></tr>\n";
    foreach ($ratings as $player_name => $pass_rating)
    {
        //Determine rating label
        if($pass_rating > 95) {
            $rating_label = "Great";        
        } elseif ($pass_rating > 90) {
            $rating_label = "Good";
        } elseif($pass_rating > 85) {
            $rating_label = "Mediocre";
        } else {
            $rating_label = "Terrible";
        }
        echo "<tr>\n";
        echo "<td>{$player_name}</td>\n";
        echo "<td>{$pass_rating}<b> -- {$rating_label}</b></td>\n";
        echo "</tr>\n";
    }
    echo"</table>\n";
}
?>
Edited by Psycho
Link to comment
Share on other sites

Hey thanks for the help it seemed to be the <br/> tags, thanks for the rewrite I compared to mine and made adjustments accordingly. the final code takes your style which I am lacking in and and also your for each block. I appreciate it because im really trying to become a great programmer, not just capable of doing it.  If you have any suggestions on how I could improve please let me know, and again thanks for the help.

final code:

<?php
// this script reads data from a text file
// open file and read data
$fp=fopen("nflstats.csv", "rw");
 if (!$fp){
 echo "Could not open the file!";
 exit();
 }
 displayData($fp);

function displayData($fp){
    $line1 = fgets($fp, 256);
    $ratings = array();
    while(!feof($fp)){
        //read one line at a time
        $line= fgets($fp, 256 );
        $total= 0;
       //Split the data using explode() function
        if($line !=""){
            $info=explode(",", $line);
            $player_name = $info[0];
            $c_value = ($info[2]*100/$info[3] - 30)/20;
            $y_value = ($info[4]/$info[3] - 3)/4;
            $t_value = $info[5]*20/$info[3];
            $i_value = 2.375 - $info[6]*35/$info[3];
            $pass_rating =round(($c_value + $y_value + $t_value + $i_value)*100/6, 2);
            $ratings[$player_name]= $pass_rating;
        }
    }
    arsort($ratings);

    echo "<table border=1>";
    echo "<tr><td colspan=\"3\" ><h2>"."NFL Player Pass Ratings"."</h2></td></tr>";
    echo "<tr><th>Name</th><th>Pass Rating</th></tr>\n";

    foreach ($ratings as $player_name=>$pass_rating){
    echo "<tr><td>{$player_name}</td><td>{$pass_rating}</td></tr>\n";
    }
    echo"</table>\n";

    echo"<table border=1>";
    echo "<tr><th colspan=\"22\" ><h2>"."NFL Player Pass Ratings: Great, Good, Mediocre"."</h2></td></tr>";
    echo "<tr><th>Name</th><th>Pass Rating</th></tr>\n";

    foreach ($ratings as $player_name=>$pass_rating){
        if($pass_rating > 95) {
            $rating_label = "Great";
        } elseif($pass_rating >90){
            $rating_label = "Good";
  } elseif($pass_rating >85){
            $rating_label = "Mediocre";
        } else{
            $rating_label = "Terrible";
        }
        echo "<tr>\n";
        echo "<td>{$player_name}</td><td>{$pass_rating}<b> -- {$rating_label}</b></td>\n";
        echo "</tr>\n";
}
   echo"</table>\n";
}
?>

Link to comment
Share on other sites

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.