Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Randomised Ads

Hi, I realise a lot of the posts in this forum are me asking for support, but whatever.

I plan on adding an advertisement system to my forums. Not ripping off RMXP.org's idea, I've had these plans for ages, but now I want to add them to my site properly, or at least get the feature scripted up and working.

I haven't got a clue how I'd do this, and google is no help.

What I need is to display a random image from a bank of images. Each one also needs to link to it's respective website.

For example, picks a random image between 1 and 10.

Image 5 is chosen, so it shows "RMXP_ORG.png" with hyperlink "www.rmxp.org".

This can use either HTML or PHP, as it is going in a TinyPortal block on my forums. (If it's needed for some strange reason: http://forums.vengeance-rpg.com) I plan on replacing the Google ad with this new ad.

Thanks if you can help, thanks too if you've tried to understand what I'm asking for but failed.

~Wyatt
 
in a separate php file you could have:
Code:
<?php

srand(time());

$adnum = (rand()%5); //5 being number of ads

if ( $adnum == 1 ) {
	echo "ad 1";
}

if ( $adnum == 2 ) {
	echo "ad 2";
}


if ( $adnum == 3 ) {
	echo "ad 3";
}


if ( $adnum == 4 ) {
	echo "ad 4";
}

if ( $adnum == 5 ) {
	echo "ad 5";
}

?>

(include this of course it'd be much easier)
there's probably a more efficient way, but that's my nooby php way.
 
This is how I personally would do it, wrote this script a couple of days ago for a friend of mine.

Code:
<?php

		$Ad 	=	rand(1, 10);	// Creates a number

	// Add or remove as many of these as you need
	// Just place the url of the image inside ""
	$Image[1]	=	"";  
	$Image[2]	=	""; 
	$Image[3]	=	""; 
	$Image[4]	=	""; 
	$Image[5]	=	""; 
	$Image[6]	=	""; 
	$Image[7]	=	""; 
	$Image[8]	=	""; 
	$Image[9]	=	""; 
	$Image[10]	=	""; 						
	
	// Same as before, just make sure that the right URL goes with the right image
	$URL[1]		=	"";	
	$URL[2]		=	"";	
	$URL[3]		=	"";	
	$URL[4]		=	"";	
	$URL[5]		=	"";	
	$URL[6]		=	"";	
	$URL[7]		=	"";
	$URL[8]		=	"";	
	$URL[9]		=	"";	
	$URL[10]	=	"";																											
?>
	
	<a href="<?php echo $URL[$Ad]; ?>"><img src="<?php echo $Image[$Ad]; ?>" alt="Advertisement" />

then again, personally I would probably use a database to save ads, that way I wouldn't need to go to the source code everytime.
 
Hmm, I'm not sure where I'm going wrong, but it doesn't seem to like the PHP in the url, it keeps trying to take me to:

http://forums.vengeance-rpg.com/<?php echo $Image[$Ad]; ?>

The code I'm using is this:

Code:
<?php

		$Ad 	=	rand(1, 2);	// Creates a number

	// Add or remove as many of these as you need
	// Just place the url of the image inside ""
	$Image[1]	=	"http://img.photobucket.com/albums/v108/dudemaster/adbanner_1.png";  
	$Image[2]	=	"http://img.photobucket.com/albums/v108/dudemaster/adbanner_2.png"; 						
	
	// Same as before, just make sure that the right URL goes with the right image
	$URL[1]		=	"http://www.vengeance-rpg.com";	
	$URL[2]		=	"http://www.vengeance-rpg.com";
																												
?>
	
	<center><a href="<?php echo $URL[$Ad]; ?>"><img src="<?php echo $Image[$Ad]; ?>" alt="advertisement" /></center>

Thanks for your help btw.


============

I am using SimpleMachines, with TinyPortal.

I get the option between:

HTML box:
Doesn't look at the PHP because it's not html

PHP box:
Doesn't look at the HTML because it's not PHP

Script box:
Works better than the two above but still doesn't exactly work (error above)
 
Hmm, I'll play around with it and see if I can get it to work. I'll try actually adding it to the theme of my forum, rather than in a TinyPortal block, that's most likely the problem.

Thanks anyway.
 

___

Sponsor

It looks like what's happening is the entire block of code:
Code:
<?php echo $URL[$Ad]; ?>"><img src="<?php echo $Image[$Ad]; ?>
is being evaluated as a single statement, everything between the first <?php and the last ?>.  You might be able to correct this by just going with that behavior and using:
Code:
<?php echo "<a href='".$URL[$Ad]."'><img src='".$Image[$Ad]."' alt='advertisement' />"; ?>
As to WHY that would happen, I have no idea.  Lesson: don't use weird editing systems.
 

___

Sponsor

Edit:  NM, your PHP isn't being evaluated at all, if it was it wouldn't be left in your source afterward.  Look:
Code:
(...)
<div style="padding:0px; " id="block18"><?php

		$Ad 	=	rand(1, 2);	// Creates a number

	// Add or remove as many of these as you need
	// Just place the url of the image inside ""
	$Image[1]	=	"http://img.photobucket.com/albums/v108/dudemaster/adbanner_1.png";  
	$Image[2]	=	"http://img.photobucket.com/albums/v108/dudemaster/adbanner_2.png"; 						
	
	// Same as before, just make sure that the right URL goes with the right image
	$URL[1]		=	"http://www.vengeance-rpg.com";	
	$URL[2]		=	"http://www.vengeance-rpg.com";
?>																												
	
<?php echo "<a href='".$URL[$Ad]."'><img src='".$Image[$Ad]."' alt='advertisement' />"; ?></div></div>
			<script language="Javascript" type="text/javascript">
(...)
Nothing in between a set of <?php ?> tags should show up in view source if it's being evaluated, as the PHP interpreter removes it before sending it to the client.
 
I just viewed the source on the page you linked, and the PHP code is visable, which means that whatever your using to show this piece of code ain't supporting the usage of PHP.


edit: Seems I wasn't fast enough :P
 

___

Sponsor

No worries, don't hesitate to ask :D  I can't tell you what exactly is causing your problems since I don't know anything about that editor you're using, but at least we can narrow them down.
 
I know this has already been covered, but I think I'll throw my bit of code out there anyway.  It seems a bit simpler to edit...

Code:
<?php
//The multidimentional array containing all of the ad info - this is the only place you edit
$ads = array(   
//               Image Source        Hyperlink Destination          Alt Text
0 => array('images/ad1.png', 'http://www.yoursite.com', 'My Awesome Site'),
1 => array('images/ad2.png', 'http://www.anothersite.net', 'Another Site') //...so on, so forth
);

//-------------------There's no need to edit anything beneath this point------------------------


//count each element in the array
foreach($ads as $value)
{
$x++;
}

//account for the array's start point of 0
$x--;

//select a random ad from the array
$select = rand(0, $x);

//display the selected ad
echo '<a href="'.$ads[$select][1].'"><img src="'.$ads[$select][0].'" alt="'.$ads[$select][2].'" /></a>';

?>

I seem to be good with arrays, so if you update that one array, it should update the rest of the code automatically.  There's no more need to mess with the rand() function or deal with multiple arrays for everything.  I just typed up this script when I saw this, so it is untested.  If there's a problem, let me know.  Hopefully this is helpful!
 
"//account for the array's start point of 0
$x--;"

why not simply make it start at 1? :P Else, it was a really nice way of doing it.
 

___

Sponsor

It's much better to start at 0, otherwise you end up accidentally breaking or having to hack every array transversing function & iterator loop (e.g. foreach + associated functions).
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top