PHP + MYSQL how to group the months by years - php

I need to create a grouped year / month list
2020
January
February
2019
May
June
My Table (name: reports)
enter image description here
Query
SELECT YEAR(report_date) as y, MONTH(report_date) as m, report_company_id
FROM reports
WHERE YEAR(report_date) = YEAR(report_date) AND report_company_id="11"
GROUP BY y, m ORDER BY y DESC, m DESC
I need help with a script displaying the structure of the list
<div class="tab-pane fade show" id="tab-<?php echo $row['company_id']; ?>" role="tabpanel" aria-labelledby="v-pills-home-tab">
<div class="accordion" id="accordionExample">
<div class="card">
<?php
$count_month = $db -> query
(
'SELECT YEAR(report_date) as y, MONTH(report_date) as m, report_company_id
FROM reports
WHERE YEAR(report_date) = YEAR(report_date) AND report_company_id="'.$company_id.'"
GROUP BY y, m
ORDER BY y DESC, m DESC'
);
$last_year = 0;
?>
<div data-role="main" class="ui-content">
<?php
foreach ($count_month as $key) { ?>
<div class="card-header" id="headingOne">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne-<?php echo $key['y']; ?>" aria-expanded="true" aria-controls="collapseOne"><?php echo $key['y']; ?></button>
</div>
<div id="collapseOne-<?php echo $key['y']; ?>" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<?php echo $monthName = date('F', mktime(0, 0, 0, $key['m'], 10)); // March ?>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
My result. year is repeated twice
enter image description here

Method 1:
If you want 2nd level aggregation in visual then you need to convert your array to another associative array using year as key
$count_month_map = Array();
foreach ($count_month as $value) {
if(array_key_exists($value['y'], $count_month_map)) {
array_push($count_month_map[ $value['y'] ], $value);
} else {
$count_month_map[ $value['y'] ] = Array( $value );
}
}
$last_year = 0;
?>
<div data-role="main" class="ui-content">
<?php
foreach ($count_month_map as $key => $value) { ?>
<div class="card-header" id="headingOne">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne-<?php echo $key; ?>" aria-expanded="true" aria-controls="collapseOne"><?php echo $key; ?></button>
</div>
<?php
foreach ($value as $key1) {
?>
<div id="collapseOne-<?php echo $key1['y']; ?>" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<?php echo $monthName = date('F', mktime(0, 0, 0, $key1['m'], 10)); // March ?>
</div>
</div>
<?php } ?>
<?php
}
?>
</div>
Here $count_month_map is another associative array formed from $count_month
Then inside the main loop you need to have a second loop to loop though the array of array
Method 2:
You can store the year in your loop in a dummy variable say $curr, at the end bracket you assign it to another dummy variable $prev
then conditionally you suppress the button
$curr = '';
$prev = '';
?>
<div data-role="main" class="ui-content">
<?php
foreach ($count_month as $key) {
$curr = $key['y'];
if($prev != $curr) {
?>
<div class="card-header" id="headingOne">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne-<?php echo $key['y']; ?>" aria-expanded="true" aria-controls="collapseOne"><?php echo $key['y']; ?></button>
</div>
<?php
}
?>
<div id="collapseOne-<?php echo $key['y']; ?>" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<?php echo $monthName = date('F', mktime(0, 0, 0, $key['m'], 10)); // March ?>
</div>
</div>
<?php
$prev = $curr;
}
?>
</div>
Note: Method 2 only applicable for year column sorted, which you are already doing

Related

Foreach Loop in a view doesn't work in CI3 view

Hi I'm trying to do foreach loop on a view but it's not showing. I've checked the database and there's data. I've checked the QueryBuilder on the model and run the query on phpmyadmin and its working. Do you guys know what happened?
view:
<div class="content-wrapper">
<section class="content">
<div class="row">
<?php
foreach ($node as $d) {
?>
<div class="col-lg-6 col-md-6 col-xs-6">
<div class="box box-device box-solid" id="device_<?php echo $d['id_device']; ?>">
<div class="box-header">
<div class="row">
<div class="col-sm-3 col-xs-3">
<button aria-pressed="false" data-device="<?php echo $d['id_device']; ?>" class="btn btn-onoff btn-sm btn-toggle<?php echo ($d['status_device'] == 1) ? ' active': ''; ?>" data-toggle="button" type="button">
<div class="handle"></div>
</button>
</div>
<div class="col-sm-9 col-xs-9">
<h3 class="box-title<?php echo ($d['rule'] == 0) ? ' notactive': ''; ?>"><?php echo $d['nama_device']; ?></h3>
</div>
</div>
</div>
<div class="box-body text-center" onclick="window.location.href = '<?php echo site_url('Setting/rule/' . $d['id_device']); ?>'">
<img alt="<?php echo $d['nama_device']; ?>" src="<?php echo base_url('uploads/device/' . $d['foto']); ?>" />
<h4><?php echo $d['nama_device']; ?></h4>
<p><?php echo $d['id_device']; ?></p>
</div>
<div class="box-footer">
<a class="btn btn-default btn-block btn-lg btn-detail" href="<?php echo site_url('Setting/rule/' . $d['id_device']); ?>">View Rule</a>
</div>
</div>
</div>
<?php
}
?>
</div>
</section>
<?php $this->load->view('components/version'); ?>
</div>
Controller:
public function rulenode() {
$this->data['sub_title'] = 'Setting - Rule';
$this->load->model('Mrule');
$this->data['node'] = $this->Mrule->device();
$this->load->view('setting/rulenode', $this->data);
}
Model:
public function device() {
$this->db->select('id_device, nama_device, foto, status_device');
$this->db->from('tb_device');
$this->db->join('arduino_rule', 'id_device = id_node');
return $this->db->get()->result_array();
// var_dump($this->db->last_query());
}
<?php
$node = $this->db->get('table_name');
foreach ($node as $d) {
?>

Print $variable value using jQuery is not working properly?

I have a small interface Site (check site interface once) where I am showing articles count in yellow box I am trying to print the value with help of jQuery in the column but value is printing 1 column after every yellow box
I have tried a lot but nothing works
<div class="news-card NewsList">
<div class="row">
<div class="col-md-2 ">
<div class="news-left pb-0">
<div class="view-more">
<a data-toggle="collapse" class="listSimilarNews" href="#collapse<?=$i?>" role="button" aria-expanded="true" aria-controls="#collapse<?=$r?>">
<span id="listSimilarNews"><?php echo (count($nestedarr)); ?></span><br>
Articles
<i class="accordionarrow icofont-rounded-down"></i>
</a>
</div>
</div>
</div>
</div>
</div>
$('#listSimilarNews').html( <?php echo (count($nestedarr)); ?>);
for more reference complete code with PHP
<?php
$mainarr = [];
for($i=0; $i < count($row); $i++){
?>
<div class="news-card NewsList">
<div class="row">
<div class="col-md-2 ">
<div class="news-left pb-0">
<div class="view-more"><a data-toggle="collapse" class="listSimilarNews" href="#collapse<?=$i?>" role="button" aria-expanded="true" aria-controls="#collapse<?=$r?>">
<span id ="listSimilarNews"><?php print_r(count($nestedarr)) ; ?>
</span><br>Articles <i class="accordionarrow icofont-rounded-down"></i></a>
</div>
</div>
</div>
</div>
</div>
<?php
$nestedarr=[];
while(($j) < count($row) && ($id == (string) $row[$j]['_id'])){
$i++;
$j++;
array_push($nestedarr, $row[$j]['newsSources']);
}
array_push($arr, $nestedarr);
?>
<script language='javascript'>
$('#listSimilarNews').html( '<?php echo (count($nestedarr)); ?>');
</script>
<?php
}
?>

Multiple Bootstrap accordion are opening and closing at the same time

Here is the situation. I have an accordion, setup and the problem is that multiple accordions are opening and closing the same time.
Here is the code:
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading-<?php echo $x; ?>">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</a>
</h4>
</div>
<div id="collapse-<?php echo $x; ?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading-<?php echo $x; ?>">
<div class="panel-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"> </iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Any help would be appreciated.
Thank you,
Kevin Davis
You can use latest Bootstrap 4 Accordion. More about it here https://getbootstrap.com/docs/4.3/components/collapse/#accordion-example
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="heading-1">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-1" aria-expanded="true" aria-controls="collapse-1">
111
</button>
</h2>
</div>
<div id="collapse-1" class="collapse show" aria-labelledby="heading-1" data-parent="#accordionExample">
<div class="card-body">
111
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-2">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-2" aria-expanded="false" aria-controls="collapse-2">
222
</button>
</h2>
</div>
<div id="collapse-2" class="collapse" aria-labelledby="heading-2" data-parent="#accordionExample">
<div class="card-body">
222
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-3">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-3" aria-expanded="false" aria-controls="collapse-3">
333
</button>
</h2>
</div>
<div id="collapse-3" class="collapse" aria-labelledby="heading-3" data-parent="#accordionExample">
<div class="card-body">
333
</div>
</div>
</div>
</div>
So your code will be
<div class="accordion" id="accordionExample">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="card">
<div class="card-header" id="heading-<?php echo $x; ?>">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</button>
</h2>
</div>
<div id="collapse-<?php echo $x; ?>" class="collapse<?php if ($x == 0) { ?> show<?php } ?>" aria-labelledby="heading-<?php echo $x; ?>" data-parent="#accordionExample">
<div class="card-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"></iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Added this class="collapse<?php if ($x == 0) { ?> show<?php } ?>" for the first box to be opened.
If you have more then one accordion on one page - don't forget to change id="accordionExample" and data-parent="#accordionExample", maybe by adding them an increment for counting accordions.

Dynamic carousel slider 3 slides per page

enter image description here
I need to display 3 date items each time in Bootstrap carousel in PHP using while loop, I want to display 3 date items in one slide and next slide have another 3 data items and so on.
Here is what I have tried:
<?php
date_default_timezone_set('UTC');
$date = date('Y-m-d');
$end_date = '2018-11-05'; ?>
<div role="listbox" class="carousel-inner" >
<?php $i_count = 1; ?>
<?php if($i_count==1){ ?>
<div class="item active">
<?php }
else if($i_count%3==0) { ?>
<div class="item">
<?php } ?>
<div class="col-md-12 pa-0">
<?php
while(strtotime($date) <= strtotime($end_date))
{
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
?>
<div class="col-md-4 col-sm-4 col-xs-4 pa-0 <?php if ($i_count == 1 || $i_count == 2 || $i_count == 3 ) echo ' activeday'?>">
<div class="date-heading">
<div class="day-of-week"><?= date('l', strtotime($date)); ?></div>
<div class="date-secondary pb-5"><?= date('F d', strtotime($date)); ?></div>
<div class="date-head-text"><?= date('l', strtotime($date)); ?></div>
</div>
<div class="choose-time">
<div class="form-inline">
<input class="time-selection" name="time[]" data-readable-date="2018-02-03" value="2018-02-03 06:15" id="appt1517638500" data-original-text="2018-02-03 06:15" type="radio">
<label id="lbl_appt1517638500" for="appt1517638500" data-original-text="11:45">11:45</label><br>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php $i_count++; ?>
</div>
<a href="#carousel-example-captions" role="button" data-slide="prev" class="left carousel-control">
<span aria-hidden="true" class="fa fa-caret-left"></span>
<span class="sr-only">Previous</span>
</a>
<a href="#carousel-example-captions" role="button" data-slide="next" class="right carousel-control" >
<span aria-hidden="true" class="fa fa-caret-right"></span>
<span class="sr-only">Next</span>
</a>
</div>

How show results in another column via "Foreach"?

I want to display more results in another column via foreach from the database but I can't get it to work.
<div class="row">
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<i class="fa fa-pencil"></i>
<i class="fa fa-times"></i>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<i class="fa fa-pencil"></i>
<i class="fa fa-times"></i>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
</div>
As you can see, it's two different columns. I want to do foreach and show one set of information in one column and the other set in another. However, the code below returns the same value in both columns.
how can I do this? Thanks.
You are iterating through each item of results in both loops, so their results are the same. Why not try something like this
<?php
$cols = 3;
?>
<div class="row">
<?php
if (is_array($results))
{
for($i = 0 ; $i < $cols ; $i++)
{
echo "
<div class='col-md-".(12/$cols)."'>";
for ($j = 0 ; $j < (count($results) / $cols) + 1 ; $j++)
{
if ($i * $cols + $j < count($results)) // make sure it won't give errors for array out of range....
{
echo "
<div class='user-item'>
<div class='user-btns'>
<a href='#' class='btn btn-green btn-xs'><i class='fa fa-pencil'></i></a>
<a href='#' class='btn btn-red btn-xs'><i class='fa fa-times'></i></a>
</div>
<img src='".$results[$i* $cols + $j]->avatar."' alt=' class='img-responsive'/>
<div class='u-details'>
<h5><i class='fa fa-user'></i>".$results[$i* $cols + $j]->first_name . ' ' . $results[$i* $cols + $j]->last_name."</h5>
<h5><i class='fa fa-envelope'></i>".$results[$i* $cols + $j]->email."</h5>
<h5><i class='fa fa-user-md'></i>".$results[$i * $cols + $j]->username."</h5>
</div>
<div class='clearfix'></div>";
}
}
echo "
</div>";
}
}
?>
</div>

Categories