noj75 Posted April 3, 2010 Share Posted April 3, 2010 Hi Guys, Im sure there is a way to simplify this into one query or something. Anyone have any ideas: <?php $mqry1 = "SELECT * FROM $blog WHERE month = 'January'"; $mqry2 = "SELECT * FROM $blog WHERE month = 'February'"; $mqry3 = "SELECT * FROM $blog WHERE month = 'March'"; $mqry4 = "SELECT * FROM $blog WHERE month = 'April'"; $mqry5 = "SELECT * FROM $blog WHERE month = 'May'"; $mqry6 = "SELECT * FROM $blog WHERE month = 'June'"; $mqry7 = "SELECT * FROM $blog WHERE month = 'July'"; $mqry8 = "SELECT * FROM $blog WHERE month = 'August'"; $mqry9 = "SELECT * FROM $blog WHERE month = 'September'"; $mqry10 = "SELECT * FROM $blog WHERE month = 'October'"; $mqry11 = "SELECT * FROM $blog WHERE month = 'November'"; $mqry12 = "SELECT * FROM $blog WHERE month = 'December'"; $mres1 = mysql_query($mqry1); $mres2 = mysql_query($mqry2); $mres3 = mysql_query($mqry3); $mres4 = mysql_query($mqry4); $mres5 = mysql_query($mqry5); $mres6 = mysql_query($mqry6); $mres7 = mysql_query($mqry7); $mres8 = mysql_query($mqry8); $mres9 = mysql_query($mqry9); $mres10 = mysql_query($mqry10); $mres11 = mysql_query($mqry11); $mres12 = mysql_query($mqry12); $cmres1 = mysql_num_rows($mres1); $cmres2 = mysql_num_rows($mres2); $cmres3 = mysql_num_rows($mres3); $cmres4 = mysql_num_rows($mres4); $cmres5 = mysql_num_rows($mres5); $cmres6 = mysql_num_rows($mres6); $cmres7 = mysql_num_rows($mres7); $cmres8 = mysql_num_rows($mres8); $cmres9 = mysql_num_rows($mres9); $cmres10 = mysql_num_rows($mres10); $cmres11 = mysql_num_rows($mres11); $cmres12 = mysql_num_rows($mres12); echo '<ul id="monthul"> <li><a href="index.php?month=January">January</a> ('.$cmres1.')</li> <li><a href="index.php?month=February">February</a> ('.$cmres2.')</li> <li><a href="index.php?month=March">March</a> ('.$cmres3.')</li> <li><a href="index.php?month=April">April</a> ('.$cmres4.')</li> <li><a href="index.php?month=May">May</a> ('.$cmres5.')</li> <li><a href="index.php?month=June">June</a> ('.$cmres6.')</li> <li><a href="index.php?month=July">July</a> ('.$cmres7.')</li> <li><a href="index.php?month=August">August</a> ('.$cmres8.')</li> <li><a href="index.php?month=September">September</a> ('.$cmres9.')</li> <li><a href="index.php?month=October">October</a> ('.$cmres10.')</li> <li><a href="index.php?month=November">November</a> ('.$cmres11.')</li> <li><a href="index.php?month=December">December</a> ('.$cmres12.')</li> </ul>'; ?> Any help would be appreciated. Regards Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/ Share on other sites More sharing options...
ignace Posted April 3, 2010 Share Posted April 3, 2010 month IN ('January', 'February', ...) Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036459 Share on other sites More sharing options...
Lukeidiot Posted April 3, 2010 Share Posted April 3, 2010 Hi Guys, Im sure there is a way to simplify this into one query or something. Anyone have any ideas: <?php $mqry1 = "SELECT * FROM $blog WHERE month = 'January'"; $mqry2 = "SELECT * FROM $blog WHERE month = 'February'"; $mqry3 = "SELECT * FROM $blog WHERE month = 'March'"; $mqry4 = "SELECT * FROM $blog WHERE month = 'April'"; $mqry5 = "SELECT * FROM $blog WHERE month = 'May'"; $mqry6 = "SELECT * FROM $blog WHERE month = 'June'"; $mqry7 = "SELECT * FROM $blog WHERE month = 'July'"; $mqry8 = "SELECT * FROM $blog WHERE month = 'August'"; $mqry9 = "SELECT * FROM $blog WHERE month = 'September'"; $mqry10 = "SELECT * FROM $blog WHERE month = 'October'"; $mqry11 = "SELECT * FROM $blog WHERE month = 'November'"; $mqry12 = "SELECT * FROM $blog WHERE month = 'December'"; $mres1 = mysql_query($mqry1); $mres2 = mysql_query($mqry2); $mres3 = mysql_query($mqry3); $mres4 = mysql_query($mqry4); $mres5 = mysql_query($mqry5); $mres6 = mysql_query($mqry6); $mres7 = mysql_query($mqry7); $mres8 = mysql_query($mqry8); $mres9 = mysql_query($mqry9); $mres10 = mysql_query($mqry10); $mres11 = mysql_query($mqry11); $mres12 = mysql_query($mqry12); $cmres1 = mysql_num_rows($mres1); $cmres2 = mysql_num_rows($mres2); $cmres3 = mysql_num_rows($mres3); $cmres4 = mysql_num_rows($mres4); $cmres5 = mysql_num_rows($mres5); $cmres6 = mysql_num_rows($mres6); $cmres7 = mysql_num_rows($mres7); $cmres8 = mysql_num_rows($mres8); $cmres9 = mysql_num_rows($mres9); $cmres10 = mysql_num_rows($mres10); $cmres11 = mysql_num_rows($mres11); $cmres12 = mysql_num_rows($mres12); echo '<ul id="monthul"> <li><a href="index.php?month=January">January</a> ('.$cmres1.')</li> <li><a href="index.php?month=February">February</a> ('.$cmres2.')</li> <li><a href="index.php?month=March">March</a> ('.$cmres3.')</li> <li><a href="index.php?month=April">April</a> ('.$cmres4.')</li> <li><a href="index.php?month=May">May</a> ('.$cmres5.')</li> <li><a href="index.php?month=June">June</a> ('.$cmres6.')</li> <li><a href="index.php?month=July">July</a> ('.$cmres7.')</li> <li><a href="index.php?month=August">August</a> ('.$cmres8.')</li> <li><a href="index.php?month=September">September</a> ('.$cmres9.')</li> <li><a href="index.php?month=October">October</a> ('.$cmres10.')</li> <li><a href="index.php?month=November">November</a> ('.$cmres11.')</li> <li><a href="index.php?month=December">December</a> ('.$cmres12.')</li> </ul>'; ?> Any help would be appreciated. Regards Tada! $sql = mysql_query("SELECT * FROM $blog"); echo "<ul id='monthul'>"; while($months = mysql_fetch_assoc($sql)){ echo "<li><a href="index.php?month=$months[month]">$months[month]</a></li>"; } echo "</ul>"; Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036460 Share on other sites More sharing options...
premiso Posted April 3, 2010 Share Posted April 3, 2010 You should really not store month separately. I would use a DATE field or even a DATETIME field to store the data. This way you can easily order by datefield and it will come out in the proper order and you do not have to worry about modifying the display information. Then if you just want the month you use MySQL's DATE_FORMAT to format the datefield when it comes out of the database. Just some food for thought. Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036465 Share on other sites More sharing options...
noj75 Posted April 3, 2010 Author Share Posted April 3, 2010 Tada! $sql = mysql_query("SELECT * FROM $blog"); echo "<ul id='monthul'>"; while($months = mysql_fetch_assoc($sql)){ echo "<li><a href="index.php?month=$months[month]">$months[month]</a></li>"; } echo "</ul>"; That will only display the months that contain data. I need to display Jan through to Dec and numrically display how many entries that month has even if it is zero. Thanks for that but any other ideas. Kind regards Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036471 Share on other sites More sharing options...
litebearer Posted April 3, 2010 Share Posted April 3, 2010 just a thought hardlt fully formed yet (psuedo code - someone smater than me could probably state it correctly)... count as 1 where month = january count as 2 where month = feb etc etc I suspect that this could be combined into one query then to display (1) have an array of the name of the months (2) loop thru the array displaying the links as with the corresponding values gatherd in the query A. As there is no need for anything else fromm the db at this stage, it inscreases speed adn reduces server usage B. simplifies the code Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036477 Share on other sites More sharing options...
jcbones Posted April 3, 2010 Share Posted April 3, 2010 My take on this problem: <?php $January = 0; $February = 0; $March = 0; $April = 0; $May = 0; $June = 0; $July = 0; $August = 0; $September = 0; $October = 0; $November = 0; $December = 0; $sql = "SELECT * FROM `$blog`"; $result = mysql_query($sql); if(mysql_num_rows($result) > 0) { while($r = mysql_fetch_assoc($result)) { @$$r['month'] += 1; } echo '<ul id="monthul"> <li><a href="index.php?month=January">January</a> ('.$January.')</li> <li><a href="index.php?month=February">February</a> ('.$February.')</li> <li><a href="index.php?month=March">March</a> ('.$March.')</li> <li><a href="index.php?month=April">April</a> ('.$April.')</li> <li><a href="index.php?month=May">May</a> ('.$May.')</li> <li><a href="index.php?month=June">June</a> ('.$June.')</li> <li><a href="index.php?month=July">July</a> ('.$July.')</li> <li><a href="index.php?month=August">August</a> ('.$August.')</li> <li><a href="index.php?month=September">September</a> ('.$September.')</li> <li><a href="index.php?month=October">October</a> ('.$October.')</li> <li><a href="index.php?month=November">November</a> ('.$November.')</li> <li><a href="index.php?month=December">December</a> ('.$December.')</li> </ul>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036505 Share on other sites More sharing options...
noj75 Posted April 4, 2010 Author Share Posted April 4, 2010 My take on this problem: <?php $January = 0; $February = 0; $March = 0; $April = 0; $May = 0; $June = 0; $July = 0; $August = 0; $September = 0; $October = 0; $November = 0; $December = 0; $sql = "SELECT * FROM `$blog`"; $result = mysql_query($sql); if(mysql_num_rows($result) > 0) { while($r = mysql_fetch_assoc($result)) { @$$r['month'] += 1; } echo '<ul id="monthul"> <li><a href="index.php?month=January">January</a> ('.$January.')</li> <li><a href="index.php?month=February">February</a> ('.$February.')</li> <li><a href="index.php?month=March">March</a> ('.$March.')</li> <li><a href="index.php?month=April">April</a> ('.$April.')</li> <li><a href="index.php?month=May">May</a> ('.$May.')</li> <li><a href="index.php?month=June">June</a> ('.$June.')</li> <li><a href="index.php?month=July">July</a> ('.$July.')</li> <li><a href="index.php?month=August">August</a> ('.$August.')</li> <li><a href="index.php?month=September">September</a> ('.$September.')</li> <li><a href="index.php?month=October">October</a> ('.$October.')</li> <li><a href="index.php?month=November">November</a> ('.$November.')</li> <li><a href="index.php?month=December">December</a> ('.$December.')</li> </ul>'; ?> Jcbones you are a star !! Works a treat thank you very much !! Out of curiosity what is the extra @$ for before @$$r['month'] Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036659 Share on other sites More sharing options...
jcbones Posted April 4, 2010 Share Posted April 4, 2010 The @ suppresses any errors or notices that you may get. Originally I was't pre-declaring the variable, so you would get a notice. It is not needed now. The $$ = http://php.net/manual/en/language.variables.variable.php Quote Link to comment https://forums.phpfreaks.com/topic/197468-simplify-a-code-snippet/#findComment-1036873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.