Zakhary Posted July 6, 2012 Share Posted July 6, 2012 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>"; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 6, 2012 Share Posted July 6, 2012 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> Quote Link to comment Share on other sites More sharing options...
Zakhary Posted July 6, 2012 Author Share Posted July 6, 2012 I wrote it wrong way because you did not understand. I want all lines to be echoed on green color. Last 8 should be black. That's what I want to do. Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 6, 2012 Share Posted July 6, 2012 //all lines but the last 8 $firstLines = array_slice($lines, 0, count($lines)-9); //last 8 lines $lastLines = array_slice($lines,count($lines)-9); Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 6, 2012 Share Posted July 6, 2012 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.