...

View Full Version : an IF statment - is there a better way to set this up?



musher
04-25-2005, 09:07 PM
Is a better way to write this code? It seams to be a bit bulky to me and I'd prefer to learn a good habbit from the start. Thanks.




<?php
$todays_date = date("Y");
$previousyear1 = $todays_date - 1;
$previousyear2 = $todays_date - 2;
$previousyear3 = $todays_date - 3;
$previousyear4 = $todays_date - 4;
$previousyear5 = $todays_date - 5;
if ($recordYEAR == $todays_date) {
echo "<td class='tabbedPaneTabSelected'><span class='tabbedPaneTabSelected'><img src='../images/spacer.gif' width='15' height='1'/>".$todays_date."<img src='../images/spacer.gif' width='15' height='1' /></span></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear1."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear1."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear2."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear2."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear3."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear3."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear4."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear4."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear5."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />Previous<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
} elseif ($recordYEAR == $previousyear1) {
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$todays_date."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$todays_date."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTabSelected'><span class='tabbedPaneTabSelected'><img src='../images/spacer.gif' width='15' height='1'/>".$previousyear1."<img src='../images/spacer.gif' width='15' height='1' /></span></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear2."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear2."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear3."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear3."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear4."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear4."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear5."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />Previous<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
} elseif ($recordYEAR == $previousyear2) {
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$todays_date."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$todays_date."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear1."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear1."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTabSelected'><span class='tabbedPaneTabSelected'><img src='../images/spacer.gif' width='15' height='1'/>".$previousyear2."<img src='../images/spacer.gif' width='15' height='1' /></span></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear3."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear3."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear4."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear4."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear5."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />Previous<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
} elseif ($recordYEAR == $previousyear3) {
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$todays_date."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$todays_date."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear1."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear1."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear2."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear2."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTabSelected'><span class='tabbedPaneTabSelected'><img src='../images/spacer.gif' width='15' height='1'/>".$previousyear3."<img src='../images/spacer.gif' width='15' height='1' /></span></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear4."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear4."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear5."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />Previous<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
} elseif ($recordYEAR == $previousyear4) {
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$todays_date."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$todays_date."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear1."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear1."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear2."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear2."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear3."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear3."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTabSelected'><span class='tabbedPaneTabSelected'><img src='../images/spacer.gif' width='15' height='1'/>".$previousyear4."<img src='../images/spacer.gif' width='15' height='1' /></span></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear5."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />Previous<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
} elseif ($recordYEAR <= $previousyear5) {
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$todays_date."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$todays_date."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear1."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear1."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear2."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear2."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear3."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear3."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear4."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear4."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTabSelected'><span class='tabbedPaneTabSelected'><img src='../images/spacer.gif' width='15' height='1'/>Previous<img src='../images/spacer.gif' width='15' height='1' /></span></td>";
}
?>

Kurashu
04-25-2005, 10:29 PM
Two things you can do: Make a switch and convert the echos' contents to sprintfs and store the patterns in variables.

e.g.



echo "<td class='tabbedPaneTabSelected'><span class='tabbedPaneTabSelected'><img src='../images/spacer.gif' width='15' height='1'/>".$todays_date."<img src='../images/spacer.gif' width='15' height='1' /></span></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear1."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear1."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear2."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear2."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear3."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear3."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear4."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />".$previousyear4."<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";
echo "<td class='tabbedPaneTab'><a href='pressreleases_sel.php?recordYEAR=".$previousyear5."'><span class='tabbedPaneTab' style='border-left-width: 0px;'><img src='../images/spacer.gif' width='15' height='1' />Previous<img src='../images/spacer.gif' width='15' height='1' /></span></a></td>";

//becomes:


$pattern_top = '<td class="tabbedPaneTabSelected"><span class="tabbedPaneTabSelected"><img src="../images/spacer.gif" width="15" height="1"/>%s<img src="../images/spacer.gif" width="15" height="1" /></span></td>';
$prev_pattern = '<td class="tabbedPaneTab"><a href="pressreleases_sel.php?recordYEAR=".%s.""><span class="tabbedPaneTab" style="border-left-width: 0px;"><img src="../images/spacer.gif" width="15" height="1" />".%s."<img src="../images/spacer.gif" width="15" height="1" /></span></a></td>';

echo sprintf($pattern_top, $todays_date);
echo sprintf($prev_pattern, $previousyear1);
echo sprintf($prev_pattern, $previousyear2);
echo sprintf($prev_pattern, $previousyear3);
echo sprintf($prev_pattern, $previousyear4);
echo sprintf($prev_pattern, $previousyear5);

Velox Letum
04-26-2005, 12:02 AM
How about a while to minimize code, not to mention single quotes for speed ;).

firepages
04-26-2005, 12:37 AM
<?php
for($x=2001;$x<=2005;$x++){
if($x==$recordYEAR){
echo '<td class="tabbedPaneTabSelected"><span class="tabbedPaneTabSelected"><img src="../images/spacer.gif" width="15" height="1"/>'.$x.'<img src="../images/spacer.gif" width="15" height="1" /></span></td>';
}else{
echo '<td class="tabbedPaneTab"><a href="pressreleases_sel.php?recordYEAR='.$x.'"><span class="tabbedPaneTab" style="border-left-width: 0px;"><img src="../images/spacer.gif" width="15" height="1" />'.$x.'<img src="../images/spacer.gif" width="15" height="1" /></span></a></td>';
}
}
?>

musher
04-26-2005, 05:24 PM
Thanks guys (leaned a couple new things today, and thats always a good thing) I actualy think I'm starting to get the hang of this PHP stuff (I think I have to agree with you PHPers, I think PHP is better and much easer to learn then ASP).

Velox read a few other post on single quotes, makes sense, does it speed stuff up alot?

Velox Letum
How about a while to minimize code, not to mention single quotes for speed


and firepages thanks for the example :thumbsup: . modified your code a bit to reverse the list order and change the last item listed to "Previous". Cool thing is I actual understand whats going on in the code :eek: (ok I did looked up the for command in the in the PHP manual)



<?php
$todays_date = date("Y");
$previousyear5 = $todays_date - 5;
for($x=$todays_date;$x>=$previousyear5;$x--){
if($x==$recordYEAR){
if ($x==$previousyear5){
echo '<td class="tabbedPaneTabSelected"><span class="tabbedPaneTabSelected"><img src="../images/spacer.gif" width="15" height="1"/>Previous<img src="../images/spacer.gif" width="15" height="1" /></span></td>';
}else{
echo '<td class="tabbedPaneTabSelected"><span class="tabbedPaneTabSelected"><img src="../images/spacer.gif" width="15" height="1"/>'.$x.'<img src="../images/spacer.gif" width="15" height="1" /></span></td>';}
}else{
if ($x==$previousyear5){
echo '<td class="tabbedPaneTab"><a href="pressreleases_sel.php?recordYEAR='.$x.'"><span class="tabbedPaneTab" style="border-left-width: 0px;"><img src="../images/spacer.gif" width="15" height="1" />Previous<img src="../images/spacer.gif" width="15" height="1" /></span></a></td>';
}else{
echo '<td class="tabbedPaneTab"><a href="pressreleases_sel.php?recordYEAR='.$x.'"><span class="tabbedPaneTab" style="border-left-width: 0px;"><img src="../images/spacer.gif" width="15" height="1" />'.$x.'<img src="../images/spacer.gif" width="15" height="1" /></span></a></td>';}
}
}
?>

Velox Letum
04-26-2005, 07:07 PM
Its not really a noticable difference with a low use script, however if you plan to have high traffic it should reduce CPU load.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum