Database results by month and year - php

I have a Joomla 3.1 site that is going to show reports about contributions made. The reports should be divided into groups by month and year. I have a code that pulls data from database but cannot make it put each month into a separate <div> and each year into a parent <div> to its months. How is it done? Here is my code.
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('contribution_date'), $db->quoteName('contribution_amount'), $db->quoteName('source')));
$query->from($db->quoteName('contribution'));
$db->setQuery($query);
$results = $db->loadObjectList();
?>
<table class='financial-reports'><th>aaa</th><th>bbb</th><th>ccc</th>
<?php
foreach ( $results as $result) {
echo '<tr>' . '<td class="fin-rep-date">' . JFactory::getDate($result->contribution_date)->format('d M, Y') . '</td>' .
'<td class="fin-rep-sum">' . $result->contribution_amount . ' ' .'руб.' . '</td>' .
'<td class="fin-rep-source">' . $result->source . '</td>' . '</tr>';
}
?>
</table>

I think the easies way would be convert results into multi-dimensional array like this:
foreach ($results as $result) {
$resultsArray[JFactory::getDate($result->contribution_date)->format('Y')][JFactory::getDate($result->contribution_date)->format('M')][] = $result;
}
And then you can use it like that:
<?php foreach ($resultsArray as $year) : ?>
<div>
<?php foreach ($year as $month) : ?>
<div>
<?php foreach ($month as $item) : ?>
<div>YOUR OUTPUT <?php echo $item->contribution_date; ?></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>

Related

How to add a for each loop in php?

i have an issues in my code below.
In days and time i want to add a loop, it s because i want to be able to get multiple different operating hours row. I ll add a repeater in my custom field.
And for now it s allow me to get only one row.
<ul>
<?php foreach($locations as $location) {
$title = get_the_title($location);
$address = '';
$address .= get_field('address', $location) . '<br>';
$address .= get_field('city', $location) . ', ';
$address .= get_field('state', $location) . ', ';
$address .= get_field('zip_code', $location);
$hours = get_field('days_open', $location);
$phone = get_field('contact_number', $location);
$email = get_field('contact_email', $location);
?>
<li>
<h3><?php echo $title; ?></h3>
<address><?php echo $address; ?></address>
<?php if($phone) { ?>
<?php echo $phone; ?>
<?php }; ?>
<?php if($email) { ?>
<?php echo $email; ?>
<?php }; ?>
<?php
if($hours && $hours[0]['days']
) {
echo $hours[0]['days'] . ': ';
}
?>
<?php
if($hours && $hours[0]['times']
) {
echo $hours[0]['times'];
}
?>
</li>
<?php
}
?>
</ul>
So i think my solution it s to add a foreach loop, can you help for that?
Thank you in advance cheers!
May be this is what you are looking for?
if(is_array($hours[0]['days'])){
foreach($hours[0]['days'] as $day)
{
echo $day . ': ';
}
}
if(is_array($hours[0]['times'])){
foreach($hours[0]['times'] as $time)
{
echo $time . ': ';
}
}
So I found the solutions it was :
<?php
if(is_array($hours)){
foreach($hours as $day)
{
echo $day['days'] . ': ';
echo $day['times'];
echo '<br />';
}
} ?>

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>

php passing on output to a variable

This might be easy, but i could not get solution for it as is novice and learning on the php
I want to put the output into a variable called $list so that it becomes
<?php $list .= '<h3> <?php echo $this->escape($item->n_make); ?> <?php echo $item->n_model; ?>
</h3>
<p> <?php echo $item->n_month; ?> <?php echo $item->n_year; ?> </p>
<p> <?php echo $item->n_short_description; ?> </p>
<hr/>'
?>
But the syntax is incorrect as its like php within php
How to get value of $list capturing in details
and can then use in explode function
<?php
$listings = explode("<hr/>", $list);
$numberOfListings = count($listings);
---- conditions
echo $listings;
?>
It's just simple concatenation:
$list .= '<h3> ' . $this->escape($item->n_make) . $item->n_model . '</h3>
<p> ' . $item->n_month . ' ' . $item->n_year . '</p>
<p> ' . $item->n_short_description . ' </p>
<hr/>';

While loop returning data

In the code below, I'm pulling all upcoming training classes from my database. I'm checking to see if the endDate has passed and I'm also checking if the status !='2'
I want this to return the 4 most recent results. It works fine until statusdoes =2. I understand that the loop is technically running 4 times, but only displaying the results with status !='2'
How can I change this so that if status = '2' the loop will continue until it finds 4 results that meet the criteria?
<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($count <= 4 && $row = $sth->fetch()) {
if($row['endDate'] > $today && $row['status'] != '2') {?>
<li>
<img class="post_thumb" src="/images/img.jpg" alt="" />
<div class="post_description">
<small class="details">
<?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
</small>
<a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
</div>
</li>
<?php }
$count++;
}
?>
You must put the $count++ inside the if loop, otherwise it will always get incremented.
As in:
<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($count <= 4 && $row = $sth->fetch()) {
if($row['endDate'] > $today && $row['status'] != '2') {?>
<li>
<img class="post_thumb" src="/images/img.jpg" alt="" />
<div class="post_description">
<small class="details">
<?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
</small>
<a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
</div>
</li>
<?php
$count++;
}
}
?>
You can break out out of the loop, if its four
while($row = $sth->fetch()) {
....
if($row['status']=='2' && $count >="4") break;
$count++;
}

Populating Navigation Tabs with SQL?

I'm looking for some kind of Nav tab (vertical) that allows it to be populated by SQL. For example, in the SQL table there would be a column for title (which is the title of the tab) and a column for the contents of the tab.
I have tried building one myself, but I have no idea how to fit it in a nav bar because usually I'd do a while loop to populate something with SQL, but because nav's have two things that need populating (the <li> and the <div> bits), I am unsure of how to do it.
Thanks.
Edit:
<ul>
<?php
$query = tep_db_query("select * table1");
while ($row = mysql_fetch_assoc($query)) {
echo '<li>' . $row['tabtitle'] . '</li>'
}
?>
</ul>
<?php
$query2 = tep_db_query("select * table1");
while ($row = mysql_fetch_assoc($query2)) {
echo '
<div id="tabs-' . $row['tabid'] . '">
<p> ' . $row['tabcontent'] . ' </p>
'
}
?>
What you have seems like it would be okay, you might be able to do something like this in order to eliminate half of your db calls.
//Get information from db.
<?php
$query = tep_db_query("SELECT tabid, tabtitle, tabcontent FROM table1");
while ($row = mysql_fetch_assoc($query)) {
$table[] = $row;
}
?>
<ul>
<?php foreach($table as $row){
echo '<li>' . $row['tabtitle'] . '</li>';
}
?>
</ul>
<?php foreach($table as $row){
echo '
<div id="tabs-' . $row['tabid'] . '">
<p> ' . $row['tabcontent'] . ' </p>
';
}
?>

Categories