SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jun 2009
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    displaying different content for odd even pages.

    Hi,

    I'm trying to find out about a php script for displaying different content based on the odd or even numbers of a post. I have an author box set up with information about me but I would like to create another version of this automatically depending on if the post is even or odd.

    If even display this version
    If odd display this version etc..

    Does this have a particular name or if anyone has a link to a guide that would be amazing!

    Thanks

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( $post&)
    {
        
    displayOddVersion();
    }
    else
    {
        
    displayEvenVersion();


  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Divide by 2 is fine, but if in the future you want to divide the post number by 3 or 4, or later 5 etc then you would do well to familiarise yourself with the modulus math operator (%).
    PHP Code:
    $posts 1233;

    if((
    $posts 2) === 1){  // use a strict check here
    echo "do the odd thing";
    }else{
    echo 
    "do the even thing";

    Then you can take that same idea and extract that leftover amount, and program your script to handle the remainder as say, a function or a class.

    This is far more extensible, and less prone to error than writing out a long if/else clause. Just imagine you decided to provide 20 different scenarios?

    PHP Code:
    $posts 1233;

    $target ;

    $leftover $posts $target;

    function 
    thing3(){
    echo 
    'do the 3 leftover thing';
    }

    function 
    thing2(){
    echo 
    'do the 2 leftover thing';
    }

    function 
    thing1(){
    echo 
    'do the 1 leftover thing';
    }

    function 
    thing0(){
    echo 
    'do the 0 leftover thing';
    }

    $op "thing"$leftover;

    $op(); 


Bookmarks

Posting Permissions

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