Ahojte
Chcel by som Váš požiadať o názor na tento kód.
Vytvoril som stránku, kde sa bodujú výrobky... na stránke, kde sa zobrazujú štatistické hodnoty je tabuľka, kde je zobrazená aktivita hodnotiteľov za posledných 12 mesiacov. Teda koľkokrát hodnotili za daný mesiac. Funguje to, ale ten kód sa mi nezdá byť ani trochu dobrý. Príde mi veľmi dlhý a neohrabaný. Vedeli by ste mi povedať, naviesť ma, ako ho zmenšiť? Poprípade zacykliť?
Výsledok vyzerá takto:
No a toto je ten kód:
<!--
----------------------------------------------------------------------------
Aktivita hodnotiteľov za posledných 12 mesiacov
----------------------------------------------------------------------------
-->
<h2 class="popis_tabuliek">Aktivita enológov (počet bodovaní) za posledných 12 mesiacov</h2>
<table class="statistika_aktivita">
<?php
function mesiac($datum) {
if ($datum == '01') {echo 'jan';}
elseif ($datum == '02') {echo 'feb';}
elseif ($datum == '03') {echo 'mar';}
elseif ($datum == '04') {echo 'apr';}
elseif ($datum == '05') {echo 'máj';}
elseif ($datum == '06') {echo 'jún';}
elseif ($datum == '07') {echo 'júl';}
elseif ($datum == '08') {echo 'aug';}
elseif ($datum == '09') {echo 'sep';}
elseif ($datum == '10') {echo 'okt';}
elseif ($datum == '11') {echo 'nov';}
elseif ($datum == '12') {echo 'dec';}
elseif ($datum == '0') {echo 'dec';}
elseif ($datum == '-1') {echo 'jan';}
elseif ($datum == '-2') {echo 'feb';}
elseif ($datum == '-3') {echo 'mar';}
elseif ($datum == '-4') {echo 'apr';}
elseif ($datum == '-5') {echo 'máj';}
elseif ($datum == '-6') {echo 'jún';}
elseif ($datum == '-7') {echo 'júl';}
elseif ($datum == '-8') {echo 'aug';}
elseif ($datum == '-9') {echo 'sep';}
elseif ($datum == '-10') {echo 'okt';}
elseif ($datum == '-11') {echo 'nov';}
}
?>
<tr>
<th>Meno</th>
<th><?php mesiac(date("m")); ?></th>
<th><?php mesiac(date("m")-1); ?></th>
<th><?php mesiac(date("m")-2); ?></th>
<th><?php mesiac(date("m")-3); ?></th>
<th><?php mesiac(date("m")-4); ?></th>
<th><?php mesiac(date("m")-5); ?></th>
<th><?php mesiac(date("m")-6); ?></th>
<th><?php mesiac(date("m")-7); ?></th>
<th><?php mesiac(date("m")-8); ?></th>
<th><?php mesiac(date("m")-9); ?></th>
<th><?php mesiac(date("m")-10); ?></th>
<th><?php mesiac(date("m")-11); ?></th>
<th>Priemer</th>
</tr>
<?php
$db = DB();
$dvanastmesiace = $db->query("
SELECT user_id, name,
IFNULL(tento.pocet,0) as tento,
IFNULL(minusjeden.pocet,0) as minusjeden,
IFNULL(minusdva.pocet,0) as minusdva,
IFNULL(minustri.pocet,0) as minustri,
IFNULL(minusstyri.pocet,0) as minusstyri,
IFNULL(minuspat.pocet,0) as minuspat,
IFNULL(minussest.pocet,0) as minussest,
IFNULL(minussedem.pocet,0) as minussedem,
IFNULL(minusosem.pocet,0) as minusosem,
IFNULL(minusdevat.pocet,0) as minusdevat,
IFNULL(minusdesat.pocet,0) as minusdesat,
IFNULL(minusjedenast.pocet,0) as minusjedenast,
IFNULL(minusdvanast.pocet,0) as minusdvanast,
(
IFNULL(tento.pocet,0) +
IFNULL(minusjeden.pocet,0) +
IFNULL(minusdva.pocet,0) +
IFNULL(minustri.pocet,0) +
IFNULL(minusstyri.pocet,0) +
IFNULL(minuspat.pocet,0) +
IFNULL(minussest.pocet,0) +
IFNULL(minussedem.pocet,0) +
IFNULL(minusosem.pocet,0) +
IFNULL(minusdevat.pocet,0) +
IFNULL(minusdesat.pocet,0) +
IFNULL(minusjedenast.pocet,0) +
IFNULL(minusdvanast.pocet,0)/13
) as priemer
FROM users
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())
GROUP BY id_uzivatela ) tento on tento.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-1
GROUP BY id_uzivatela ) minusjeden on minusjeden.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-2
GROUP BY id_uzivatela ) minusdva on minusdva.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-3
GROUP BY id_uzivatela ) minustri on minustri.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-4
GROUP BY id_uzivatela ) minusstyri on minusstyri.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-5
GROUP BY id_uzivatela ) minuspat on minuspat.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-6
GROUP BY id_uzivatela ) minussest on minussest.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-7
GROUP BY id_uzivatela ) minussedem on minussedem.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-8
GROUP BY id_uzivatela ) minusosem on minusosem.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-9
GROUP BY id_uzivatela ) minusdevat on minusdevat.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-10
GROUP BY id_uzivatela ) minusdesat on minusdesat.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-11
GROUP BY id_uzivatela ) minusjedenast on minusjedenast.id_uzivatela = user_id
LEFT JOIN (
SELECT id_uzivatela, COUNT(body) as pocet
FROM bodovanie
JOIN vina ON id_vino = id_vino_b
WHERE MONTH(date) = MONTH(CURRENT_DATE())-12
GROUP BY id_uzivatela ) minusdvanast on minusdvanast.id_uzivatela = user_id
WHERE
(
IFNULL(tento.pocet,0) +
IFNULL(minusjeden.pocet,0) +
IFNULL(minusdva.pocet,0) +
IFNULL(minustri.pocet,0) +
IFNULL(minusstyri.pocet,0) +
IFNULL(minuspat.pocet,0) +
IFNULL(minussest.pocet,0) +
IFNULL(minussedem.pocet,0) +
IFNULL(minusosem.pocet,0) +
IFNULL(minusdevat.pocet,0) +
IFNULL(minusdesat.pocet,0) +
IFNULL(minusjedenast.pocet,0) +
IFNULL(minusdvanast.pocet,0)
) != 0
");
foreach ($dvanastmesiace as $mesiace) {
echo '<tr>
<td>'.$mesiace[1].'</td>'.
'<td>'.$mesiace[2].'</td>'.
'<td>'.$mesiace[3].'</td>'.
'<td>'.$mesiace[4].'</td>'.
'<td>'.$mesiace[5].'</td>'.
'<td>'.$mesiace[6].'</td>'.
'<td>'.$mesiace[7].'</td>'.
'<td>'.$mesiace[8].'</td>'.
'<td>'.$mesiace[9].'</td>'.
'<td>'.$mesiace[10].'</td>'.
'<td>'.$mesiace[11].'</td>'.
'<td>'.$mesiace[12].'</td>'.
'<td>'.$mesiace[13].'</td>'.
'<td>'.$mesiace[13].'</td>
</tr>';
}
?>
</table>