PHP code 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

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

PHP code design

Post by pilau »

The following function outputs an array containg details about a gig with a certain ID:

Code: Select all

function ExpiryTemplate($newsdate,$expirydate,$newstitle,$id) {
 $str_E_Temp="";
 //if Gig item hasn't expired do:
  if (strtotime($expirydate) > time()) {
     	$str_E_Temp.= "<TR width='95%' bgcolor='#9B3334 style='border-bottom: #C8D4E0 1px solid;' align='center' valign='middle' nowrap>";

		$str_E_Temp.="<TD style='font-size: 14px' align='left'><A HREF =\"NewsDetails.php?ID=" .$id. "\" >".$newstitle. "</A></TD>";
		$str_E_Temp.="<TD style='font-size: 14px' align='left'>" .$newsdate. "</TD>";

		$str_E_Temp.= "</TR>";
  }
  //if Gig item has expired do:
  if (strtotime($expirydate) < time ()) {
     	$str_E_Temp.= "<TR width='95%' bgcolor='#9B3334' style='border-bottom: #C8D4E0 1px solid;' align='center' valign='middle' nowrap>";

		$str_E_Temp.="<TD style='font-size: 14px' align='left'>".$newstitle."</TD>";
		$str_E_Temp.="<TD style='font-size: 14px' align='left'>" .$newsdate. "</TD>";

		$str_E_Temp.= "</TR>";
  }

 return $str_E_Temp;
 }
The next function reads a mysql table containing rows of gig details. (each row is a single gig. It summons the previous function to output the gig details:

Code: Select all

function TableMenuList($pstrSQL,$pstrHeaderRowParameters, $pstrHeaderColumnParameters,$pstrDataRowParameters, $pstrDataColumnParameters,$intFieldCount)
{
$datab = new Database();
$strTemp="";
$rst= $datab->Execute ($pstrSQL,$strTemp);

if ($strTemp!="")
{
	$strTemp="<TR ".$pstrDataRowParameters . "><TD " .$pstrDataColumnParameters . ">" . $strTemp . "</TD></TR>";
	return $strTemp;
}

else
	{
	$strTemp="";

	//$strTemp="";
	while ($line = mysql_fetch_array($rst, MYSQL_ASSOC))
	{
		$strTemp2=$line['News'];
		$strTemp1=$line['ID'];
		$strTemp3=$line['NewsDate'];
        $strTemp4=$line['ExpiryDate'];
	  $strTemp .= ExpiryTemplate($strTemp3,$strTemp4,$strTemp2,$strTemp1);
	}
}

return $strTemp;
}
I want that the <tr> background will change like it chnages for each post on this thread, one post is X color, than the one after him is Y, and the next is X color again, and so on.
How shoud I do it? Using a boolean?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Quick example:

Code: Select all

<?php
$tr = 0;
//loop starts here
print "<tr class=\"";
if (!$tr)
{
    print "tablerowoff";
    $tr = 1;
}
else
{
    print "tablerowon";
    $tr = 0;
}
print "\">";

//finish the loop
?>
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Great, thanks lot!
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Another commom idiom for this is using the mod operator with a counter.

If you're using a for loop (instead of a while or foreach) then you already have a counter so somethling like

Code: Select all

if (($i%2)==1) {
      // do one style
    } else {
      // do other style
    }
If a counter is available, I think this is "nicer" than introducing the boolean toggle, but if there's no counter the boolean way is good.

Of course even nicer is normally something like (works for either version)

Code: Select all

if (($i%2)==1) {
      $style="odd-row";
    } else {
      $style="even-row";
    }
    $table.="<tr class=\"$style\"> ... </tr>\n";
Now you're not replicating any of the formatting code. And the $style can actually be condensed to a ternary operator

Code: Select all

$style=($i%2) ? "even-row" : "odd-row";
making the code clear, simple, and tight.
Post Reply