Rotating content (image and text) on website

Thanks for the response Mittineague –

As mentioned earlier, my php knowledge is slim to none … are you saying I cannot use this code if I have 6 blurbs? I tried the code with 4 blurbs first as a test and still got errors.

Also, initially I tried SamA’s code but wasn’t able to make it work at all (my error, I know, not SamA’s) so I tried this code.

How would I change the code?

Many thanks,

Just simple math. If you have something that returns values, but you need to use those values less one, subtract one.

Okay, I very much appreciate your help but I am not understanding is – so sorry to be dense.

The only number listed in that page is 0 – so are you saying my code should read:

include $blurbs[rand(-1, count($blurbs))];

Okay, in googling php array random content, I found a script that looks quite similar to the one I am using, and the -1 is between the two ending parentheses on line 15 … which I honestly never would have figured out. I assumed we were talking about numbers actually written in the code (of which, there was only 0).

So I have changed my code to read:


include $blurbs[rand(-1, count($blurbs)-1)];

That appears to be working. Any other suggestions or beginning php tutorials gratefully appreciated. :smiley:

Ooops … typo …

I have changed the code to read:

include $blurbs[rand(0, count($blurbs)-1)];

Making educated guesses can work (or appear to work) sometimes, but don’t hesitate to check the documentation. IMHO PHP has very good documentation. eg.

int rand ( int $min , int $max )
If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 5 and 15 (inclusive), for example, use rand(5, 15).

The array has six members (PHP calls them array elements).
They have index values ranging from 0 to 5
You could mess with this, but there is no need to complicate something that will work fine as it is.

Because count will return the integer 6 but you need max to be 5, your change to count($blurbs) - 1 is correct.

However, a min of -1 is not correct because the lowest array index is 0 not -1

Gotcha! Thanks so much for explaining.

Re documentation – the problem for me is that it assumes a degree of knowledge that I don’t have. So I will have to make time to google “php for idiots (or complete beginners)” and read/watch some tutorials. My only webpage skills are with html and css … and for anything else I have just enough skill to google and experiment. :wink: As you noted, that only gets you so far. :smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.