PHP MYSQL Blog Archive Menu by Year and Month - php

I'm looking for an efficient way to collate all blog posts into a menu of the following format:
2012
August(6)
September(4)
October(2)
Month representing the month(obviously), and the value inside the brackets representing the number of posts in that month. Once clicked, a search will then be made for all posts in that month, in that year.
I need it to be dynamic, picking up November automatically when a post is created in that month, and carrying on into December, into 2013 etc etc...
All I have is a UNIX timestamp for each post. I would really like to avoid using seperate functions to gather endless comlex arrays etc.
Any help much appreciated.

From your question, I understand you're trying to come up with a query to group a number of elements by month and year. The following should do the trick:
SELECT
YEAR(dateField) AS YEAR,
MONTH(dateField) AS MONTH,
COUNT(*) AS TOTAL
FROM table
GROUP BY YEAR, MONTH
Obviously, "dateField" being the name of your datetime/timestamp column and "table" being the name of your table.
More information on the GROUP BY clause and aggregate functions (such as the COUNT(*) function used above) here.

Related

show records from sql created during specific week

I have tons of records that are in my database table leadactivity basically I need to display all the records that were created in the first week of the current month, then also another query to display records in the second week, same for third week and then finally the fourth week of the current month.
I have a column called created_date which onupdate puts in the current timestamp
What is the best way to achieve this?
You can use date functions for this:
select la.*
from leadactivity la
where day(la.created_date) between 1 and 7 and
created_date >= curdate() + (1 - day(curdate())) day;
The above assumes MySQL syntax. Most databases have similar functionality, although the specific functions may differ.

displaying the results of a query on an invoices table month by month, year by year

I have an invoices table in my database, one of the fields of which is a created date.
Users can enter a date range and the query will get results from between the range given. I want to then go through the results and display them so that all the results from, for example, jan 2013 are in a table together (pref with totals for that table), feb 2013 invoices are all in a table together and so on across years if requested.
I've never had to do something like this before and I'm trying to figure out the best way to do this.
Has anyone had to deal with this before and if so what was the best method to do so?
I have done the same thing in my rails project.
1st you fetch records 'group by invoice_year invoice_month'
then i built a hash(or any data structure you prefer) and add data corresponding to that year and month. Then use this to build your table
example: range is jan-2013 to present day
invoice_data = {'2013' => { 'jan' => {some data}, 'feb' => {some data}...}, '2014' => {'jan' {some data}}}
you have to format the query data and build such data structure.
look at extract method in sql.
Updated answer since you might not know the first part...do the hard part directly in you SQL instead of some massive php loop. Select whatever you want to display and end the query with:
WHERE invoice_date BETWEEN '$start_date' AND '$end_date' GROUP BY YEAR(invoice_date), MONTH(invoice_date)
You can pass the 2 php variables through php from the form your users use to enter the date range. Make sure you quote them if you're using DATETIME format and getting it through datepicker for example. Then it should be a piece of cake to go from there with php and display it the way you want...
You should have a look at the SQL GROUP BY Statement ( http://www.w3schools.com/sql/sql_groupby.asp ).
Assuming you have the totals of the invoices in a field called invoice_total and that your invoice date is in a field called invoice_date the following SQL statement could be used:
SELECT sum(invoice_total), year, month
FROM invoices
GROUP BY YEAR(invoice_date) as year, MONTH(invoice_date) as month

Pull three upcoming events from MySQL database

I have a database of events that are used to fill a PHP calender. I want to also have a page that lists three upcoming events in date order when ever the page loads. The records have a separate field for day, month and year. The issue i run in to is getting records that accrue after the current date.
Table structure will be good to have but I will guess the query that you need. Use that as a point to start from.
SELECT * FROM events WHERE CONCAT(year,'-',month,'-',date) > DATE_FORMAT(NOW(),'%Y-%m-%d')
ORDER BY year DESC, month DESC, date DESC LIMIT 3;
You are going to have to build a date from your three fields to test against current date, or split current date into year, month, day and test all three against data in your table.
Got to say, not a brill decision on someones part to split date like that.
Need table structure before we can properly help query wise.

How to sort data depending on what day it was posted?

How do you sort data which was stored in a mysql database depending on the days of the week in which the data was submited ??
I basically want to create a diary which outputs information in each day of the week depending on what day it was posted by dates so,
Mon - Data in order of date
Tue -
Wed - e.t.c
Any code examples and information will be great, thanks.
You can do a
SELECT DAYOFWEEK(datehere) as dayofweek, datehere FROM something ORDER BY dayofweek, datehere;
You can use the DAYOFWEEK function to extract the day, and then sort on it just like any other data.
What kinf of data type is the column where you store the date submission?
It seems like you're asking for a basic SELECT statement?
SELECT some_column, another_colum FROM your_table ORDER BY your_date_column DESC
This assumes you actually have a column that logs the insertion timestamp.
If this answer is obnoxiously simplistic, please forgive me...and give us more details :)
Regards.
If your data is stored as a DATE or DATETIME field, use the DAYOFWEEK or DATE_FORMAT functions to turn it into day name for output, but continue to order by the DATE field
SELECT DATE_FORMAT(my_date_column, '%W') AS dayofweek
FROM my_table
ORDER BY my_date_column
Well, the sorting bit is easy, just sort on the column that represents the post's date. The grouping in days is something you can do in your code, since you need to check the date there anyway (for post-processing the actual output).
To put it this way, you can do a subselect to get the specific day of the week, but in your code you would have to check the day again to group posts per day. In that case it's better (and cleaner, since you're separating business logic from data) to do this in your code, something like this:
select all posts (within date range)
make an associative array, with the
days as keys, and posts (in new
arrays) as values
loop through the
days and then posts to output the
posts per day
SELECT *
FROM diary_entries
ORDER BY FIELD(DAYOFWEEK(created), '2, 3, 4, 5, 6, 7, 1'), created
DAYOFWEEK grabs day of the week number (1 = Sunday).
FIELD makes Monday first day of the week.
After sorting by day of week, then sorted by date created.

Archive Builder PHP

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";

Categories