Some php help with this ad rotator wp plugin..

Discussion in 'PHP' started by Solid_Nuts, Nov 26, 2008.

  1. #1
    I'm trying to get the ad rotator wp plugin to work by the line order instead of it being random.. can any php expert help me out?

    here is the orignal code

    
    <?php
    function getad($ad = '') {
      $ad = trim($ad);
      if(strlen($ad) > 0) {
        $ad = ABSPATH . "wp-content/" . $ad . '.txt';
        if(file_exists($ad)) {
          $ads = file($ad);
          return $ads[rand(0, sizeof($ads)-1)];
        }
      }
    }
    ?>
    PHP:
    and this is the modfied one but it dosen't move onto the next line..

    
    <?php
    function getad($ad = '') {
      $ad = trim($ad);
      if(strlen($ad) > 0) {
        $ad = ABSPATH . "wp-content/" . $ad . '.txt';
        if(file_exists($ad)) {
          $ads = file($ad);
          //$adscnt = count($ads);
          foreach($ads as $linenum => $line) {
    			return $line;
    		}
        }
      }
    }
    ?>
    PHP:
    I'm sure it's something simple but no idea since I'm clueless when it comes to php.

    appreciate any help with this.
     
    Solid_Nuts, Nov 26, 2008 IP
  2. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    try
    
    <?php
    $_SESSION['current_ad'] = 0;
    function getad($ad = '') {
      $ad = trim($ad);
      if(strlen($ad) > 0) {
        $ad = ABSPATH . "wp-content/" . $ad . '.txt';
        if(file_exists($ad)) {
          $ads = file($ad);
          $i = $_SESSION['current_ad'];
          $_SESSION['current_ad']++;
          return $ads[$i];
        }
      }
    }
    ?>
    
    PHP:
    However, you must have sessions enabled in your code.
     
    djzmo, Nov 27, 2008 IP
  3. Solid_Nuts

    Solid_Nuts Active Member

    Messages:
    886
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thnaks for the help but where will i need to add the session in my code?
     
    Solid_Nuts, Nov 27, 2008 IP
  4. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    just try the code I given first.

    If you see any errors regarding session, you will have to add
    session_start();
    PHP:
    at the very top of your page (ie. header), after the <?php.
     
    djzmo, Nov 27, 2008 IP
  5. Solid_Nuts

    Solid_Nuts Active Member

    Messages:
    886
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I tried the code and it stopped displaying the html codes in the text file all together.. any ideas?
     
    Solid_Nuts, Nov 27, 2008 IP
  6. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #6
    well, forget about the sessions.
    now try these codes:

    <?php
    $_COOKIE['current_ad'] = 0;
    function getad($ad = '') {
      $ad = trim($ad);
      if(strlen($ad) > 0) {
        $ad = ABSPATH . "wp-content/" . $ad . '.txt';
        if(file_exists($ad)) {
          $ads = file($ad);
          $i = $_COOKIE['current_ad'];
          $_COOKIE['current_ad']++;
          return $ads[$i];
        }
      }
    }
    ?>
    PHP:
    don't forget to remove the session_start(); to avoid whitescreen
     
    djzmo, Nov 27, 2008 IP
  7. Solid_Nuts

    Solid_Nuts Active Member

    Messages:
    886
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #7
    the 2nd code loads only the first line with out rotating to the other lines..

    Thanks I Appreciate your help.
     
    Solid_Nuts, Nov 27, 2008 IP