I have a database with some months. I am using the following to group similar months:
$Mydates = array();
while(........)
{
$date = $row['date'];
$Mydates[$date] = (isset($Mydates[$date])) ? $Mydates[$date] + 1: 1;
}
My table contains 4 data, 3 Jan's and 1 Feb. Now this information is being run through an array which contains the months of the year. Here is what I have so far:
$months = array("Jan","Feb","Mar",etc........);
foreach($months as $month)
{
foreach($Mydates as $Mydate => $Number)
{
if($month == $Mydate)
{
echo $month." (".$Number.")<br />";
}else{
echo $month."<br />";
}
}
}
Now when there were only Jan's this works fine. Now that I have added another month my results display like below:
Jan(3)
Jan(3)
Feb(1)
Feb(1)
Mar
Mar
etc.....
etc.....
How can I get this to only display once like this:
Jan(3)
Feb(1)
Mar
etc....
Now I have noticed that the more months add the more it duplicates. Can someone look at this and tell me where I am going wrong here?
It seems, $Mydates is already aggregated and accessible by month. Remove the inner loop and just look, if there's a corresponding month in $Mydates
foreach($months as $month) {
if(isset($Mydates[$month])) {
echo $month." (".$Mydates[$month].")<br />";
} else {
echo $month."<br />";
}
}
Related
I'm trying to create a menu that inserts the current year and archives the previous years dynamically. I have the below nested within a <ul id="Year_Menu">
$current_year = date("Y");
$nxt_yr = date("Y", strtotime('+1 years'));
if ($current_year >= $nxt_yr) {
$recent_past_year = $current_year -1;
$years = array($current_year, $recent_past_year, "2018", "2017", "2016");
} else {
$years = array($current_year, "2018", "2017", "2016");
}
foreach ($years as $value) {
echo "<li><a href='blog_posts.php?month=01&yr=$value;'>$value</a></li>";
}
</ul>
This works for this year and the next, but in 2021 the jig is up :) I can keep creating elseif statements & go up to 2030 (or whatever) but talk abt inefficient. I'm not already thinking this is the most efficient btw, but still learning, I'm trying to figure out a better way to do this, so that $recent_past_year ends up archiving, and the rest of the code just keeps moving forward.
How can I get $recent_past_year in 2021 (& beyond) to dynamically archive within the $years array? ...stuck
You might use a range to get the years using the current year as the start:
$current_year = date("Y");
$years = range(intval($current_year), 2016);
echo "<ul>";
foreach ($years as $value) {
echo "<li><a href='blog_posts.php?month=01&yr=$value;'>$value</a></li>";
}
echo "</ul>";
Result
2019201820172016
Php demo
Use PHP function range.
Example:
<?php
$thisYear = 2019;
$yearsFrom2016 = array_reverse(range(2016, $thisYear));
I'm starting to play with PHP and MySQL.
However, I have encountered a problem and can not deal with it.
In my database I have data such as: id, domain_name, year, month, organic_search_visit
I want to display them now using PHP, but the data is displayed not as I want.
The data in my database looks like this:
Site1.com
January - 2017 - 11k visit
February - 2017 - 10k visit
...
January - 2018 - 11k visit
February - 2018 - 10k visit
...
Site2.com
January - 2017 - 11k visit
February - 2017 - 10k visit
...
January - 2018 - 11k visit
February - 2018 - 10k visit
...
I want to display the data from the database using PHP in the table so that it can see how I had traffic in a given year / month.
My code:
$results = $mysqli->query("SELECT domain_name, month, year, organic_search_visit FROM organic_search");
echo "<table>";
echo "<thead><tr>";
while($row = mysqli_fetch_array($results))
{
echo "<td>Month</td>";
echo "<th>".''.$row['month'].''."</th>";
echo "</tr><thead>";
echo "<tr>";
echo "<td>2017</td>";
echo "<td>".''.$row['organic_search_visit'].''."</td>";
}
echo "</tr>";
echo "</table>";
Start with sorted result data. Iterate through the data and print the relevant table and row tags when they change in the data.
You can use something like the example below. Note that this has a fixed list of months for column headers and requires the data to match.
$results = $mysqli->query("SELECT domain_name, month, year, organic_search_visit FROM organic_search GROUP BY domain_name,month,year, ORDER BY domain_name, year, month");
$months = array('January', 'February', 'March'); //etc
$domain = "";
$year = "";
$month = 0;
while($row = mysqli_fetch_array($results))
{
if($domain!=$row['domain_name']){ //Check if the domain has changed
$domain = $row['domain_name'];
$year = "";
$month = 0;
if($domain!="") echo "</tr></table>"; //Only close last domain table if there was a previous domain
echo "<table><tr><th>".$domain."</th>"; //Start new domain table
foreach($months as $month) echo "<td>".$month."</td>"; //Print header row
echo "</tr>";
}
if($year!=$row['year']){
$year = $row['year'];
$month = 0;
if($year!="") echo "</tr>";
echo "<tr><td>".$year."</td>";
}
while($months[$month] != $row['month']){
$month+=1;
echo "<td></td>";
}
echo "<td>".$row['organic_search_visit']."</td>";
}
echo "</tr></table>";
I only want to show some html on certain days of the week. I think I am very close, but cannot get this to work. Thanks for the help.
<?php
$dayofweek = date('l');
$daystoshow = array('Thursday','Sunday','Wednesday');
if ($dayofweek == $daystoshow) {
echo "show this";
}
?>
Use in_array() to check to see if a value in in an array:
if (in_array($dayofweek, $daystoshow)) {
Allright:
Lets use PHP date function
Do you want to show it only during a particular month ?
$date = date("m");
if ($date == "01") { // only on january
//output
}
Do you want to show it only during a particular day ?
$date = date("d");
if ($date == "01") { //Only on the first of the month
//output
}
Do you want to show it only during particular dayS ?
$days = array("01","11","28");
$date = date("d");
if (in_array($date,$days)) { //Only 01,11,28
//output
}
Do you want to show it only during particular days of week ?
$days = array("Thursday","Sunday","Wednesday");;
$date = date("l");
if (in_array($date,$days)) {
//output
}
Do you want to show it only during a particular year ?
$date = date("Y");
if ($date == "2014") { //Only on 2014
//output
}
i need to list total post article archive by years and months like this :
Output(my need):
2014
January(31)
February(28)
March(0)
April(130)
May(450)
June(0)
July(0)
August(0)
September(0)
October(520)
November(20)
December(31)
PHP:
$sql = "SELECT title, YEAR(FROM_UNIXTIME(timestamp)) AS YEAR,
MONTHNAME(FROM_UNIXTIME(timestamp)) AS MONTH,
COUNT(*) AS TOTAL
FROM article GROUP BY YEAR, MONTH ORDER BY YEAR DESC, MONTH ";
$newsdata = DB->fetch($sql);
$currentYear = null;
foreach($newsdata AS $news){
if ($currentYear != $news['YEAR']){
echo '<ul>'.$news['YEAR'].'</ul>';
$currentYear = $news['YEAR'];
}
echo '<li>'.$news['MONTH'].' '.$news['TOTAL'].'</li>';
}
My code worked but print only month if posted article in this month.
Ouput:
2014
January(31)
February(28)
April(130)
May(450)
October(520)
November(20)
December(31)
I need to list all month and print total article for each month. if month not posted article print (0) for this month.
how do can i fix my problem? my code/way is true?!
Your database query won't return a row for a month that has no articles. It doesn't know anything about months. So you need to handle this in code by having an array of all months and looping through that. The alternative would be to alter your schema, have another table with all 12 months, and join on that. But I think the following is easier for you:
$months = array( "January", "February", ... );
Then you can do the following in your output instead:
// Index article counts by month and year for easy lookup
$indexedNewsData = array();
foreach ($newsdata as $news) {
$indexedNewsData[$news['YEAR']][$news['MONTH']] = $news['TOTAL'];
}
// Then print output
foreach($newsdata AS $news){
if ($currentYear != $news['YEAR']){
echo '<ul>'.$news['YEAR'].'</ul>';
$currentYear = $news['YEAR'];
} else {
// Continue here otherwise we will print each year's data 12x
continue;
}
foreach ($months as $month) {
$total = intval($indexedNewsData[$news['YEAR']][$month]);
echo '<li>'.$month.' '.$total.'</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.