Random ads with viewer counter

1

I have the following script to put random ads on the site, in addition to simple and easy also works very well and fast with multiple ads.

The problem is as follows: How many times have each ad been chosen by this script (how many times have they been viewed)?

<?php
$imagens = array (
"http://megafeiraoveiculos.com.br/banners/01.png",
"http://megafeiraoveiculos.com.br/banners/02.png",
"http://megafeiraoveiculos.com.br/banners/03.png"
);

$imagenstotal = count($imagens);
$imagenstotal--;
$randomimagens = rand(0,$imagenstotal);
$URL = array (

"http://megafeiraoveiculos.com.br/contato.php", // 01
"http://megafeiraoveiculos.com.br/contato.php", // 02
"http://megafeiraoveiculos.com.br"              // 03
);

echo "<a href='$URL[$randomimagens]' target='_blank'>
<img style='border:1px solid #3f3f3f;' src='$imagens[$randomimagens]'> </a>";
?>

    
asked by anonymous 13.12.2016 / 15:16

1 answer

1

You can save this information in a Database, this solution will not cover this, in this I will focus on saving this information in a file, which is one of the ways to do this:

<?php
$imagens = array (
    array(
        'img' => "http://megafeiraoveiculos.com.br/banners/01.png",
        'url' => "http://megafeiraoveiculos.com.br/contato.php"
    ),
    array(
        'img' => "http://megafeiraoveiculos.com.br/banners/02.png",
        'url' => "http://megafeiraoveiculos.com.br/contato.php"
    ),
    array(
        'img' => "http://megafeiraoveiculos.com.br/banners/03.png",
        'url' => "http://megafeiraoveiculos.com.br"
    ),
);
$img_rand = $imagens[rand(0, count($imagens) - 1)];

$ficheiro = 'tests.txt';
$lines = explode("\n", file_get_contents($ficheiro));

$num = 1;
foreach($lines as $key => $value) { // percorrer todas as linhas
    $img = explode(',', $value)[0];
    if($img == $img_rand['img']) { // caso a imagem já exista no ficheiro
        $num = explode(',', $value)[1] + 1; // vamos ver quantas vezes já apareceu e somar 1
        unset($lines[$key]); // apagar a linha antiga do nosso array
        break;
    }
}

$lines[] = $img_rand['img']. "," .$num; // acrescentar nova linha com o valor atualizado, seja pela primeira vez (1) ou não (quantidade anterior + 1)
$lines = implode("\n", $lines);
file_put_contents($ficheiro, trim($lines));

?>
<a href='<?php echo $img_rand['url'] ?>' target='_blank'>
<img style='border:1px solid #3f3f3f;' src='<?php echo $img_rand['img']; ?>'> </a>

Set the name of the file where you want to save, this file will have the following structure:

http://megafeiraoveiculos.com.br/banners/03.png,16
http://megafeiraoveiculos.com.br/banners/01.png,14
http://megafeiraoveiculos.com.br/banners/02.png,16

As we see next to the comma is the number of times the image has changed.

PS: It is very important that there are no more commas in a line, just the one that separates the image from the number of times it has already appeared

I hope you do not mind but I think I've improved the structure of the $imagens, $URL arrays a bit, so it's all together in one structure.

    
13.12.2016 / 17:03