Jump to content

Recognizing last line in .txt


Zakhary

Recommended Posts

I want to make all echoed lines green WITHOUT last 8 lines. However, I don't know how to make it. Any help?

 

Preview: http://tourney.adrenalinex.co.uk/rounds/r2.php

 

Code:

<?
function format_time($t) {
   $minutes = floor($t / 60000);
   $seconds = sprintf('%02d',floor(($t / 1000) % 60));
   $ms = sprintf('%03d', $t % 1000);
   return $minutes . ":" . $seconds . "." . $ms;
}
$highscores = file_get_contents('http://highscores.adrenalinex.co.uk/tourney4.php?c=1&r=2');
$lines = explode("\n", $highscores);
$x=0;
echo "<table style=\"text-align: center; font-weight: bold; border: 1px solid #CCC;\" border=\"1\" cellpadding=\"4\" align=\"center\"><tr><td style=\"background: url('wp-content/themes/arras/images/feed-title-white.jpg'); font-weight: bold; color: red; width: 40px;\"> </td><td style=\"background: url('wp-content/themes/arras/images/feed-title-white.jpg'); font-weight: bold; color: red; width: 200px;\">Drivers Name</td><td style=\"background: url('wp-content/themes/arras/images/feed-title-white.jpg'); font-weight: bold; color: red; width: 100px;\">Time</td><td style=\"background: url('wp-content/themes/arras/images/feed-title-white.jpg'); font-weight: bold; color: red; width: 100px;\">Interval to 1st</td><td style=\"background: url('wp-content/themes/arras/images/feed-title-white.jpg'); font-weight: bold; color: red; width: 100px;\">Interval to driver ahead</td></tr>";
foreach ($lines as &$line) {
   if ($x>0) {
      echo "<TR><TD>$x</TD><TD>$names
</TD><TD>";
      echo format_time($times[$x]);
      echo "</TD><TD>";
      if ($x>1) {
         echo "+ " . format_time($times[$x] - $times[1]);
      } else {
         echo "-";
      }
      echo "</TD><TD>";
      if ($x>1) {
              echo "+ " . format_time($times[$x] - $times[$x-1]);
      } else {
              echo "-";
      }
      echo "</TD></TR>";
   }
   $x++;
   list($names, $times[$x]) = sscanf($line, "%s %d");
}
echo "</TABLE>";
?>

Link to comment
Share on other sites

Your code is a little difficult to follow. You can just strip off the last 8 lines the loop using array_slice(). There are also more efficient ways to handle the different handling of the first and subsequent records.

 

<?php

function format_time($t)
{
   $minutes = floor($t / 60000);
   $seconds = sprintf('%02d',floor(($t / 1000) % 60));
   $ms = sprintf('%03d', $t % 1000);
   return $minutes . ":" . $seconds . "." . $ms;
}

$highscores = file_get_contents('http://highscores.adrenalinex.co.uk/tourney4.php?c=1&r=2');
$lines = explode("\n", $highscores);
//Strip off the last 8 records
$lines = array_slice($lines, 0, count($lines)-9);

$table_data = '';
foreach ($lines as $idx => $line)
{
    list($name, $time) = sscanf($line, "%s %d");
    $no = $idx + 1;
    $f_time = format_time($time);
    if($no == 1)
    {
        $first_time = $time;
        $int_to_first = '-';
        $int_to_prev  = '-';
    }
    else
    {
        $int_to_first = format_time($time - $first_time);
        $int_to_prev  = format_time($time - $last_time);
    }

    $table_data .= "<tr><td>$no</td><td>$name</td><td>$f_time</td><td>$int_to_first</td><td>$int_to_prev</td></tr>\n";
    $last_time = $time;
}

?>
<html>
<head>
<style>

.results_table {
    text-align: center;
    font-weight: bold;
    border: 1px solid #CCC;
}

th {
    background: url('wp-content/themes/arras/images/feed-title-white.jpg');
    font-weight: bold;
    color: red;
}
</style>
</head>

<body>
<table class="results_table" border="1" cellpadding="4" align="center">
    <tr>
    <th style="width: 40px;"></th>
    <th style="width: 200px;">Drivers Name</th>
    <th style="width: 100px;">Time</th>
    <th style="width: 100px;">Interval to 1st</th>
    <th style="width: 100px;">Interval to driver ahead</th>
    </tr>
    <?php echo $table_data; ?>
</table>
</body>
</html>

Link to comment
Share on other sites

You could use jcbones's solution of creating the two or, just create a ternary operator int he code to switch from red to green in the loop. I would do this:

 

<?php

function format_time($t)
{
   $minutes = floor($t / 60000);
   $seconds = sprintf('%02d',floor(($t / 1000) % 60));
   $ms = sprintf('%03d', $t % 1000);
   return $minutes . ":" . $seconds . "." . $ms;
}

$highscores = file_get_contents('http://highscores.adrenalinex.co.uk/tourney4.php?c=1&r=2');
$lines = explode("\n", $highscores);
//Get index of where red records should start
$red_idx = count($lines) - 8;

$table_data = '';
foreach ($lines as $idx => $line)
{
    list($name, $time) = sscanf($line, "%s %d");
    $no = $idx + 1;
    $f_time = format_time($time);
    if($no == 1)
    {
        $first_time = $time;
        $int_to_first = '-';
        $int_to_prev  = '-';
    }
    else
    {
        $int_to_first = format_time($time - $first_time);
        $int_to_prev  = format_time($time - $last_time);
    }

    $class = ($idx<$red_idx) ? 'green' : 'red';
    $table_data .= "<tr class='$class'><td>$no</td><td>$name</td><td>$f_time</td><td>$int_to_first</td><td>$int_to_prev</td></tr>\n";
    $last_time = $time;
}

?>
<html>
<head>
<style>

.results_table {
    text-align: center;
    font-weight: bold;
    border: 1px solid #CCC;
}

th {
    background: url('wp-content/themes/arras/images/feed-title-white.jpg');
    font-weight: bold;
    color: red;
}

.green td { color: green; }
.red td { color: red; }
</style>
</head>

<body>
<table class="results_table" border="1" cellpadding="4" align="center">
    <tr>
    <th style="width: 40px;"></th>
    <th style="width: 200px;">Drivers Name</th>
    <th style="width: 100px;">Time</th>
    <th style="width: 100px;">Interval to 1st</th>
    <th style="width: 100px;">Interval to driver ahead</th>
    </tr>
    <?php echo $table_data; ?>
</table>
</body>
</html>

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.