Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

Lionsanime Directory   Improve your ranking, submit to directories   V7N Directory

Reply
 
LinkBack Thread Tools Display Modes
Old 06-28-2004, 04:19 AM   #1 (permalink)
Inactive
 
I, Brian's Avatar
 
Join Date: 10-26-03
Posts: 2,466
iTrader: 0 / 0%
Latest Blog:
None

I, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebody
Randomising page elements with php includes

I'm building a site with some advertising on. There are 15 possible adverts, and I wish to call up only 5 on any page.

I'm calling up the ads in a slightly random manner, via php include statements:

[code:1:2c63422be7]
<?php include 'ad' . rand(1,3) . '.php' ?>
<?php include 'ad' . rand(4,6) . '.php' ?>
<?php include 'ad' . rand(7,9) . '.php' ?>
<?php include 'ad' . rand(10,12) . '.php' ?>
<?php include 'ad' . rand(13,15) . '.php' ?>
[/code:1:2c63422be7]

I would like to completely randomise the order in which the 15 adverts are called - ie, so they can appear in any order.

I would also like to be able to automatically remove one or more ads at random, to help randomise the entire display. For example, if ads 1,4,7,10 and 13 are called, I would like to see a script remove one or two of these randomly.

Is this going to be easy for someone to script for? What would such a script look like?

Or would I be asking for too much?
I, Brian is offline  
Add Post to del.icio.us
Reply With Quote
Sponsored Links
SEO Hosting by HostGator  Advertise Here  Buy Blog Links
Old 06-28-2004, 08:17 AM   #2 (permalink)
v7n Mentor
 
hatchet's Avatar
 
Join Date: 10-11-03
Posts: 1,149
iTrader: 0 / 0%
Latest Blog:
None

hatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nice
Should be able to do it in an array.. Set one random number, append it to an unique array, loop it back through, etc.. then check for array_length to see if it's 5 while(array_length($array) < 5) .. stop .. display codes.
Call each of the ads by $array[0], $array[1] etc.. in the place of rand(1,3). Or something like that anyway.
hatchet is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-28-2004, 08:36 AM   #3 (permalink)
Inactive
 
I, Brian's Avatar
 
Join Date: 10-26-03
Posts: 2,466
iTrader: 0 / 0%
Latest Blog:
None

I, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebody
Well, perhaps I could if I knew how to script or program.

Perhaps I should have posted this in the requests board, asking for some example code to try out?
I, Brian is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-28-2004, 11:04 AM   #4 (permalink)
v7n Mentor
 
hatchet's Avatar
 
Join Date: 10-11-03
Posts: 1,149
iTrader: 0 / 0%
Latest Blog:
None

hatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nice
while(count($adarray) < 5) {
$adnum=rand(1,15);
array_push($adarray, $adnum);
array_unique($adarray);
} return $adarray;
This would be called at the very top of each page
Then you would call each ad by the following..
<?php include 'ad' . $adarray[0] . '.php' ?>
<?php include 'ad' . $adarray[1] . '.php' ?>
etc..
I haven't tested this or the logic but it seems ok.
hatchet is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-29-2004, 01:46 AM   #5 (permalink)
Inactive
 
I, Brian's Avatar
 
Join Date: 10-26-03
Posts: 2,466
iTrader: 0 / 0%
Latest Blog:
None

I, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebody
Thanks for the help, hatchet. I'm afraid I'm simply getting the script printed on the page, and lots of include errors - ad.php not found. I think maybe I should rethink my strategy for this.
I, Brian is offline  
Add Post to del.icio.us
Reply With Quote
Old 06-29-2004, 07:26 AM   #6 (permalink)
v7n Mentor
 
hatchet's Avatar
 
Join Date: 10-11-03
Posts: 1,149
iTrader: 0 / 0%
Latest Blog:
None

hatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nice
Try changing the include to read like the following
<?php include ("ad".$adarray[0].".php"); ?>
You should be get rid of the include errors as long as the ad1.php exists within the same folder. There may be other errors ... either post the erros here or email me the files and I'll see if I can get it working.
hatchet is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-02-2004, 02:09 AM   #7 (permalink)
Inactive
 
I, Brian's Avatar
 
Join Date: 10-26-03
Posts: 2,466
iTrader: 0 / 0%
Latest Blog:
None

I, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebody
Thatnks for trying to help, hatchet, but I couldn't quite get it to work.

Perhaps I should simplify the entire question -

I have 5 page elements I wish to randomise the order of, so that instead of coming up as:

1,2,3,4,5

the order is randomised:

3,5,2,4,1

etc

Would that be much easier to script for?
I, Brian is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-02-2004, 07:38 AM   #8 (permalink)
v7n Mentor
 
hatchet's Avatar
 
Join Date: 10-11-03
Posts: 1,149
iTrader: 0 / 0%
Latest Blog:
None

hatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nice
Ok... try this..
[code:1:c12ed91825]$s = array();
$n = 5; // the range you require (this will give 1-5)

function set_num() {
global $s, $n;
$add = "yes";
$ran = rand(1, $n);
if(count($s) > 0) {
foreach($s as $sh) {
if($ran == $sh) {
$add = "no";
}
}
}
if($add == "yes") {
$s[] = $ran;
} else {
set_num();
}
}

// call the function as needed
while(count($s) < $n) {
set_num();
}
// output
<?php include ("ad".$s[0].".php"); ?>
<?php include("ad".$s[1].".php"); ?>
[/code:1:c12ed91825]

Let me know.. you can change the number ($n) up to 15 if you need it to and then just call as many as you want. They'll be random and unique.
hatchet is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-02-2004, 07:40 AM   #9 (permalink)
v7n Mentor
 
hatchet's Avatar
 
Join Date: 10-11-03
Posts: 1,149
iTrader: 0 / 0%
Latest Blog:
None

hatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nice
I should also clarify - I did not code the above example. It was a code sample at php.net from stuartc1 posted June 2003.
hatchet is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-03-2004, 02:33 AM   #10 (permalink)
Inactive
 
I, Brian's Avatar
 
Join Date: 10-26-03
Posts: 2,466
iTrader: 0 / 0%
Latest Blog:
None

I, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebody
Hi Hatchet -

Your help is certainly appreciated in this Darn it, though, I must be doing something wrong as it's still not working.

I'm sure it must be something just very simple, but although I've tried to vary the code, the only thing I can get to work is if I rename the output statements like so:

[code:1:dad4063a06]
<?php include ("ad1".$s[1].".php"); ?>
[/code:1:dad4063a06]

Although that will bring up the numbers 1-5, they aren't in a randomised sequence.

If I simply leave the script with the php includes as so:
[code:1:dad4063a06]
<?php include ("ad".$s[1].".php"); ?>
[/code:1:dad4063a06]

then I simply get a page of include errors.


This is where the page is at:
http://www.britecorp-hosting5.co.uk/ads/

and this the code as it is for the moment:
[code:1:dad4063a06]
<html>
<head>
<title>Ads</title>
</head>
<body>

<script>
$s = array();
$n = 5; // the range you require (this will give 1-5)

function set_num() {
global $s, $n;
$add = "yes";
$ran = rand(1, $n);
if(count($s) > 0) {
foreach($s as $sh) {
if($ran == $sh) {
$add = "no";
}
}
}
if($add == "yes") {
$s[] = $ran;
} else {
set_num();
}
}

// call the function as needed
while(count($s) < $n) {
set_num();
}
</script>

<?php include ("ad".$s[1].".php"); ?>
<?php include ("ad".$s[2].".php"); ?>
<?php include ("ad".$s[3].".php"); ?>
<?php include ("ad".$s[4].".php"); ?>
<?php include ("ad".$s[5].".php"); ?>

</body>
</html>
[/code:1:dad4063a06]

Any ideas?

Anyway, your help is much appreciated - I'll be sure to send some links your way.
I, Brian is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-04-2004, 06:36 AM   #11 (permalink)
Inactive
 
Join Date: 10-14-03
Location: Top left of England, UK
Posts: 237
iTrader: 0 / 0%
Latest Blog:
None

trevHCS is on the right pathtrevHCS is on the right pathtrevHCS is on the right path
Send a message via MSN to trevHCS
Replace <script> with <?php and replace </script> with ?>

<script> and </script> are used for client side scripting like Javascript, therefore when PHP reads the page it just thinks thats part of the HTML and ignores it and so your later array $s doesn't contain anything.

Trev
trevHCS is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-04-2004, 06:41 AM   #12 (permalink)
SVB
Inactive
 
SVB's Avatar
 
Join Date: 10-13-03
Posts: 7,481
iTrader: 0 / 0%
Latest Blog:
None

SVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest orderSVB is a web professional of the highest order
Send a message via Yahoo to SVB
yep, <script> </script> is for javascript.
<?php ?> is for PHP
SVB is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-05-2004, 01:20 PM   #13 (permalink)
v7n Mentor
 
hatchet's Avatar
 
Join Date: 10-11-03
Posts: 1,149
iTrader: 0 / 0%
Latest Blog:
None

hatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nicehatchet is just really nice
Yep.. that's definately the problem. Replace that whole page with this
[code:1:9e9cc83fe5]<?php
$s = array();
$n = 5; // the range you require (this will give 1-5)

function set_num() {
global $s, $n;
$add = "yes";
$ran = rand(1, $n);
if(count($s) > 0) {
foreach($s as $sh) {
if($ran == $sh) {
$add = "no";
}
}
}
if($add == "yes") {
$s[] = $ran;
} else {
set_num();
}
}

// call the function as needed
while(count($s) < $n) {
set_num();
}
?>
<html>
<head>
<title>Ads</title>
</head>
<body>
<?php include ("ad".$s[0].".php"); ?>
<?php include ("ad".$s[1].".php"); ?>
<?php include ("ad".$s[2].".php"); ?>
<?php include ("ad".$s[3].".php"); ?>
<?php include ("ad".$s[4].".php"); ?>

</body>
</html>[/code:1:9e9cc83fe5]
You have to start with $s[0] as your first one.
hatchet is offline  
Add Post to del.icio.us
Reply With Quote
Old 07-08-2004, 01:31 AM   #14 (permalink)
Inactive
 
I, Brian's Avatar
 
Join Date: 10-26-03
Posts: 2,466
iTrader: 0 / 0%
Latest Blog:
None

I, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebodyI, Brian is liked by somebody
Thanks for that folks, much appreciated.

It's always the little things...
I, Brian is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
3 Essential Elements For Success januario Marketing Forum 0 01-27-2008 06:52 PM
Off page seo elements identity_00 SEO Forum 9 02-16-2005 02:20 AM
PHP dynamic includes vs ASP dynamic includes question! imaginemn Coding Forum 1 06-17-2004 06:55 PM
Applying Classes to UL and LI elements in CSS pachrist Coding Forum 2 06-14-2004 02:55 PM
Does text inside IFrame elements affect Google page ranking? rjohnson Google Forum 4 11-15-2003 11:04 AM


Sponsor Links
Get exposure! Get exposure! Find Scripts Web Hosting Directory Get exposure! SEO Blog


All times are GMT -7. The time now is 06:44 PM.
© Copyright 2008 V7 Inc