Code: Show last 5 months (with number of messages written in that month) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Code: Show last 5 months (with number of messages written in that month) (/showthread.php?tid=21980) |
Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]Callista[/eluser] Hi, for my job i'm making a news/blog page. on the top it show 3 tables. first table had 6 titles of the last 6 messages. second table has 5 months in it with the number of messages written in that month behind it Last table has de 6 most populair tags. I got every thing working but the code for the months is a little bit big and messy. So i was wandering if anyone had an idea how i could get this cleaner. CONTROLLER Code: function template_2($pagina_ID) MODEL Code: function count_month_01($month_01, $this_year) VIEUW Code: <div class="nieuwslist"> This is just to much code for a simpel function but i have no idea or inspiration for a better construction. Anyone have an idea? Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]n0xie[/eluser] Wow that's a lot of redundant code :O First off, why is there 5 functions in your model that do exactly the same thing? You can replace all that with just one generic function 'count_month' where you give an 2 variables, the month, and the year. Alternatively you could grab all 5 months in one go and just loop over them in the view. Also you have written a function to translate the months to dutch. PHP can do this automagically for you so you don't have to think about it. My proposal is: Collect all the data in 1 array. Then in your view, loop over the array and format them accordingly. You are now putting presentation (the way it looks) logic in your controller, which will be a pain if you ever want to change it. Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]Callista[/eluser] [quote author="n0xie" date="1251295789"]Wow that's a lot of redundant code :O First off, why is there 5 functions in your model that do exactly the same thing? You can replace all that with just one generic function 'count_month' where you give an 2 variables, the month, and the year. Alternatively you could grab all 5 months in one go and just loop over them in the view. Also you have written a function to translate the months to dutch. PHP can do this automagically for you so you don't have to think about it. My proposal is: Collect all the data in 1 array. Then in your view, loop over the array and format them accordingly. You are now putting presentation (the way it looks) logic in your controller, which will be a pain if you ever want to change it.[/quote] First: The months are in numbers in my database. That's why i had the translation code. To translate them from month numbers to month names. how would you let php do this automatically? Second: How do you get the last five months in one loop? because i had to calculate them moth by month i needed the model function month by month. If i can calculate them in one function i would be really glad but i couldn't get it done. Because i have to look at the years as well. That way if it's January 2010. The last 5 months are January 2010, December 2009, November 2009 etc. etc. Maybe i just got to lost in all my codes but i have no clue how to make this smaller and i really want it smaller because like you said it looks like a lot of redundant code. Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]n0xie[/eluser] [quote author="Callista" date="1251296570"] First: The months are in numbers in my database. [/quote] How do you store them in your table? As integers or as date? Take a look at the date() function in php. It will take an timestamp and format it any way you want. Quote:Second:This is a rather crude example but it will show you how to build an array for the last 5 months. How you fill that array is up to you. Code: function voorbeeldje() As you can see, you can feed the function a MySQL date, which is easy to produce with the date() function. Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]Callista[/eluser] yikes! ok i'll have to study that code. I'm learning programming pretty fast but i'm still not that good in programming that i instantly understand codes haha you asked about my database: Right now i have 3 seperates fields in my nieuws_table for the days, months and years. This so i could select news on a month. These fields are simpel varchars. With the date function it produces a whole date (right?) not just a month. So i thought i couldn't use that. But i'll take a look at the codes. I like your solution for the year problem with the if($month == 0). Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]jedd[/eluser] You may want to read up on the MySQL (I'm assuming you're using this DB) reference manual at the options for date/time types. Rather than using 3 x columns for managing years, months, days - you could use something like the DATE type for just one column. If you want to keep the time of the posting (?) as well, you could look at DATETIME. Happily lots of people have already come before you with these problems, so SQL and PHP have all kinds of functions for showing the difference between two dates, selecting X months ago (even when it spans new-year borders) and so on. Anyhoo, I suspect this would make your life much easier in the long term. Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]n0xie[/eluser] Since you're new to programming I'll try to get you on your way. It could be written cleaner but hopefully you can follow the actions this way: CONTROLLER Code: $today = date('Y/m/d'); MODEL Code: function get_last_months($startdate = NULL, $number_of_months = 5) VIEW Code: <div class="nieuwslist"> If your locale is set correctly, PHP should translate the month names automatically for you. Hope this helps and I hope this was what you were looking for Code: Show last 5 months (with number of messages written in that month) - El Forum - 08-26-2009 [eluser]Callista[/eluser] This is great!! thank you soo much!! i'm hitting myself now that i didn't think of a solution like this. Sometime i just over think codes and get a little lost haha. I make it more complicated then necessary. |