← Javascript

Dúvida Random

Lida 8168 vezes

Offline

SFCMiguel 
Membro
Mensagens 187 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 100 Posts

Boas tardes tenho o seguinte código no meu blog para fazer a rotação de 3 banners.

Citar
<script language="JavaScript">
var max = 3;
var nrImages = 3;
function makeImages() {
this[0] = "http://ptwebtech.com/banners/banner_468x60.gif";
this[1] = "http://affiliation.fotovista.com/track/imp_img.php?bid=MTc2OzEzNjgy";
this[2] = "/parceiros/publicidade/bannerpub.gif";
this.length = nrImages;
}
function makeLinks() {
this[0] = "http://www.ptwebtech.com/clientes/link.php?id=4";
this[1] = "http://affiliation.fotovista.com/track/click.php?bid=MTc2OzEzNjgy";
this[2] = "http://www.projectofree.com/publicidade/";
this.length = nrImages;

}
var vetImages = new makeImages();
var vetLinks = new makeLinks();
var x = Math.round(Math.random()*max);
var y = max / nrImages;
for(var cont = 1;cont*y<= max;cont++) {
if (x <= (cont*y)) {
document.write("<a href="+vetLinks[cont-1]+" target=_blank><img src="+vetImages[cont-1]+" border='0'/></a>");
break;
}
}
</script>

O problema é que o 1º banner está sempre a repetir, o segundo é elas por elas, e o ultimo raramente passa, e a intenção era ao fazer f5 trocar o banner por outro.
Alguém sabe uma maneira mais eficaz ou o que se passa com este script?
Offline

André Freitas 
Membro
Mensagens 941 Gostos 16
Troféus totais: 29
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Viva,
Tens aqui um random em PHP mais simples: http://www.forum.maistrafego.pt/index.php?topic=13567.0

Cumprimentos,
André Freitas.
Offline

SFCMiguel 
Membro
Mensagens 187 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 100 Posts

Olá 4ndr3, eu experimentei esse mas não sei porque dava erro, deve ser por eu ter de colocar dentro de:

Citar
?php get_header(); ?>
      <div id="main-wrapper">
         <div id="left">   <div align="center">

(aqui vai tar o script)

</div> 
Offline

André Freitas 
Membro
Mensagens 941 Gostos 16
Troféus totais: 29
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Viva,
Não coloques o <?php nem o ?>

Cumprimentos,
André Freitas.
Offline

SFCMiguel 
Membro
Mensagens 187 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 100 Posts

Tive de por <? e ?> para reconhecer o código mas depois quando adicionei o código da imagem voltou a da erro:

Citar
align="center">
<?$blah[] = '
<a href='http://www.ptwebtech.com/clientes/link.php?id=4' target='_blank'><img src='http://ptwebtech.com/banners/banner_468x60.gif' /></a>
';

$blah[] = '
<b>Olá Portugal!</b>
';

$blah[] = '
<b>Olá Mais-Tráfego!</b>
';

// seed php random number generator
// http://www.php.net/srand
srand ((float) microtime() * 10000000);

// print a random $blah
// http://www.php.net/array_rand
print $blah[array_rand($blah)];
?></div>

Parse error: syntax error, unexpected T_STRING in /home/projecto/public_html/wp-content/themes/groovie/groovie-tt/index.php on line 3
Offline

Fernando Augusto 
Membro
Mensagens 922 Gostos 0
Feedback +1

Troféus totais: 30
Trófeus: (Ver todos)
Linux User Mobile User Windows User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Tenta este.

Código: [Seleccione]
<script language="javascript"><!--

function banner() {
imagens=new Array();
textos=new Array();
imagens[0]="http://i40.tinypic.com/14j3rpi.png";
imagens[1]="http://i42.tinypic.com/30be14z.png";
imagens[2]="http://i40.tinypic.com/2rff2iv.png";
imagens[3]="http://i43.tinypic.com/xm2z3n.png";
textos[0]="Naruto Portugal RPG";
textos[1]="Naruto Portugal RPG";
textos[2]="Naruto Portugal RPG";
textos[3]="Naruto Portugal RPG";
var sorteia=Math.random() * imagens.length;
var ncerto=parseInt(sorteia);
document.write("<img border=0 src="+imagens[ncerto]+" alt="+textos[ncerto]+">");
}
banner();
//-->
</script>

Altera só o link das imagens e o texto
Offline

SFCMiguel 
Membro
Mensagens 187 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 100 Posts

Olá killer999 eu tenho de meter links
Offline

André Freitas 
Membro
Mensagens 941 Gostos 16
Troféus totais: 29
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Level 5 Level 4 Level 3 Level 2

Viva,
Tenta o seguinte:

Código: [Seleccione]
';


$blah[] = '
<b>Olá mundo!</b>
';

$blah[] = '
<b>Olá Portugal!</b>
';

$blah[] = '
<b>Olá Mais-Tráfego!</b>
';

// seed php random number generator
// http://www.php.net/srand
srand ((float) microtime() * 10000000);

// print a random $blah
// http://www.php.net/array_rand
print $blah[array_rand($blah)];

echo'
Offline

Fernando Augusto 
Membro
Mensagens 922 Gostos 0
Feedback +1

Troféus totais: 30
Trófeus: (Ver todos)
Linux User Mobile User Windows User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Desculpa, pensava que era só para mudar de imagem.

Encontrei este aqui.

Código: [Seleccione]
Banner rotativo. A cada abertura de páginas aparece um banner diferente

<--inicio do código-->

<script language="Javascript">
hoje = new Date()
numero_de_imagens = 3
segundos = hoje.getSeconds()
numero = segundos % numero_de_imagens
if (numero == 0){
banner = "http://www.seusite.com//banner1.gif"
link = "http://www.listfire.com/48897"
alvo = "out"
texto= "Lista livre de spam"
}
if (numero == 01){
banner = "http://www.seusite.com/promobanner03.gif"
link = "http://www.publipt.com/pages/index.php?refid=silviobr"
alvo = "out"
texto= "Ganhe dinheiro lendo e-mail"
}
if (numero == 02){
banner = "http://www.seusite.com/powerlist.gif"
link = "http://www.powerlist.rg8.net/?ID=ganhonline"
alvo = "out"
texto= "Divulgação gratuita"
}
if (numero == 03){
banner = "http://www.seusite.com/megaterremoto2.gif"
link = "http://www.megaterremoto.com/index.asp?ID=sasnews"
alvo = "out"
texto= "Divulgação gratuita"
}
document.write('<a href="' + link + '" target="' + alvo + '"><img src="' + banner + '" alt="' + texto + '" width="468" height="60" border="0"></a>')</script>

<--fim do código-->

As partes em vermelho são as únicas que você deve mexer.(Obs: Não corrija acentuação no código)
Offline

SFCMiguel 
Membro
Mensagens 187 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 100 Posts

4ndr3 esse código funciona quando meto <? ?> mas não funciona quando meto o código das imagens.

killer999 Esse sim funcionou bem, mas diz que não repete imagens mas ele repete xD Começo a desconfiar que se calhar não há nenhum mesmo eficaz.
Offline

Fernando Augusto 
Membro
Mensagens 922 Gostos 0
Feedback +1

Troféus totais: 30
Trófeus: (Ver todos)
Linux User Mobile User Windows User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Pois, espera que mais alguem dentro do assunto venha ao tópico.
Espero ter ajudado
Offline

SFCMiguel 
Membro
Mensagens 187 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 100 Posts

Mais ninguém?
É que estes códigos só dão para imagens e eu queria um que fosse simples e que desse para flash também.
Offline

-RJ- 
Membro
Mensagens 1188 Gostos 0
Troféus totais: 30
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Não funciona quando metes as imagens porque metes
Citar
<a href='http://www.ptwebtech.com/clientes/link.php?id=4' target='_blank'><img src='http://ptwebtech.com/banners/banner_468x60.gif' /></a>
em vez do ' usa " (ou \")...

Código: [Seleccione]
echo 'isto não funciona 'exemplo' ';
echo "isto já funciona 'exemplo' ";
echo ' isto também funciona "exemplo" ';
Offline

SFCMiguel 
Membro
Mensagens 187 Gostos 0
Troféus totais: 27
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3 Level 2 Level 1 100 Posts

Enão ficaria

Citar
<?php
// generates some random content

// start an array with some text strings...

$blah[] = '
<a href="http://www.ptwebtech.com/clientes/link.php?id=4" target="_blank"><img src="http://ptwebtech.com/banners/banner_468x60.gif" /></a>
';

$blah[] = '
<b>Olá Portugal!</b>
';

$blah[] = '
<b>Olá Mais-Tráfego!</b>
';

// seed php random number generator
// http://www.php.net/srand
srand ((float) microtime() * 10000000);

// print a random $blah
// http://www.php.net/array_rand
print $blah[array_rand($blah)];

?>

Deixando apensas os ' . '  Na parte do código e os " . " Nas imagens?
Offline

-RJ- 
Membro
Mensagens 1188 Gostos 0
Troféus totais: 30
Trófeus: (Ver todos)
Apple User Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes Poll Voter Level 5 Level 4 Level 3

Exacto, isso já funciona de certeza ;)