Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    an IF statment - is there a better way to set this up?

    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.


    Code:
    <?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>";
      }
    ?>
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Two things you can do: Make a switch and convert the echos' contents to sprintfs and store the patterns in variables.

    e.g.

    PHP Code:
        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); 

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about a while to minimize code, not to mention single quotes for speed .
    "$question = ( to() ) ? be() : ~be();"

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    PHP Code:
    <?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>';
        }
    }
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 . 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 (ok I did looked up the for command in the in the PHP manual)

    Code:
    <?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>';}
        } 
      }
    ?>
    Last edited by musher; 04-26-2005 at 05:28 PM.
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #6
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Its not really a noticable difference with a low use script, however if you plan to have high traffic it should reduce CPU load.
    "$question = ( to() ) ? be() : ~be();"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •