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
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Feb 12, 2004 3:27 pm
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 » Thu Feb 12, 2004 3:35 pm
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";
}
?>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Feb 12, 2004 3:37 pm
$maps is set from information being pulled form the database inside the loop
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Feb 12, 2004 3:38 pm
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 » Thu Feb 12, 2004 3:40 pm
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...
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Feb 12, 2004 3:52 pm
Definantly something I will be looking into
Thanks a lot.. but until then anyone help me
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Feb 12, 2004 3:53 pm
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.
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Feb 12, 2004 4:01 pm
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 » Thu Feb 12, 2004 4:19 pm
but there are some variables that the ending afte _ don't match
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Feb 12, 2004 4:21 pm
Oh, i didn't see any in his example code, which ones are they?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu Feb 12, 2004 4:44 pm
I'm sooooooo confused right now.. can you clarity please?
eletrium
Forum Commoner
Posts: 34 Joined: Tue Feb 10, 2004 3:38 pm
Post
by eletrium » Thu Feb 12, 2004 6:09 pm
Here is a Refactoring of the code:
Code: Select all
<?php
function outputFormatted( aMap $map, aRecord $record, aPercent $percent )
{
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";
}
if ($map == "de_dust2")
{
outputFormatted( $map, $dust2record, $dust2percent );
}
elseif ($map == "de_aztec")
{
outputFormatted( $map, $aztecrecord, $aztecpercent );
}
elseif ($map == "de_nuke")
{
outputFormatted( $map, $nukerecord, $nukepercent );
}
elseif ($map == "de_train")
{
outputFormatted( $map, $trainrecord, $trainpercent );
}
elseif ($map == "de_cpl_fire")
{
outputFormatted( $map, $firerecord, $firepercent );
}
elseif ($map == "de_cpl_mill")
{
outputFormatted( $map, $millrecord, $millpercent );
}
elseif ($map == "de_inferno")
{
outputFormatted( $map, $infernorecord, $infernopercent );
}
elseif ($map == "de_cbble")
{
outputFormatted( $map, $cbblerecord, $cbblepercent );
}
elseif ($map == "de_comrade")
{
outputFormatted( $map, $comraderecord, $comradepercent );
}
elseif ($map != $maplist)
{
outputFormatted( $map, $comraderecord, $comradepercent );
}
} //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 » Thu Feb 12, 2004 6:11 pm
Ech, the function I wrote wrapped <span style='color:blue' title='I'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 » Thu Feb 12, 2004 6:13 pm
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?