Jump to content

getRandom vs php rand() function


Recommended Posts

Hi all!

having some trouble working with the getRandom - or actually, getting it to work another way.

I'm currently selecting a random image from an imageArray by using the getRandom function.

Now I want to know which image it picked though, so I thought of using php's own rand() function this way:

rand(0, $arrayTitle->count());

Works like a blast, until it hit the first image at $arrayTitle(0)...

I get the following error:

Error: Uncaught Error: Method name must be a string in /Users/JCN/Sites/ffa/wire/core/WireArray.php:1648
Stack trace:
#0 /Users/JCN/Sites/ffa/site/templates/head.inc(12): WireArray->__invoke(0)
#1 /Users/JCN/Sites/ffa/site/templates/home.php(1): include_once('/Users/JCN/Site...')
#2 /Users/JCN/Sites/ffa/wire/core/TemplateFile.php(182): require('/Users/JCN/Site...')
#3 /Users/JCN/Sites/ffa/wire/core/Wire.php(398): TemplateFile->___render()
#4 /Users/JCN/Sites/ffa/wire/core/Wire.php(333): Wire->runHooks('render', Array)
#5 /Users/JCN/Sites/ffa/wire/modules/PageRender.module(422): Wire->__call('render', Array)
#6 /Users/JCN/Sites/ffa/wire/core/Wire.php(398): PageRender->___renderPage(Object(HookEvent))
#7 /Users/JCN/Sites/ffa/wire/core/Wire.php(333): Wire->runHooks('renderPage', Array)
#8 /Users/JCN/Sites/ffa/wire/core/Wire.php(459): Wire->__call('renderPage', Array)
#9 /Users/JCN/Sites/ffa/wire/core/Wire.php(333): Wire->runHooks('render', Array)
#10 /Users/JCN/Sites/ffa/wire/modules/Process/ProcessPageView.module(187) (line 1648 of /Users/JCN/Sites/ffa/wire/core/WireArray.php) 

...all other images work fine though. Is this something I can fix in the .htaccess? And how come I've never had this before, in other projects? Love to hear your take on things...


Link to comment
Share on other sites

What kind of data is in your $arrayTitle?

and have you compared the value of index 0 with the other items?

Do they differ somehow or are they more or less equal?

Link to comment
Share on other sites

Hi Horst!

thanks for the quick reply!

Ah, fair enough, might be good to share what's in the array indeed; It's actually an imageField, with multiple images in it.

Formatted value for the field is Automatic, as I'm using this field on different pages, which in some case will only have one image, and other places multiple. 

The images are more or less equal if I look at dimensions, size, and titles (just numbers, letters and dashes and underscores). Don't think anything is out of the ordinary there...

Anything else I can check?

Link to comment
Share on other sites

You get back a random integer and how do you call the image from the array? Can you provide a line or two of your code? :)

Does it look like this?

$iRand = rand(0, $arrayTitle->count());
$image = $arrayTitle->eq($iRand);

And why do you not use the getRandom() method? I do not understand what should be the advantage of using php rand here? If you select a random item from your multiple images field, you get the handle to a pageimage and all of its methods and properties.

$image = $arrayTitle->getRandom();

But regardless how you do it, at least you should get a random pageimage object from which you can pull the title or name or whatever you need.

echo $image->title;
echo $image->url;
echo $image->name;
echo $image->filename;
echo $image->description;


PS: Do you make sure that at least one image is provided with your field?

Link to comment
Share on other sites

Well, I started off with the getRandom function, but then the function of the 'random background-image' changed dramatically; 

I needed a way to invert the text on the background-image (black/white depending on image) AND the images have to link through to projects elsewhere on the site.

Brainfart; Maybe it's easier to add an extra image-field to the project and load them from there...instead of going through all this...any way;

At the moment I have the background-images-field ($header_image) and a list of selectable projects ($hlinks) on the home template.

So I was hoping to use the rand() function to find which position from the first array was selected, so I can also grab the linked project from the other array at the same position. Which works fine, except for the initial (0) one, as mentioned before.

Also, right now I'm using the description field ($h_desc) of the image to invert (as mentioned above) when necessary.

Current code:

$h_images = $home->header_image;

$rand_nr = rand(0, $h_images->count());
//$selected = $h_images->getRandom();
$selected = $h_images($rand_nr);

$image = $selected->url;
$h_desc = $selected->description;

$header_imgs = array();
$invert_b = array();
$hi_links = array();

$hlinks = $home->header_image_links;

foreach($h_images as $img){
	$img_loc = $img->url;
	array_push($header_imgs, $img_loc);

	$img_descr = $img->description;
	array_push($invert_b, $img_descr);

foreach($hlinks as $link){
	array_push($hi_links, $link->url);

$index = array_search($selected->url, $header_imgs);

    	echo $hlinks->eq($index)->url;
PS: Do you make sure that at least one image is provided with your field?

Yes, at the moment I'm testing with four images in there...

Could you perhaps also explain the difference between your code and the one I'm using right now, I've never used the eq before for this:

$image = $arrayTitle->eq($iRand);

// VS

$image = $arrayTitle($iRand);


Link to comment
Share on other sites

And a small update;

I changed the random selection to using the eq() function, and now the link (from the linked projects array) works perfectly, but the imageArray is still throwing an error.

Three test-cases;


$selected = $h_images->eq(0);

Finds the image if I echo $selected, and returns the following as well (doing this to double-check):

echo $selected->url.'<br>';
$index = array_search($selected->url, $header_imgs);
echo $index.'<br>';

But then throws an error:

Fatal error: Uncaught Error: Method name must be a string in /Users/JCN/Sites/ffa/wire/core/WireArray.php:1648 

- So here it's actually not finding the link in the other array??


$selected = $h_images->eq(1);

Works like a charm, get all requested items echoed, and link is correct, no problems on this one.


$selected = $h_images->eq($rand_nr);

when selecting the [0] from the array, still the same error as above, but additionally (another refresh/random) the following occurs;

Notice: Trying to get property of non-object in /Users/JCN/Sites/ffa/site/templates/home.php on line 15


Not sure what is happening here, but it seems it cannot access the first image here, but does have access to the first link in the other array...kind of frustrating.

If there's more I can debug or check to improve what we know, please do share. Thanks for all the help so far!

Link to comment
Share on other sites

I cannot follow for what you need all those arrays besides the images-array?

In the images-array all informations are accessible, why do you need to populate additional arrays with redundant data?

I clearly haven't checked what you want to do here.

You get you a random image from a multiple images field (what is a wirearray). And now, where is the other data that you need to find, before you populate all those arrays?

I'm also not sure if this is correct:

$rand_nr = rand(0, $h_images->count());

or if it should be

$rand_nr = rand(0, $h_images->count() -1);

Images->count is 1-based and numeric array indexes are zero-based.


But back to the main question. Is it right that you have an images field and a list with links that is somehow paired one by one, but lives in different objects? What do you use to store the links in?

Wouldn't it be possible to write the links directly into an images field (by default, you have descriptions and tags field, but with a module like images-extra, you can add as many fields to the images as you need).

  • Like 1
Link to comment
Share on other sites

Hi Horst,

yes, I came to a similar conclusion; the additional arrays only makes everything more difficult. Or rather, I'm making it more difficult then it should be...

I wanted to use them though, because I needed to access the same entry (say entry #1) from three different fields. Kind of confusing to be honest.

I just changed it over to using the description and tags field, as you suggested, as I only needed two other fields to arrange everything.

So this way I can also stick with the getRandom, instead of needing the rand() function.

The description field is now used for the project-url, and the tags field to check if the text on the image needs to be inverted or not.

Works like a charm! Didn't know about the images-extra module though, so that's a good one for future reference, thanks!

And the count which is 1-based vs the array was also something I was struggling with before/encountered earlier, but now managed to completely avoid!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By VeiJari
      Hello forum, this is my first security related post, so I'm a bit of a newbie.
      I understand that when I have direct front-input from user I should sanitize the input, but how about when I use a secret key for showing a API for a third-party supplier? Should I sanitize the input->get() key?
      I've tested this issue and I tried ?key=<?php echo $page->field; ?> And without adding any sanitization it comes back: /?key=<?php%20echo%20$page->field;%20?>
      So can I rely on this, or should I still use $sanitizer just in case?
      Thanks for the help!
    • By opalepatrick
      I am looking to hide a googlemap api key from general source code. The first suggestions were to use an environmental variable, then because that doesn't hide the info to then encrypt it. Others have suggested using a config file. (Can I put an array in the config file?) 
      What do you guys do to hide API keys from general source code like this?
    • By MarcC
      So I tried using $page->repeater->getRandom() in some template code but it didn't seem to work. Should this work? If not, what is the recommended way of working with a single, randomly-chosen repeater item?
  • Create New...