Adding 5 variables instead of one! PHP/MYSQL

Asked

Viewed 184 times

0

Hello, I’m having a problem in my conditions(if). Note that I am very new to php/mysql, so this is the reason why possibly my codico is a mess, but it is possible to 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 codico and display 5 ads so that everyone has virtually equal 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, they are summed ONE views for ALL columns(ads01,ads02,ads03,ads04,ads05) in my taabela in my database. Where the correct value was to add only to the variable with the lowest value.

Behold: inserir a descrição da imagem aqui

All values gained +1, where only the count03(ads03 in the table) should have added +1.

I think it is some problem in IF or some failure in closing mysql_query. help me?

  • Missing UPDATE in the code for such

  • Where has UPDATE on your page?

  • What is UPDATE about? I’m new :[

  • Update will change the column value, in your case increment +1

  • mysql_query("UPDATE counters SET ads02 = ads02 + 1"); ?

  • Because in case, I just wanted to be changed the ads02 column in the counters table.

Show 1 more comment

1 answer

0


To increment the column of lower value:

function ShowVar($var) {
    foreach($GLOBALS as $varName => $value) {
        if ($value === $var) {
            return $varName;
        }
    }
    return false;
}

$ads01 = mysql_fetch_row(mysql_query("SELECT ads01 FROM counters"));
$ads02 = mysql_fetch_row(mysql_query("SELECT ads02 FROM counters"));
$ads03 = mysql_fetch_row(mysql_query("SELECT ads03 FROM counters"));
$ads04 = mysql_fetch_row(mysql_query("SELECT ads04 FROM counters"));
$ads05 = mysql_fetch_row(mysql_query("SELECT ads05 FROM counters"));


$result = min($ads01, $ads02, $ads03, $ads04, $ads05);
$a = ShowVar($result);

$valor = mysql_fetch_row(mysql_query("SELECT $a FROM counters"));

$newValor=$valor[0]+1;

mysql_query("UPDATE counters SET $a='$newValor'");

will increment the column of the lowest value by +1. If the columns have the same value, it will increment +1 in the first column of the table, then in the second column and so on.

  • With your logic and small modifications I got the result I wanted!!! besides having left the code smaller, thank you :D

Browser other questions tagged

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