1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What am I doing wrong?

Discussion in 'PHP' started by mintoj, Sep 25, 2008.

  1. #1
    Hi,

    I have the following array of objects and I am trying to get it to randomly select one the of the objects from the array, but it only ever seems to return the last object in the array. Anyone have any ideas why and how to fix it:

    <?php

    @$obj->Site = 'Site1';
    $obj->URL = 'URL1';
    $theSite[0] = $obj;

    $obj->Site = 'Site2';
    $obj->URL = 'URL2';
    $theSite[1] = $obj;

    $obj->Site = 'Site3';
    $obj->URL = 'URL3';
    $theSite[2] = $obj;

    $counts = count($theSite);
    $choice = rand (0,$counts-1);

    echo $theSite[$choice]->Site;

    ?>


    Thanks

    J
     
    mintoj, Sep 25, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    i am not familiar with this class/obj thing?? but it works for me by adding unset($obj); after u assign value to the $theSite array.
    The reason i will think of unset($obj); is because when i print_r() the original array it show all as Site3 and URL3.

    
    @$obj->Site = 'Site1';
    $obj->URL = 'URL1';
    $theSite[0] = $obj;
    unset($obj);
    
    $obj->Site = 'Site2';
    $obj->URL = 'URL2';
    $theSite[1] = $obj;
    unset($obj);
    
    $obj->Site = 'Site3';
    $obj->URL = 'URL3';
    $theSite[2] = $obj;
    unset($obj);
    
    $counts = count($theSite);
    $choice = rand (0,$counts-1);
    echo $theSite[$choice]->Site;
    
    PHP:
     
    ads2help, Sep 25, 2008 IP
  3. mintoj

    mintoj Peon

    Messages:
    317
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that, destroying the object each time does seem to work.

    J
     
    mintoj, Sep 25, 2008 IP