rumpels Posted October 21, 2004 Report Share Posted October 21, 2004 Sadomāju izveidot universaalu arhīva skriptu, tobish ja ir kaut kada tabula kurā ir DATE ir iespējams tos sagrupēt pēc gada un mēneša. Problēma ir tajā ka otrajā selektā nevar uzrādīt ka izvada tikai WHERE year = '$gads' līdz ar to ir ķeza un izvada visus mēnešus kuri pastāv tabulā pie abiem gadiem un gadi visur ir vienādi. Vai ir kādas idejas kā būtu labāk konstruēt, ko esmu nepareizi izštukojis? <html> <head> <title>arhīvs</title> <?php mysql_connect ("localhost", "user", "pass"); mysql_select_db("database"); ?> <meta http-equiv="Content-Type" content="text/html; charset=windows-1257"> </head> <body> <? $query = mysql_query("SELECT *,YEAR(datums) AS year FROM tabula GROUP BY year ORDER BY year DESC"); while($rinda = mysql_fetch_assoc($query)) { $gads = $rinda["year"]; echo $gads.". gads<br>"; $query2 = mysql_query("SELECT *,YEAR(datums) AS year,MONTH(datums) AS month,DAYOFWEEK(datums) AS day FROM tabula GROUP BY month"); while($rinda2 = mysql_fetch_assoc($query2)) { $months = array("", "Janvāris", "Februāris", "Marts", "Aprīlis", "Maijs", "Jūnijs", "Jūlijs", "Augusts", "Septembris", "Oktobris", "Novembris", "Decembris"); $dien = array("Svētdiena", "Pirmdiena", "Otrdiena", "Trešdiena", "Ceturtdiena", "Piektdiena", "Sestdiena"); $die = $rinda2["day"]; $men = $rinda2["month"]; $gad = $rinda2["year"]; echo " <a href=\"?month=".$men."&year=".$gad."\">".$months[$men]."</a><br>"; } } ?> </body> </html> Link to comment Share on other sites More sharing options...
рпр Posted October 21, 2004 Report Share Posted October 21, 2004 pēc topika spriežot problēmu varētu atrisināt šādi: YEAR(datums) AS year...WHERE YEAR(datums) = '2004' Link to comment Share on other sites More sharing options...
Venom Posted October 21, 2004 Report Share Posted October 21, 2004 v/b problēma tajā, ka rakstot year kā kolonnas vārdu tu izmanto mysql iekšējo komandu (resp. mysql domā ka tā ir funkcija un gaida, ka tur būs "("), lai izmantotu šādus kolonnu nosaukumus, tie jāietver backtickos ` (parasti zem esc, pirms 1) select year(datums) as `year` WHERE `year`=YEAR(NOW()) select DISTINCT `group` as gruppa GROUP BY `group` et cetera Link to comment Share on other sites More sharing options...
Venom Posted October 21, 2004 Report Share Posted October 21, 2004 v/b problēma tajā, ka rakstot year kā kolonnas vārdu tu izmanto mysql iekšējo komandu (resp. mysql Link to comment Share on other sites More sharing options...
rumpels Posted October 21, 2004 Author Report Share Posted October 21, 2004 sql rāda sintakses kljudu SELECT *,YEAR(datums) AS `YEAR` WHERE `YEAR` = YEAR(NOW()) FROM tabula Vai versija šeit var būt vainīga, domājams ka nē, bet kādēļ pieprasījumā uzrāda kļūdu? Link to comment Share on other sites More sharing options...
Venom Posted October 21, 2004 Report Share Posted October 21, 2004 SELECT *,YEAR(datums) AS `YEAR` FROM tabula WHERE `YEAR` = YEAR(NOW()) Link to comment Share on other sites More sharing options...
rumpels Posted October 21, 2004 Author Report Share Posted October 21, 2004 (edited) sorry par to sintaksi esmu laikam parnjemts un kljudos aizvien vairak, bet sql tiesham nenjem preti sho vaicajumu.. SELECT *,YEAR(datums) AS `gads`,MONTH(datums) AS `month`,DAYOFWEEK(datums) AS `day` FROM tabula WHERE `gads` = YEAR('2003') GROUP BY `month` #1054 - Unknown column 'gads' in 'where clause' Edited October 21, 2004 by rumpels Link to comment Share on other sites More sharing options...
Venom Posted October 21, 2004 Report Share Posted October 21, 2004 nu tad arī izmanto WHERE YEAR(datums) = 2003 Link to comment Share on other sites More sharing options...
rumpels Posted October 21, 2004 Author Report Share Posted October 21, 2004 thanks viss izdevās paldies par palīdzību, ja kādam apmeklētājam interesē beigu var. http://paste.php.lv/1301 Link to comment Share on other sites More sharing options...
Venom Posted October 21, 2004 Report Share Posted October 21, 2004 neskaitot neprātīgu " izmantošanu, vēlos atzīmēt variantu while(...) { $months = array("", "Janvāris", "Februāris",...); $dien = array("Svētdiena", ..., "Sestdiena"); } - sanāk ka katrā ciklā tev tie masīvi tiek rādīti no jauna. lai pataupītu RAMu: $months = array("", "Janvāris", "Februāris",...); $dien = array("Svētdiena", ..., "Sestdiena"); while(...) { $month un $dien šeit jau pieejami } Link to comment Share on other sites More sharing options...
bubu Posted October 21, 2004 Report Share Posted October 21, 2004 (edited) Ek, ka cilvēkiem joprojām patīk eskeipoties: echo " <a href=\"?month=".$men."&year=".$gad."\">".$months[$men]."</a><br>" Nebūtu vienkāršāk: echo ' <a href="?month='.$men.'&year='.$gad.'">'.$months[$men].'</a><br>' vai pat: echo " <a href='?month=$men&year=$gad'>".$months[$men].'</a><br>' Un kāda jēga vispār no ši a hrefa? Tas tak netiek nekur izmantots, ne? Vai arī viss šis ir tikai fragments no lielāka koda gabala? Edited October 21, 2004 by bubu Link to comment Share on other sites More sharing options...
rumpels Posted October 21, 2004 Author Report Share Posted October 21, 2004 Oki izveidošu beigu gabalu ka skriptu un iemetishu arhivetu ar sql datiem un bus rum_arhiver_v_1.0:) Link to comment Share on other sites More sharing options...
Venom Posted October 21, 2004 Report Share Posted October 21, 2004 echo ' <a href="?month='.$men.'&year='.$gad.'">'.$months[$men].'</a><br />' vairāk piekrītu šim variantam, jo kaučkā pierastāk redzēt noģenerētā htmlā atributūtus iekš "" SQL kvērijiem gan izmantoju " ", jo tur stringus jānodod ar ' ' (izņemot gadījumus ar 'DELETE FROM TABLE '.$table); Link to comment Share on other sites More sharing options...
Recommended Posts