I make a page, that display records from database,in group of 10 records per page using paginition concept,
i wanna print Like: Month name/Year.
That month and year is distinct form that page means of 10 records.
Suppose page number 10 contains 10 records with 3 distinct month named Dec 2011 , Jan 2012 and, March 2012,
In this case i want to print Month: Dec/2011 ,Jan,March/2012
Thanks in advance..
If your date is stored like you write it: Dec 2011, Jan 2012 then you should do like this
$arr = array();
$sql = mysql_query(__YOUR_SQL__);
while($row = mysql_fetch_array($sql)) {
$date = explode(" ", $row['date']);
if(!array_key_exists($date[1], $arr))
$arr[$date[1]] = array();
array_push($arr[$date[1]], $date[0]);
}
foreach($arr as $key => $value) {
if(count($value) == 1)
echo $value[0] . '/' . $key;
else
echo implode(", ", $value) . ' ' . $key;
}
Related
I hope I can be clear enough.
I'm retrieving dates from database (day only). I have a loop and I want to print something like that:
1 april
1 april
2 april
3 april
3 april
3 april
etc...
So : same days = no <br> tag, different days print a <br>.
My actual code:
$prec;
foreach ($list as $rs) {
$day = date("d", strtotime($rs['date']))." ";
if ($day === $prec) {
$space = "";
} else {
$space ="<br>";
}
$prec = $day;
echo $day;
echo date("F", strtotime($rs['date'])).", ";
echo $space;
}
But this output is wrong:
23 March
25 March
27 March
27 March
27 March
27 March
27 March
28 March
28 March
28 March
02 April
03 April
04 April
What am I missing?
The problem is the placement of where you output the line break - if you output the line break before the date rather than after, the output will be as intended.
echo $space;
echo $day;
echo date("F", strtotime($rs['date'])).", ";
That is because your comparison is based on how the current value compares to the previous one, so the generated result needs to be placed between these two days to follow the same logic
Keep in mind that your code will still return wrong output if you happen to have the same day in a separate month next to each other, because you only compare by day (if you compare by the full date, it will work better)
you have to echo $space before echo $day
//same days = no <br> tag, different days print a <br>
$list = [1, 3, 3, 3,4 ];
$prec ='';
foreach ($list as $day) {
if ($day === $prec) {
$space = ",";
} else {
$space ="<br>";
}
echo $space;
echo $day;
$prec = $day;
}
Result :
<br>1<br>3,3,3<br>4
I have got this results of a query:
(book_id, yr)
37 2014
37 2012
35 2013
35 2012
35 2011
I would like to echo it like this
book id is 37 from 2014
editions available are 2014 and 2012
One way would be to group them inside a container, then just use min and max:
// grouping
$container = array();
while($row = your_fetch_assoc($result_set)) {
$container[$row['book_id']][] = $row['yr'];
}
// then comes the presentation
foreach($container as $book_id => $year) {
$to = max($year);
$from = min($year);
echo "
book id is {$book_id} <br/>
editions available are {$to} and {$from} <br/><br/>
";
}
$years = array();
$id = 0;
foreach($row as $r){
if( $r['book_id'] != $id && $id != 0){
echo "book id is ".$id." editions available are ".implode('and ', $years);
$years = array();
$id = 0;
}
$id = $r['book_id'];
$years[] = $r['yr'];
}
echo "book id is ".$id." editions available are ".implode('and ', $years);
with that you iterate over you books and collect the years. Every time you find a new ID you echo the old entry with all the years you collectet. In the end you echo the last entry aswell.
I am creating an archives navigation on my website and have found a few posts regarding how to do it and have pulled together some code, but can't seem to get it to work.
I want it to display like:
January 2015 (9)
December 2014 (8)
November 2014 (10)
October 2014 (15)
...
Here's the code :
$sql = "SELECT YEAR(timestamp_published) AS 'year', Month(timestamp_published) AS 'month', COUNT(id) AS 'count' FROM table GROUP BY YEAR(timestamp_published), MONTH(timestamp_published) DESC";
$data = $db->query($sql);
while($row = $db->fetch_assoc()) {
$data[$row['year']][$row['month']] = $row['count'];
}
foreach ($data as $year => $months) {
echo archive_date($month).' ';
foreach ($months as $month => $count) {
echo $year.' ('.$count.') (' . $data->id . ')<br/>';
}
}
}
Thank you! I ended up solving my own issue. I was building this in a class and there were a couple of issues. I've got the code working now, here it is for anyone in the future.
$sql = "SELECT YEAR(timestamp_published) AS 'year', MONTH(timestamp_published) AS 'month', COUNT(`publish`) AS 'count' FROM ".static::$table_name." GROUP BY YEAR(timestamp_published) DESC, MONTH(timestamp_published) ASC";
$result = $db->query($sql);
$data = array();
while($row = $result->fetch_assoc()) {
$data[$row['year']][$row['month']] = $row['count'];
}
foreach ($data as $year => $months) {
echo '<strong>'.$year.'</strong><br/><br/>';
foreach ($months as $month => $count) {
$dateObj = DateTime::createFromFormat('!m', $month);
$monthName = $dateObj->format('F'); // March
echo $monthName . ' ('.$count.')<br/>';
}
echo '<hr/>';
}
It actually comes out looking like this:
2015
January (3)
February (5)
March (6)
...
2014
January (7)
February (9)
...
I think you are looking to get the string representation of the month (bit unclear) but that can be achieved by using date() like so
date ("F", $month)
so in your case
echo date ("F", $month) . ' ' . $year.' ('.$count.')<br/>';
In the database I have various dates stored like so:
ID| Dates
-----------------
1 | 23 April 2014
2 | 24 April 2014
3 | 25 April 2014
4 | 01 May 2014
5 | 02 May 2014
I managed to trim all the digits from the string with PHP which only leaves me with the month titles.
What I am trying to accomplish is to loop the months with a While loop and only show each month only once.
I want to achieve the following:
<h1>April<h1>
<p>23 April 2014</p>
<p>24 April 2014</p>
<p>25 April 2014</p>
<h1>May</h1>
<p>01 May 2014</p>
<p>02 May 2014</p>
So, what I actually want is to dynamically sort the months under its rightful tag
I hope somebody can help
Thank you
If your are query ascending by date, you can achieve your requirement by following example:
<?php
$results = array();// your dataset
$month_name = array(); // empty array
foreach($results as $row){
$this_month = date('M', strtotime($row['date_field']));
if (!in_array($this_month, $month_name)) {
$month_name[] = $this_month;
echo "<h1>{$this_month}</h1>";
}
echo "<p>{$row['date_field']}</p>";
}
I believe there are more better solution can be possible (by change database data field as date instead of keep date as string), but from your current scenario, this solution I think perfect.
******EDIT******
Updated code base on your provided example:
$month_name = array();
$result = mysql_query($sql);
while ($record = mysql_fetch_assoc($result)) {
// $record['date_field'] is your dates field of your database
$this_month = date('M', strtotime($record['date_field']));
if (!in_array($this_month, $month_name)) {
$month_name[] = $this_month;
echo "<h1>{$this_month}</h1>";
}
echo "<li> <a href='dates.php?ID=" . $record['ID'] . "'>";
echo "<p><strong>" . $record['date'] . " | " . $record['timeStart'] . " - " . $record['timeEnd'] . "</strong></p>";
echo "<p>" . $record['place'] . " - " . $record['provincie'] . "</p>";
echo "<p>" . $record['kind'] . "</p>";
echo "</a> </li>";
}
my database table is something like :
id year month
1 2011 november
2 2011 november
3 2011 october
i need to create a query so it return something like that :
2011
november
november
october
What is the correct query syntax to do it in php script ?
Here is the code i used :
<?php
$uid = $_SESSION['uid'];
$sql = "SELECT year, GROUP_CONCAT(month) AS months FROM articles GROUP BY year";
$res = mysql_query ($sql) or die (mysql_error());
if (mysql_num_rows($res) > 0) {
while ($rows = mysql_fetch_assoc($res)) {
foreach ($rows AS $row) {
echo $row['year'] . "<br>";
$months = explode(",", $row['months']);
foreach ($months AS $m) {
echo $m . "<br>";
}
}
}
}
?>
You can use GROUP_CONCAT() to return a comma-separated list of months:
SELECT year, GROUP_CONCAT(month) AS months GROM tbl GROUP BY year
Returns:
2011 november,november,october
Or just select both columns and handle the display/presentation in your code:
SELECT year, month FROM tbl WHERE year = 2011
Returns
2011 november
2011 november
2011 october
In code, loop and only display the year when it changes.
Update Since a code example seems warranted...
// Results from above GROUP_CONCAT() query already fetched & stored in `$rowset`:
while ($row = mysql_fetch_assoc($res)) {
$rowset[] = $row;
}
// Now $rowset is a 2D array containing all rows.
foreach ($rowset as $row) {
// Output the year
echo $row['year'] . "\n";
// months are comma-separated via GROUP_CONCAT()
// explode them into an array
$months = explode(",", $row['months']);
foreach ($months as $m) {
// Output the months
echo $m . "\n";
}
}
If I understood your question correctly, I think a simple "ORDER BY" clause would do the trick for you, something similar to:
SELECT * FROM table_name ORDER BY year DESC
Then, use your scripting language to display the data, a good idea could be (maybe) to fetch the year of the first row, store it in a temporal variable, then loop through the rows and check if the year has changed, if it has, change the value of the aforementioned variable, something like this:
$current_year = $data_set[0]['year'];
foreach ( $data_set as $data_row )
{
$current_year = ( $data_row['year'] != $current_year) ? $data_row['year'] : $current_year;
//Do something with it and/or the rest of the data
}
Hope this was of any help.
Cheers.