Hello, I'm having a problem under my conditions (if). Note that I am very novice in php / mysql, so that's the reason maybe my stuff is a mess, but you can understand it.
<?php
@mysql_connect("localhost", "root", "vertrigo") or die(mysql_error()); //Database connexion
mysql_select_db("aeuhue") or die(mysql_error()); // Select DB
$ads01 = "Ola, anunciante 01!\n";
$ads02 = "Ola, anunciante 02!\n";
$ads03 = "Ola, anunciante 03!\n";
$ads04 = "Ola, anunciante 04!\n";
$ads05 = "Ola, anunciante 05!\n";
function ShowVar($var) { // Funcao para retornar o nome da menor variavel em vez do valor
foreach($GLOBALS as $varName => $value) {
if ($value === $var) {
return $varName;
}
}
return false;
}
$count01 = mysql_fetch_row(mysql_query("SELECT ads01 FROM counters"));
$count02 = mysql_fetch_row(mysql_query("SELECT ads02 FROM counters"));
$count03 = mysql_fetch_row(mysql_query("SELECT ads03 FROM counters"));
$count04 = mysql_fetch_row(mysql_query("SELECT ads04 FROM counters"));
$count05 = mysql_fetch_row(mysql_query("SELECT ads05 FROM counters"));
echo "$count01[0] <br>"; // apenas para visualizar o valor sem precisar ir no phpmyadmin
echo "$count02[0] <br>";
echo "$count03[0] <br>";
echo "$count04[0] <br>";
echo "$count05[0] <br>";
echo "<br><br><br><br>";
$result = min($count01, $count02, $count03, $count04, $count05); // Seleciona as variaveis e verifica qual é o minimo
$a = ShowVar($result); // ShowVar ira voltar o nome da varivel com menor valor, se count03 for a menor, ele retornará "count03"
echo "<b>$a</b><br><br>"; // apenas para visualizar e ver se o if está correto.
if($a == 'count01') // Se a ShowVar($a) retornar 'count01', exiba o $ads01 e adicione uma visualização no ads01
echo $ads01;
mysql_query("UPDATE counters SET ads01 = ads01 + 1");
if($a == 'count02')
echo $ads02;
mysql_query("UPDATE counters SET ads02 = ads02 + 1");
if($a == 'count03')
echo $ads03;
mysql_query("UPDATE counters SET ads03 = ads03 + 1");
if($a == 'count04')
echo $ads04;
mysql_query("UPDATE counters SET ads04 = ads04 + 1");
if($a == 'count05')
echo $ads05;
mysql_query("UPDATE counters SET ads05 = ads05 + 1");
?>
The idea of this code is to display 5 ads so that everyone has almost the same views, using php and mysql. There is a mini counter in mysql.
The problem: The conditions (if) seem to be correct, but every time I enter the page, a VIEW for ALL columns is added (ads01, ads02, ads03, ads04, ads05 ) in my taabela in my database. Where the correct one was to add only to the variable that has the lowest value.
All values have + 1, where only count03 (ads03 in the table) should have added +1.
I think it's some problem with the IF or some failure to close mysql_query. help me?