Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   How to code a PHP sequence array. Help. (https://www.warriorforum.com/programming/538443-how-code-php-sequence-array-help.html)

Alexander K 9th February 2012 04:37 PM

How to code a PHP sequence array. Help.
 
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.

I imagine this is fairly simple? Could someone show me or direct me to a website explaining? Thanks.

drem 9th February 2012 09:35 PM

Re: How to code a PHP sequence array. Help.
 
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.

drem 9th February 2012 09:38 PM

Re: How to code a PHP sequence array. Help.
 
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
}

Brandon Tanner 10th February 2012 09:11 AM

Re: How to code a PHP sequence array. Help.
 
I wrote a simple php script a while back that rotates between 10 different links in sequence. You could easily modify it to use how every many links you want...

http://www.warriorforum.com/programm...ml#post2968668

Alexander K 10th February 2012 10:30 AM

Re: How to code a PHP sequence array. Help.
 
Thanks guys!

Going with Brandon's method. Testing it, seems like it'll work out great.


All times are GMT -6. The time now is 05:50 PM.