Random ads with view counter

Asked

Viewed 133 times

1

I’m with the following script to put random ads on the site, besides simple and easy also works really well and fast with various ads.

The problem is this: How many times each ad was chosen by this script (how many times they were 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>";
?>

  • If you use some database you can create a table with a column with a reference and the other with the quantity and increment, if you do not have can do an array in the same way serialize the data and save in a txt.

1 answer

1

You can store that information in a database, this solution won’t cover that, this I’m going to focus on storing that information in a file, which is one of the ways to do that:

<?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>

Adjust the name of the file you want to save to, this file will get 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 after the comma is the number of times the image fell.

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

I hope you don’t mind but I think I’ve improved the arrays structure a bit $imagens, $URL, so everything is together in the same structure.

  • Miguel, is there any way to block the image so it doesn’t appear more than once on the same page? I thought about using cookie but I don’t know if it would be the right option. Can you contact me? [email protected]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.