Discussion:
Un truc bien lourd :-)
(trop ancien pour répondre)
Auden <""@free.fr>
2007-06-02 10:37:23 UTC
Permalink
Bonjour,

J'ai tapé des lignes de code que j'aimerais bien optimiser. Actuellement le
tout fonctionne, mais c'est loin d'être du "joli boulo". Alors historie
d'allérger le tout, je me tourne vers vous !

Voici le code en question :


---------------------------
$query1 = "SELECT AVG(crit1) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query2 = "SELECT AVG(crit2) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query3 = "SELECT AVG(crit3) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query4 = "SELECT AVG(crit4) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query5 = "SELECT AVG(crit5) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
$result3 = mysql_query($query3);
$result4 = mysql_query($query4);
$result5 = mysql_query($query5);
$record_array1 = mysql_fetch_array($result1, MYSQL_NUM);
$record_array2 = mysql_fetch_array($result2, MYSQL_NUM);
$record_array3 = mysql_fetch_array($result3, MYSQL_NUM);
$record_array4 = mysql_fetch_array($result4, MYSQL_NUM);
$record_array5 = mysql_fetch_array($result5, MYSQL_NUM);
$moyenne1 = $record_array1[0] ;
$moyenne2 = $record_array2[0] ;
$moyenne3 = $record_array3[0] ;
$moyenne4 = $record_array4[0] ;
$moyenne5 = $record_array5[0] ;
$totalmoyenne = $moyenne1 + $moyenne2 + $moyenne3 + $moyenne4 + $moyenne5 ;
$total = $totalmoyenne/5 ;


if ($total == 0 ) {
echo "Pas de note";
} else {
echo round($total, 2);
echo " / 10";
}

------------------------------------

Le tout est trés scolaire mais je pense qu'en 3 lignes je devrais pouvoir
avoir la même chose :-)

Merci !


Auden
thierry
2007-06-02 13:46:19 UTC
Permalink
bonjour

ci-après un bout de code qui passe le php -l
mais que je n'ai pas testé (à cause de la requête sql)


$crits = array('crit1','crit2','crit3', 'crit4', 'crit5');
$moyenne = 0;

foreach ($crits as $crit ) {
$res = mysql_query("SELECT AVG($crit) FROM
boutiqueanimalissaintpriest WHERE TO_DAYS(NOW()) - TO_DAYS(date) <= 180");
$row = mysql_fetch_array($res);
$moyenne += $row[0];
}
$total = $moyenne / count($crits);

if ($total == 0 ) {
echo "Pas de note";
} else {
echo round($total, 2);
echo " / 10";
}

bon courage

thierry
Matthieu Moy
2007-06-02 13:46:19 UTC
Permalink
Post by Auden <""@free.fr>
$query1 = "SELECT AVG(crit1) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query2 = "SELECT AVG(crit2) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query3 = "SELECT AVG(crit3) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query4 = "SELECT AVG(crit4) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
$query5 = "SELECT AVG(crit5) FROM boutiqueanimalissaintpriest WHERE
TO_DAYS(NOW()) - TO_DAYS(date) <= 180;";
Ca marche pas de faire juste

SELECT AVG(crit1), AVG(crit2), AVG(...) FROM ...

?
Post by Auden <""@free.fr>
$totalmoyenne = $moyenne1 + $moyenne2 + $moyenne3 + $moyenne4 + $moyenne5 ;
$total = $totalmoyenne/5 ;
Voire carrément

SELECT (AVG(crit1) + AVG(crit2) + AVG(...)) / 5 FROM ...
--
Matthieu
Continuer la lecture sur narkive:
Loading...