How to code a PHP sequence array. Help.

by 4 replies
6
Been googling, and I haven't found how to do an array that goes in sequence. Found how to do a random one.

Code:
 
What I'm trying to achieve is an array that goes in order from 1, 2, 3, 4, and so on. Then starts back at 1 when it reaches the end. Basically, when someone clicks on a link it goes through the array and loads the next number. It goes up by clicks for everyone. Meaning Person A gets 1, Person B gets 2, and so on.

I imagine this is fairly simple? Could someone show me or direct me to a website explaining? Thanks.
#programming #array #code #php #sequence
  • Well the biggest problem will be the array for all users, they are controlled by a per page load basis. If you held it in a database then loaded it, going at random or some 50/50 chance would be easy.

    To populate an array like you ask all you need to do is

    $links[] = 'site1';
    $links[] = 'site2';

    this will add it to the end of the array, in the order you are looking for.

    Ok just to elaborate a little array pushing is used in many languages, you can do the same with the

    array_push function http://php.net/manual/en/function.array-push.php

    array_push($links,"site url");

    but it use to be faster to do the way I mentioned above, I haven't tested it in a long time tho.
    • [ 1 ] Thanks
  • Then you would do a count check

    $arr_size = count($links);

    if($user_click_count > ($arr_size - 1)) {
    $user_click_count = 0; //reset to 0 since we were at the end
    }
    • [ 1 ] Thanks
    • [1] reply
  • Thanks guys!

    Going with Brandon's method. Testing it, seems like it'll work out great.
  • [DELETED]
  • Banned
    [DELETED]

Next Topics on Trending Feed

  • 6

    Been googling, and I haven't found how to do an array that goes in sequence. Found how to do a random one. Code: // Create the array $links = array(); // Populare the array $links[0] = "http://www.site.com/ads1.php"; $links[1] = "http://www.site.com/ads2.php"; $links[2] = "http://www.site.com/ads3.php"; // Count links $num = count($links); // Randomize order $random = rand(0, $num-1); // Print random link echo $links[$random]; What I'm trying to achieve is an array that goes in order from 1, 2, 3, 4, and so on. Then starts back at 1 when it reaches the end. Basically, when someone clicks on a link it goes through the array and loads the next number. It goes up by clicks for everyone. Meaning Person A gets 1, Person B gets 2, and so on.