A help with design

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

A help with design

Post by John Cartwright »

I have this script and I'm trying to reduce the amounts of line in the script, I got the logic and everything figure out but I want to try and reduce this snipplet of my script but have no idea where to start? Anyone know of any ideas :) TY

Code: Select all

<?php
	 if ($map=="de_dust2") {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
	 	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$dust2record</font></td>\n".
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$dust2percent %</font></td>\n";
	 }elseif ($map == "de_aztec")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".	
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$aztecrecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$aztecpercent %</font></td>\n";
     }elseif ($map == "de_nuke")  {	 
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$nukerecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$nukepercent %</font></td>\n";
     }elseif ($map == "de_train")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$trainrecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$trainpercent %</font></td>\n";
     }elseif ($map == "de_cpl_fire")  {		 
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".          
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$firerecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$firepercent %</font></td>\n";
     }elseif ($map == "de_cpl_mill")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$millrecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$millpercent %</font></td>\n";
     }elseif ($map == "de_inferno")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
          "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$infernorecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$infernopercent %</font></td>\n";
     }elseif ($map == "de_cbble")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
          "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$cbblerecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$cbblepercent %</font></td>\n";
     }elseif ($map == "de_comrade")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".          
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comraderecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comradepercent %</font></td>\n";
     }elseif ($map != $maplist)  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".          
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comraderecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comradepercent %</font></td>\n";
     }		 
		} 
?>
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

but how are all these variables set? maybe you can set them in arrays, like this:

$maps = array('de_dust2' = array('map','record','percent'),
'de_aztec' = array('map','record','percent'));

and to get the results:

Code: Select all

<?php
    echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".    
        "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">">{$maps[$map][0]}</font></td>\n". 
          "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">">{$maps[$map][1]}</font></td>\n". 
        "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">{$maps[$map][2]}%</font></td>\n"; 
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$maps is set from information being pulled form the database inside the loop
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

This is a little more help

Code: Select all

<?php
// Setting variables from the table 
    while ( $row = mysql_fetch_array($result) ) { 
      $date =      $row["date"]; 
      $outcome =   $row["outcome"]; 
      $league =    $row["league"]; 
      $map =       $row["map"]; 
      $opponent =  $row["opponent"]; 

if ($pastmap != $map) { 

     echo "<tr>\n";
     
	 if ($map=="de_dust2") {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
	 	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$dust2record</font></td>\n".
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$dust2percent %</font></td>\n";
	 }elseif ($map == "de_aztec")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".	
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$aztecrecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$aztecpercent %</font></td>\n";
     }elseif ($map == "de_nuke")  {	 
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$nukerecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$nukepercent %</font></td>\n";
     }elseif ($map == "de_train")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$trainrecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$trainpercent %</font></td>\n";
     }elseif ($map == "de_cpl_fire")  {		 
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".          
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$firerecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$firepercent %</font></td>\n";
     }elseif ($map == "de_cpl_mill")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$millrecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$millpercent %</font></td>\n";
     }elseif ($map == "de_inferno")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
          "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$infernorecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$infernopercent %</font></td>\n";
     }elseif ($map == "de_cbble")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".
          "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$cbblerecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$cbblepercent %</font></td>\n";
     }elseif ($map == "de_comrade")  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".          
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comraderecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comradepercent %</font></td>\n";
     }elseif ($map != $maplist)  {
	 echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n".          
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$map</font></td>\n". 
     	  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comraderecord</font></td>\n".
		  "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">$comradepercent %</font></td>\n";
     }		 
		} 
	echo "</tr>\n";
?>
eletrium
Forum Commoner
Posts: 34
Joined: Tue Feb 10, 2004 3:38 pm

Post by eletrium »

I'm no expert in PHP, but this thing is convoluted... you might consider going to Barnes and Noble and picking up Martin Fowler's book Refactoring and reading the chapter covering how NOT to write convoluted code in the first place....

Knowing that, it is just a few steps of breaking things up into smaller pieces gradually.

Pretty much, clean code is just code broken up into many smaller pieces.

Great code is clean code where the smaller pieces are re-used elsewhere in logical fashions.

You don't even need to BUY the book, just flip through that one chapter. It will make you a 100 times better just reading that one chapter.

Also, flip through the Pragmatic Programmer as well... no in depth reading, just read snippets here and there and pick out stuff you really like. I use about half of it, but would never use the other half. However, the half I DO use is absolutely indispensible. My faves are "Good Neighbors have Good Fences" and "Tracer Bullets". Also, "Orthogonality" is nice.

Pretty much though, you HAVE to write convoluted code to start out as a programmer. Everyone does. Its only when you go back a month or more later on and try to work with it that you get the idea of why you need to clean things up... its also a good way to learn HOW it should be cleaned up.
I followed techniques for cleaning out of books, then found out it didn't work for me, so I adapted the techniques... I learned by trial and error how I needed to annotate and comment stuff so when I came back moths later I could still work with it ok...

I'd go back and look at code you wrote about a month ago... might give you insight on that...
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

do u only get one map back?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Definantly something I will be looking into :P
Thanks a lot.. but until then anyone help me :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Do I only get one map back. Let me get back to you on this, I'm working on something to do with htat, but its nothing to do with that snipplet, that works just fine.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What about something along the lines of ..

Code: Select all

<?php
$map = 'de_dust';
$mappart = end(explode('_', $map)); //gets the bit after the last _
$maprecord = $mappart.'record';
$mappercent = $mappart.'percent';
?>
<td width="4%" height="22" background="record_table_rowbg.jpg"></td>
<td width="32%" height="22" background="record_table_rowbg.jpg">
  <font size="2"><?php echo $map ?></font>
</td>
<td width="32%" height="22" background="record_table_rowbg.jpg">
  <font size="2"><?php echo ${$maprecord} ?></font>
</td>
<td width="32%" height="22" background="record_table_rowbg.jpg">
  <font size="2"><?php echo ${$mappercent} ?> %</font>
</td>
Which handles all maps presuming they are named foo_bar and you are using bar as the 'key' to the map data .. foorecord, foopercent etc..
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

but there are some variables that the ending afte _ don't match
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Oh, i didn't see any in his example code, which ones are they?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm sooooooo confused right now.. can you clarity please?
eletrium
Forum Commoner
Posts: 34
Joined: Tue Feb 10, 2004 3:38 pm

Refactoring

Post by eletrium »

Here is a Refactoring of the code:

Code: Select all

<?php 
  
  function outputFormatted( aMap $map, aRecord $record, aPercent $percent )
  &#123;
      echo "<td width="4%" height="22" background="record_table_rowbg.jpg"></td>\n". 
         "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">aMap</font></td>\n". 
          "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">aRecord</font></td>\n". 
          "<td width="32%" height="22" background="record_table_rowbg.jpg"><font size="2">aPercent %</font></td>\n";   
  &#125;

    if ($map == "de_dust2") 
    &#123; 
      outputFormatted( $map, $dust2record, $dust2percent );
    &#125;
    
    elseif ($map == "de_aztec")  
    &#123; 
      outputFormatted( $map, $aztecrecord, $aztecpercent );
    &#125;
    
    elseif ($map == "de_nuke")  
    &#123;    
      outputFormatted( $map, $nukerecord, $nukepercent );
    &#125;
    
    elseif ($map == "de_train")  
    &#123; 
      outputFormatted( $map, $trainrecord, $trainpercent );
    &#125;
    
    elseif ($map == "de_cpl_fire")  
    &#123;       
      outputFormatted( $map, $firerecord, $firepercent );
    &#125;
    
    elseif ($map == "de_cpl_mill")  
    &#123; 
      outputFormatted( $map, $millrecord, $millpercent );
    &#125;
    
    elseif ($map == "de_inferno")  
    &#123; 
      outputFormatted( $map, $infernorecord, $infernopercent );
    &#125;
    
    elseif ($map == "de_cbble")   
    &#123; 
      outputFormatted( $map, $cbblerecord, $cbblepercent );
    &#125;
    
    elseif ($map == "de_comrade")   
    &#123; 
      outputFormatted( $map, $comraderecord, $comradepercent );
    &#125;
    
    elseif ($map != $maplist)  
    &#123; 
      outputFormatted( $map, $comraderecord, $comradepercent );
    &#125;       
      
     &#125;  //end if 
?>
NOW, the function that I wrote is just pretty much pseudo code. I forget how to write a function in PHP specifically, but you can just look that up yourself. Can't hand hold you and do your homework for you. But you should be able to see how this jumble of code was Refactored to be much more readable.
eletrium
Forum Commoner
Posts: 34
Joined: Tue Feb 10, 2004 3:38 pm

Post by eletrium »

Ech, the function I wrote wrapped <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> when it posted... copy and paste the code into a text editor to see it better... I should fix it but I'm too lazy atm after that...
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You should get all of that html out of the scrip. It's impossible to try to figure out what's going on. At first glance there is just one html template there?
Post Reply