Laravel Eloquent Group By Dates - laravel

i was trying to search on how i can create the same result on this image below on my laravel .I was trying to group it by dates. All the dates that are on the same month will be merge on a single row.
what i accomplished so far. i cannot merge the dates that has the same months.
Does any one know how i can accomplished this? thanks

<table>
<thead>
<tr>
<th>Test Type</th>
<?php
$types = Tblreporttype::all();
?>
#foreach($types as $type)
<?php
$dates = Tblreportdate::with('tblreporttype')
->groupBy('fDate')
->where('tblreporttype_id', '=', $type->id)
->get();
?>
#foreach($dates as $date)
<?php
$convert = $date->fDate;
$my_date = date('M/y', strtotime($convert));
?>
<th width="100">
<?php
if($my_date == $my_date) {
echo $my_date;
}
?>
</th>
#endforeach
#endforeach
</tr>
</thead>
<tbody>
<?php
$dates = Tblreportdate::with('tblreporttype')
->groupBy('fDate')
->get();
?>
<?php
$types = Tblreporttype::all();
?>
#foreach($types as $type)
<tr>
<td width="200">{{ $type->fType }}</td>
#foreach($dates as $date)
<?php
$convert = $date->fDate;
$my_date = date('d', strtotime($convert));
?>
<td width="100">
<?php
if($date->tblreporttype->id == $type->id) {
echo $my_date;
} else {
echo " ";
}
?>
</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
This is the output

Related

Paging in codeigniter doesn't work - could not be converted to string

I'm not able to insert the pager function inside the readHunters() method so that the records in my table are not all on the same page (I want to put 10 records per page).
I know that in app->Config->Pager.php there is public $perPage = 10; which helps to define the number of records in the table with pagination.
HunterController.php
public function readHunters()
{
try {
$hunter = new HunterModel();
$data['hunter'] = $hunter->findAll();
$data['pager'] = $this->$hunter->page;
return view('read_hunters', $data);
} catch (\Exception $e) {
exit($e->getMessage());
}
}
read_hunters.php
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Year</th>
<th>Height</th>
<th>Weight</th>
<th>Type hunter</th>
<th>Type nen</th>
<th>Type blood</th>
<th>Date registration</th>
<th>Date upgrade</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($hunter as $hxh): ?>
<tr>
<td><?= $hxh['id_hunter']?></td>
<td><?= $hxh['name_hunter']?></td>
<td><?= $hxh['year_hunter']?></td>
<td><?= $hxh['height_hunter']?> m</td>
<td><?= $hxh['weight_hunter']?> kg</td>
<td><?= $hxh['type_hunter']?></td>
<td><?= $hxh['type_nen']?></td>
<td><?= $hxh['type_blood']?></td>
<td><?= date('d/m/Y H:i:s', strtotime($hxh['date_registration']))?></td>
<td><?= $hxh['date_update'] == $hxh['date_registration'] ? '' : date('d/m/Y H:i:s', strtotime($hxh['date_update'])) ?></td>
<td>
"><i class="fa fa-edit"></i> Update
<i class="fa fa-trash"></i> Delete
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?= $pager->links(); ?>
To provide a paginated list in the app, your controller's method looks like this:
$data = [
'hunter' => $hunter->paginate(10),
'pager' => $hunter->pager,
];
return view('read_hunters', $data);

Laravel Nested #foreach By 2 Different Model in View

Model
// Relationship in \App\FatherRegistrars and \App\MotherRegistrars and \App\GuardianMaleRegistrars \App\GuardianFemaleRegistrars
public function student_registrars()
{
return $this->belongsToMany('App\StudentRegistrars')->withTrashed();
}
Controller
public function index(Request $request)
{
$dataFathers = \App\FatherRegistrars::get();
$dataMothers = \App\MotherRegistrars::get();
$dataGM = \App\GuardianMaleRegistrars::get();
$dataGF = \App\GuardianFemaleRegistrars::get();
// manual pagination using code attached in AppServiceProvider.php
$data = $dataFathers->toBase()->merge($dataMothers)->paginate($items);
return view('parents-guardians-registrars.index', compact('data', 'dataFathers', 'dataMothers', 'dataGM', 'dataGF'))->withItems($items);
}
View
#foreach($data as $var)
<tr>
<td style="text-align:center;">
<input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
</td>
<td>{{$var->id }}</td>
<td>{{$var->name}}</td>
<td>{{$var->email}}</td>
<td>
<?php $elements = array(); ?>
#foreach($var->student_registrars as $category)
<?php $elements[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements); ?>
</td>
<td>
// Second foreach should be here
</td>
<td>
Detail
</td>
#endforeach
// Second foreach
#foreach($dataGM as $var2)
<tr>
<td>
<?php $elements = array(); ?>
#foreach($var2->student_registrars as $category)
<?php $elements[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements); ?>
</td>
</tr>
#endforeach
</tr>
And the result for code above is:
I have a little problem related multiple foreach in one view. It's actually just a simple problem but I am stuck here. Any body can solve it?
Should I use partial view to do this?
This may not exactly work (not tested or verified), but you can give something like this a try:
#foreach($data as $var)
<tr>
<td style="text-align:center;">
<input type="checkbox" id="select" class="sub_chk" data-id="{{$var->id}}" value="{{$var->id}}" name="selected_values[]"/>
</td>
<td>{{$var->id }}</td>
<td>{{$var->name}}</td>
<td>{{$var->email}}</td>
<?php $elements = array(); ?>
#foreach($var->student_registrars as $i => $category)
<td>
<?php $elements[] = ' '.$category->name.' '; ?>
<?php echo implode(',<br>', $elements); ?>
</td>
<td>
<?php $elements2 = array(); ?>
#foreach($var2->student_registrars[$i] as $category)
<?php $elements2[] = ' '.$category->name.' '; ?>
#endforeach
<?php echo implode(',<br>', $elements2); ?>
</td>
#endforeach
<td>
Detail
</td>
</tr>
#endforeach
The problem you have is that there is no direct association $data and $dataGM. You should try and use Relationships so that the data needed for $dataGM can be accessed as a relationship from $data, e.g.
$children = $data[$i]->children;
$adopted = $data[$i]->adopted_children;
Then you can loop directly on these items instead of having to create multiple variables.

How do I add pagination to codeigniter

Below is how the ci controller looks like.
I'm just following tutorial from Youtube Channel.
How do you add pagination so the page only load 20 results per page ?
<?php
class Product_details extends CI_Controller{
function index(){
$this->load->library('pagination');
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products();
$this->load->view('product_listing',$data);
}
}
View
<table>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Image</th>
</tr>
<?php
foreach ($userArray as $key => $value) {
echo "<tr>
<td>".$value['id']."</td>
<td>".$value['post_id']."</td>
<td>".$value['price']."</td>
<td><img src=".$value['imageUrl']."/></td>
</tr>";
}
?>
</table>
Thank you
you can pass page number in the model.
Controller
<?php
class Product_details extends CI_Controller{
function index($pageNo){
$this->load->model('product_model');
$data['userArray'] = $this->product_model->return_products($pageNo);
$this->load->view('product_listing',$data);
}
}
Model
public function all($pageNo){
$pageNo -= 1;
$this->db->select("*")
->from('products')
->order_by('id',"ASC")
->limit(20, $pageNo * 20);
$query = $this->db->get();
return $query->result_array();
See the example how to add pagination.
Let's My controller is Dashboard.php and it's method is index.
Now configure pagination.
public function index()
{
$config['base_url'] = site_url('/dashboard/index/');
$config['total_rows'] = $this->dashboard_model->num_rows();
$config['per_page'] = 5;
$config['use_page_numbers'] = FALSE;
$this->pagination->initialize($config);
$data['data'] = $this->dashboard_model->all_blog($config['per_page'], $this->uri->segment(3));
$this->load->view("/dashboard/blog", $data);
}
Model is Dashboard_model.php
public function all_blog($limit, $offset)
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$q = $this->db->get();
$data = $q->result_array();
return $data;
}
public function num_rows()
{
$this->db->from('blogs');
$this->db->select('*');
$this->db->order_by("created_at", "desc");
$this->db->limit($limit, $offset);
$q = $this->db->get();
$data = $q->num_rows();
return $data;
}
Now it's my view blog.php
<div class="table-responsive">
<table class="footable table table-stripped toggle-arrow-tiny" data-page-size="8" data-filter=#filter; id="filter">
<thead>
<tr>
<th></th>
<th>S.No</th>
<th>Title </th>
<th>Blog Image</th>
<th>Added For</th>
</tr>
</thead>
<tbody>
<?php
$count = $this->uri->segment(3, 0);
foreach ($data as $key) :
?>
<tr>
<td><input type="checkbox" class="i-checks" name="input[]"></td>
<td><?php echo ++$count; ?></td>
<td><?php echo $key['blog_title']; ?></td>
<td><img src="<?php echo $key['blog_image']; ?>" style="width: 150px; height: 80px" /></td>
<td><?php echo $key['user_type']; ?></td>
</tr>
<?php endforeach?>
</tbody>
</table>
</div>
<?php
echo $this->pagination->create_links();
?>

How to display data from query - one to many relationship between db tables

I'm trying to display data from query but I can't show it as it should.
My tables in database are: ordersheader and orderitems. It's one to many relationship between them. One ordersheader have many orderitems. They are joined ON idOrder. One idOrder in ordersheader corresponds to many rows in orderitems.
I have to show all orders for current date and to show all orderitems for each idOrder. It should look like this for all orders:
http://prntscr.com/7lwoan
First row is order - it has to be displayed all orderitems for this order and when you click Details it has to be shown each orderitem on separate row.
Now, as I used code below, each orderitem is shown on separate row. How to make it looks like the above link? Thanks!
My model is:
function getOrders(){
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('ordersheader.*,customer.name,orderitems.*');
$this->db->from('ordersheader');
$this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);
$query = $this->db->get();
return $query->result();
}
My view is:
<?php
if($orderlist){
foreach($orderlist as $row) {
?> <tr class="overview">
<td><?php echo $row->idOrder; ?></td>
<td class="text-center"><img src="<?= base_url();?>/assets/images/tick.png"></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->eggSize; ?></td>
<td><?php echo $row->quantity; ?></td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 60%;">
60%
</div>
</div>
</td>
<td>18:00</td>
<td><button type="button" class="btn btn-link btn-xs view-full-order">Full</button></td>
</tr>
<tr class="full hide">
<td></td>
<td></td>
<td></td>
<td><?php echo $row->eggSize; ?></td>
<td><?php echo $row->quantity; ?></td>
<td>
<div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: <?php echo $rand; ?>%;">
<?php echo $rand; ?>%
</div>
</div>
</td>
<td>14:00</td>
<td></td>
</tr>
<?php } } ?>
</tbody></table>
Edited: I made it with group_concat and explode. But now, problem is that for each idOrder I should calculate average rand (this is the value of progress bar) of all orderitems. This should be for each order - avegare sum of orderitems. Now, it's not calculated correct.
It now looks like that:
http://prntscr.com/7mu0k3
This row where is Billa should be average of the next 2 rows.
My view is:
<tbody class="client">
<?php
if ($orderlist) {
foreach ($orderlist as $row) {
$i = 0;
?> <tr class="overview">
<td><?php echo $row->idOrder; ?></td>
<td class="text-center">
<?php
$s = $row->eggSize;
$egg_size_array = explode(",", $row->eggSize);
$quantity_array = explode(",", $row->quantity);
if (!empty($egg_size_array))
foreach ($egg_size_array as $key => $value) {
$rand[$key] = rand(1, 100);
$random_hour[$key] = rand(01, 24);
}
?>
</td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->eggSize; ?></td>
<td><?php
$s = $row->quantity;
$s = explode(',', $s);
echo array_sum($s);
print_r($rand); echo "<br>";print_r(array_sum($rand)); echo "<br>"; print_r(count($egg_size_array));
?></td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: <?php
echo array_sum($rand)/ count($egg_size_array); ?>%;">
<?php echo array_sum($rand)/ count($egg_size_array); echo "%"; ?>
</div>
</div>
</td>
<td><?php echo max($random_hour); echo " ч."; ?></td>
<td><button type="button" class="btn btn-link btn-xs view-full-order">
<?php echo lang('details'); ?> </button></td>
</tr>
<?php
if (!empty($egg_size_array))
foreach ($egg_size_array as $key => $value) {
// $rand = rand(1, 100);
//$random_hour = rand(01, 24);
?>
<tr class="full hide">
<td></td>
<td></td>
<td></td>
<td>
<?php echo $value; ?></td>
<td><?php echo $quantity_array[$key]; ?></td>
<td>
<div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: <?php echo $rand[$key]; ?>%;">
<?php echo $rand[$key]; ?>%
</div>
</div>
</td>
<td><?php echo $random_hour[$key] . ' ч.' ?></td>
<td></td>
</tr>
<?php
}
?><?php
}
}
?>
</tbody></table>
My model is:
function getOrders(){
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d');
$this->db->select('orderitems.eggSize as size');
$this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.itemNumber) as itemNumber');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.quantity ) as quantity ');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.unitPrice) as unitPrice');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.eggSize ) as eggSize');
$this->db->from('ordersheader');
$this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);
$this->db->group_by('orderitems.idOrder');
$query = $this->db->get();
return $query->result();
}
You should create a function to return order children.
function GetItems($idOrder)
{
return $this->db->get_where("orderitems", "idOrder" => $idOrder)->array_result();
}
After that, you will remove the JOIN with the orderitems table. And then you should get the array_result of your query and iterate with it by foreach and attach the children in your collection like this:
$curr_date = $date->format('Y-m-d ');
$this->db->select('ordersheader.*,customer.name');
$this->db->from('ordersheader');
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);
$query = $this->db->get()->array_result();
$index = 0;
foreach($query as $item)
{
$query[$index]["children"] = GetItems($item->idOrder);
$index++;
}
I've created a mockup in a fiddle to show you an example: http://codepad.org/LfjQJQCP

Displaying serial numbers with query result

I am trying to fetch data from my database and when I display it I want to show serial numbers (Just like a list) , example:
**Serial Number** Name Country
1. John USA
2. Srijon UK
I have tried something with PHP Loops, but I couldn't make it work. Would you please kindly help me? please note that by serial numbers I don't mean values retrieved from database.
Thanks in Advance :)
Here's my Model
//Function To Create All Batch List
function batch_list($perPage,$uri) {
$this->db->select('*');
$this->db->from('batch');
$this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
$this->db->order_by('batchid','DESC');
$getData = $this->db->get('', $perPage, $uri);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
//End of Function To Create All Batch List
Here's mY Controller
function index(){
$this->load->library('pagination');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list($config['per_page']
,$this->uri->segment(3));
$data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);
}
Here's My View
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($records as $row){ ?>
<tr>
<td><?php echo $row['batchname'];?> </td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?> support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Serial</th>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php $i = $this->uri->segment(3); foreach ($records as $row){ $i++; ?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['batchname'];?> </td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?> support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
In my case the fourth and fifth parameters where page number and rows per page respectively
ie: example.com/category/page/1/10
first page with 10 rows per page.
so in this case just use this before the loop
$i = $this->uri->segment(4)? (
$this->uri->segment(4) + $this->uri->segment(5)? $this->uri->segment(5):0
)
:0;
and from inside the loop just increment the value of i.
foreach ($result_obj as $key => $value)
{
$i++;
...
}
Try this..work like this..1,2,3,4,5
$i = 0
while($condition)
{
echo '<td>'.$i++.'</td>';
}

Resources