Jump to content

Multiple random ads


legato89
 Share

Recommended Posts

I'm having an issue getting multiple random ads to display. Currently, pages created under the parent page 1304 are chosen at random using ->get() and displayed fine. The issue occurs when trying to display multiple ads. The same ad is chosen and displayed.

Here is my code: 

				if ( $ad_page instanceof NullPage )
				{
				$ad = $pages->get('parent=1304, sort=random, include=hidden, limit=1');
					
				}

I have tried using getRandom and find() but to no avail.

Link to comment
Share on other sites

Would something like this work?

$ads = $pages->find("parent=1304, include=hidden");
$randomAds = $ads->findRandom(4); // or whatever number you need

foreach ($randomAds as $a) {
    #do stuff
}

If there are a lot of children of parent=1304 be aware that this might not be the best performing option.

  • Like 1
Link to comment
Share on other sites

Would something like this work?

$ads = $pages->find("parent=1304, include=hidden");
$randomAds = $ads->findRandom(4); // or whatever number you need

foreach ($randomAds as $a) {
    #do stuff
}

If there are a lot of children of parent=1304 be aware that this might not be the best performing option.

There are around 8. Currently, the children will display and randomly be chosen after each refresh.

Link to comment
Share on other sites

That's strange. Just had a change to test it on 253 country pages which are all children of 1 parent page (countries with id 1013)

<?php 

/*
* countries template
*/

$countries = $pages->find("parent=1013");
$randomCountries = $countries->findRandom(4); // or whatever number you need

foreach ($randomCountries as $rc) {
    echo "{$rc->title}\n";
}

// result
Ierland
Niue
Suriname
Mayotte

// after refresh, result
Spanje
Grenada
Spitsbergen en Jan Mayen
Jemen

Seems to work a charm. Are you sure you got you're variable names and such correct??

And, this also works, and might be more efficient. Not sure if both give the same 'randomness' but they both seem to work quite nicely:

<?php 

/*
* countries template
*/

$countries = $pages->find("parent=1013, limit=4, sort=random");

foreach ($countries as $rc) {
    echo "{$rc->title}\n";
}
Link to comment
Share on other sites

That's strange. Just had a change to test it on 253 country pages which are all children of 1 parent page (countries with id 1013)

<?php 

/*
* countries template
*/

$countries = $pages->find("parent=1013");
$randomCountries = $countries->findRandom(4); // or whatever number you need

foreach ($randomCountries as $rc) {
    echo "{$rc->title}\n";
}

// result
Ierland
Niue
Suriname
Mayotte

// after refresh, result
Spanje
Grenada
Spitsbergen en Jan Mayen
Jemen

Seems to work a charm. Are you sure you got you're variable names and such correct??

And, this also works, and might be more efficient. Not sure if both give the same 'randomness' but they both seem to work quite nicely:

<?php 

/*
* countries template
*/

$countries = $pages->find("parent=1013, limit=4, sort=random");

foreach ($countries as $rc) {
    echo "{$rc->title}\n";
}

Thanks for your help. I think the issue may be how the code is being generated further down:

				$display_sidebar .= $ad->body;

				# Update this ad's view count
				$ad_views = $ad->ad_views;
				$count = ( empty($ad_views) ) ? 1 : $ad_views + 1;

				$ad->setOutputFormatting(FALSE);
				$ad->set('ad_views', $count);
				$ad->save('ad_views');
Link to comment
Share on other sites

It's hard to tell from the snippets of code you posted and without knowing exactly what's going on in the code. Maybe if you paste all relevant code we could have a look.

On a sidenote

If you do figure it out be sure to go for the limit approach. Out of curiosity i did some testing and it showed what probably was to be expected:

// Method 1
echo "<strong>Method 1</strong>";
$t = Debug::timer();
$countries = $pages->find("parent=1013, limit=4, sort=random");
echo "<ol>";
foreach ($countries as $rc) {
    echo "<li>{$rc->title}</li>";
}
echo "</ol>";
echo "<p>Found random items in " . Debug::timer($t) . "seconds</p>";

// Method 2
echo "<strong>Method 2</strong>";
$t = Debug::timer();
$countries2 = $pages->find("parent=1013");
$randomCountries = $countries2->findRandom(4); // or whatever number you need
echo "<ol>";
foreach ($randomCountries as $rc) {
    echo "<li>{$rc->title}</li>";
}
echo "</ol>";
echo "<p>Found random items in " . Debug::timer($t) . "seconds</p>";

Results in this on a set of 253 pages:

Method 1

  1. Botswana
  2. Micronesia
  3. Sint-Helena, Ascension en Tristan da Cunha
  4. Ghana

Found random items in 0.0040seconds

Method 2

  1. Zwitserland
  2. Guatemala
  3. Roemenië
  4. Taiwan

Found random items in 0.0690seconds

It shows a pretty consistent speed difference around factor 16. Not that you would probably notice with the small number of ads.

Link to comment
Share on other sites

				# if: no ad specified; choose at random
				if ( $ad_page instanceof NullPage )
				{
					$ad = $pages->get('parent=1304, sort=random, include=hidden, limit=1');
					
				}
				
				# else: display the specified ad
				else
				{
					$ad = $ad_page;
				} # end if

...and then how the ad is displayed plus the ad counter code

				$display_sidebar .= $ad->body;

				# Update this ad's view count
				$ad_views = $ad->ad_views;
				$count = ( empty($ad_views) ) ? 1 : $ad_views + 1;

				$ad->setOutputFormatting(FALSE);
				$ad->set('ad_views', $count);
				$ad->save('ad_views');
			}
			# else:
			else
			{
				$display_sidebar .= $content->sidebar;
			} # end if

			$display_sidebar .= '</div>' . LF;
		} # end loop

	} # end if

	echo $display_sidebar;
?>

Edit: I got it working, kind of. I had to comment out lines 89-91 to get rid of the error message I kept getting, and use the echo you included instead of having it displayed at line 83. 

Link to comment
Share on other sites

It looks like "$ad" is not a page but a PageArray (multiple pages). So guessing from your code above you use something like the following to get "$ad_page":

$ad_page = $pages->find('...');

// or

$ad_page = $pages->findRandom('...');

The solution would be to change line 65-67:

# else: display the specified ad
else
{
	if($ad_page instanceof PageArray) $ad = $ad_page->first();
	else $ad = $ad_page;
} # end if
Link to comment
Share on other sites

Or if you want to use multiple ads you have to use a foreach loop:

# else: display the specified ad
else
{
	if($ad instanceof PageArray) {
		foreach($ad as $ad_item)
			$display_sidebar .= $ad_item->body;
		
			# Update this ad's view count
				$ad_views = $ad_item->ad_views;
			$count = ( empty($ad_views) ) ? 1 : $ad_views + 1;
			
			$ad_item->setOutputFormatting(0);
			$ad_item->set('ad_views', $count);
			$ad_item->save();
		}
	} else {
		$display_sidebar .= $ad->body;
		
		# Update this ad's view count
		$ad_views = $ad->ad_views;
		$count = ( empty($ad_views) ) ? 1 : $ad_views + 1;
		
		$ad->setOutputFormatting(FALSE);
		$ad->set('ad_views', $count);
		$ad->save();
	}
} # end if
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...