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.
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.
– Augusto