Оцветяване на взети полета

chovekadjo

Registered
Взех класирането на sport1.bg:
Код:
             header('Content-type: text/html; charset=UTF-8');
             $contents = file_get_contents("http://www.sport1.bg/sport1/home/football/world/spa/standings");
             preg_match_all('#<table border="1" cellspacing="0" width="100%"\s?>(.*?)</table>#is', $contents, $matches);
             $matches = $matches[0];
                 foreach ($matches as $m) {
                     echo "<div style='width: 600px; margin: 0 auto;'>$m . <br></div>";
                 }
Искам тези, който са от първо до трето място да са със зелен цвят, 4-я със оранжев, 5 и 6 с жълто, а 18, 19 и 20 със червен. Някой може ли да помогне :)
 
PHP:
  header('Content-type: text/html; charset=UTF-8');
             $contents = file_get_contents("http://www.sport1.bg/sport1/home/football/world/spa/standings");
             preg_match_all('#<table border="1" cellspacing="0" width="100%"\s?>(.*?)</table>#is', $contents, $matches);
             $matches = $matches[0];
$i = 0;
                 foreach ($matches as $m) {
if($i==1 OR $i==2 OR $i==3) {$color="green";}
elseif($i==4) {$color="orange";}
elseif($i==5 OR $i==6){$color="yellow";}
elseif($i==18 OR $i==19 OR $i==20){$color="red";}
else{$color="black";}
                     echo "<div style='width: 600px; margin: 0 auto;color:$color;'>$m . <br></div>";
$i++;
 }

мисля че това ще ти помогне :)
 
как така не стана... я дай някво демо да се ориентирам...

между временно пробвай така
PHP:
foreach ($matches as $m) {

$i++;
if($i==1 OR $i==2 OR $i==3) {$color="green";}

elseif($i==4) {$color="orange";}

elseif($i==5 OR $i==6){$color="yellow";}

elseif($i==18 OR $i==19 OR $i==20){$color="red";}

else{$color="black";}

                     echo "<div style='width: 600px; margin: 0 auto;color:$color;'>$m . <br></div>";


 }
 
Доколкото виждам се взима цялата таблица, а не елемент по елемент и затова не става. И всъщност се получава, че цикъла минава само веднъж. Затова и не се оцветява.
 
PHP:
<?php
    header('Content-type: text/html; charset=UTF-8');
    $broi = 20;//брой на отборите
    $contents = file_get_contents("http://www.sport1.bg/sport1/home/football/world/spa/standings");
    preg_match_all('#<table border="1" cellspacing="0" width="100%"\s?>(.*?)</table>#is', $contents, $matches, PREG_PATTERN_ORDER);
    $matches = $matches[0];	
    function str_replace_once($str_pattern, $str_replacement, $string)
	{
       
        if (strpos($string, $str_pattern) !== false)
	    {
            $occurrence = strpos($string, $str_pattern);
            return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
        }
       
        return $string;
    }
	
    $new = str_replace_once('<tr>', '<tr style="">', $matches[0]);
    for($i=1; $i<=$broi; $i++)
    {
        if($i==1 OR $i==2 OR $i==3) {$color="green";}
        elseif($i==4) {$color="orange";}
        elseif($i==5 OR $i==6){$color="yellow";}
        elseif($i==18 OR $i==19 OR $i==20){$color="red";}
        else{$color="black";}
        $new = str_replace_once('<tr>', "<tr style='color:$color;'>", $new);
	}	
    echo "<div style='width: 600px; margin: 0 auto;'>$new</div>";
?>

Готов си.
 

Back
Горе