Page 1 of 1

CAn anyone make this more efficient?

Posted: Fri Jul 16, 2004 1:32 am
by John Cartwright

Code: Select all

<?php
<?php 
if  (!defined("IN_SITE"))
    {
    exit("Hacking Attempt!");
    }
	
	$result = mysql_query("SELECT * FROM `matches`") or die("[matches.php] Select Error ". mysql_error());
	
	while ($row = mysql_fetch_array($result))
	{
		$x = 0;

		$round1     = explode("-",$row["round1"]);
		$round2     = explode("-",$row["round2"]);
		
		for($x = 0; $x < 2; $x++) 
		{
			switch ($round1[$x]) :
				case $round1[$x] > $round1[$x+1] :
					 $round1[$x] = "<font color='#666666'>".$round1[$x]."-".$round1[$x+1]."</font>";
					 break;
				case $round1[$x] < $round1[$x+1] :
					 $round1[$x] = "<font color='#999999'>".$round1[$x]."-".$round1[$x+1]."</font>";
					 break;
			endswitch;

			switch ($round2[$x]) :
				case $round2[$x] > $round2[$x+1] :
					 $round2[$x] = "<font color='#666666'>".$round2[$x]."-".$round2[$x+1]."</font>";
					 break;
				case $round2[$x] < $round2[$x+1] :
					 $round2[$x] = "<font color='#999999'>".$round2[$x]."-".$round2[$x+1]."</font>";
					 break;
			endswitch;
		}

// will output all the info here as it loops through each row
	}

?>

?>
This took me like an hour of thinking to get the logic of this... but it looks ugly !! Does anyone have an idea to minimize the lines?

btw this is my first time using switch statement... :) BRAVO FOR ME!

Posted: Fri Jul 16, 2004 1:51 am
by feyd
This assumes you want to do "something" with ties..

Code: Select all

<?php 
   if  (!defined("IN_SITE")) { exit("Hacking Attempt!"); }
    
   $result = mysql_query("SELECT * FROM `matches`") or die("[matches.php] Select Error ". mysql_error()); 
    
   while ($row = mysql_fetch_assoc($result)) 
   { 
      $round1     = explode("-",$row["round1"]); 
      $round2     = explode("-",$row["round2"]); 
       
      for($x = 0; $x < 2; $x++) 
      { 
         $round1[$x] = '<font'.($round1[$x] > $round1[$x+1] ? ' color="#666666"' : ($round1[$x] != $round1[$x+1] ? ' color="#999999"' : '' ) ).'>'.$round1[$x]."-".$round1[$x+1]."</font>";
         $round2[$x] = '<font'.($round2[$x] > $round2[$x+1] ? ' color="#666666"' : ($round2[$x] != $round2[$x+1] ? ' color="#999999"' : '' ) ).'>'.$round2[$x]."-".$round2[$x+1]."</font>";
      } 

// will output all the info here as it loops through each row 
   } 

?>

Posted: Fri Jul 16, 2004 1:54 am
by John Cartwright
unexpected : on line 54

Code: Select all

<?php
$round1[$x] = '<font'.($round1[$x] > $round1[$x+1] ? ' color="#666666"'. : ($round1[$x] != $round1[$x+1] ? ' color="#999999"' : '' ) ).'>'.$round1[$x]."-".$round1[$x+1]."</font>";
?>
is line 54

Posted: Fri Jul 16, 2004 1:59 am
by feyd
oops. had an extra dot in there.. try it now.

Posted: Fri Jul 16, 2004 2:00 am
by John Cartwright
w00t ty mr. guru