I have data from one table, let's say t_schedule, by default, they are shown like this
|Date |Time |Activity |
|2014-12-22|08.00.00|Activity 1|
|2014-12-22|09.00.00|Activity 2|
|2014-12-22|10.00.00|Activity 3|
|2014-12-23|08.00.00|Activity 1|
|2014-12-23|09.00.00|Activity 2|
|2014-12-24|08.00.00|Activity 1|
|2014-12-24|09.00.00|Activity 2|
|2014-12-24|10.00.00|Activity 3|
I want to show them like this
|Date |Time |Activity |
|2014-12-22|08.00.00|Activity 1|
| |09.00.00|Activity 2|
| |10.00.00|Activity 3|
|2014-12-23|08.00.00|Activity 1|
| |09.00.00|Activity 2|
|2014-12-24|08.00.00|Activity 1|
| |09.00.00|Activity 2|
| |10.00.00|Activity 3|
How should I do? Can somebody halp me, please?
If you want do this by query then you have do a complex query, rather than you can do it in view. I think you have use a loop to show the the first result. if just modify it just little bit than it will be shown like you desired output.
<?php $last_date = ''; ?>
<?php for($i=0; $i<count($result); $i++): ?>
<tr>
<?php if($result[$i]->date != $last_date): ?>
<td><?php echo $result[$i]->date; ?></td>
<?php $last_date = $result[$i]->date; ?>
<?php else: ?>
<td></td>
<?php endif;?>
<td><?php echo $result[$i]->time; ?></td>
<td><?php echo $result[$i]->activity; ?></td>
</tr>
<?php endfor; ?>
I have just giving you an example, i hope you can solve your problem now.
Related
I want to loop through with all main categories. This is to use where-condition to check which categories have the id on main category and output it.
The DB look like this:
id | name | parent_ids
10 | Cat1 |
12 | Cat3 |
17 | Cat2 | 10,12
25 | Cat4 | 12
#foreach($lagerMainCategories-\>where('parent_ids', $lagerMainCategory-\>id) AS $lagerSubCategories)
<li>
{{$lagerSubCategories-\>name}} ({{$lagerSubCategories-\>id}})</li>
#include('Skote.layouts.lagerCategory.lagerSubCategory', \['lagerMainCategory' =\> $lagerSubCategories\])
#endforeach
I get out
Cat1
Cat3
Cat4
I want to
Cat1
Cat2
Cat3
Cat2
Cat4
Ok, thanks for Help. I have found a solution.
#foreach($lagerMainCategories->where('parent_ids', '!=', null) AS $lagerSubCategories)
#if(in_array($lagerMainCategory->id, explode(',', $lagerSubCategories->parent_ids)))
<li>{{$lagerSubCategories->name}} ({{$lagerSubCategories->id}}) </li>
#include('Skote.layouts.lagerCategory.lagerSubCategory', ['lagerMainCategory' => $lagerSubCategories])
#endif
#endforeach
All of this has worked fine for a year, however now I have to separate my goals on the same page between 2020 and 2021. I have "goal_year" as my column within the "goals_table" database table.
I tried to create 2 separate cells where the code pulls in goals for 2020 and goals for 2021 but it makes the entire page show blank.
I'm trying to do this without creating a separate model and controller file.
DATABASE
id | district | goal_year | goal_af_am_msm | goal_b | goal_c
---------------------------------------------------------------
1 | macon | 2020 | 456 | 87 | 234
2 | cobb | 2020 | 987 | 34 | 762
3 | macon | 2021 | 432 | 67 | 819
4 | fulton | 2021 | 843 | 21 | 236
Here's my code:
MODEL
function _goal()
{
$this->db->where('district', 'macon');
$query = $this->db->get('goals_table');
return $query->row();
}
CONTROLLER
public function index()
{
$data['_goal_data'] = $this->Macon_model->_goal(); // GOALS
$this->template->load('admin', 'default', 'macon', $data);
}
VIEW
<td>
<?php echo $_goal_data->goal_af_am_msm; ?> <!-- ENTERED GOALS -->
</td>
ALTERNATE VIEWS THAT I TRIED
1).
<td>
<?php echo $_goal_data->goal_af_am_msm->where('goal_year','2020'); ?>
<!-- Shows entire page blank -->
</td>
2).
<td>
<?php if($_goal_data->goal_year == '2020') echo $_goal_data->goal_af_am_msm ; ?>
<!-- Shows 2020 GOALS but show blank for 2021 -->
</td>
3).
<td>
<?php echo $_goal_data->goal_year == '2020' ? $_goal_data->goal_af_am_msm : ''; ?>
<!-- Shows 2020 GOALS but show blank for 2021 -->
</td>
So I kept playing around with it and I got it figured out. I ended up having to change my model from
return $query->row();
to
return $query->result();
Then I had to update my view file to
<?php foreach($_goal_data as $macon_data) : ?>
<?php if($macon_data->goal_year == '2020') echo $macon_data->goal_af_am_msm; ?>
<?php endforeach; ?>
I want to show product sizes related to product I already have made a relation b/w tables into database i just only want to know that when i click the specific product the size related to that product will be shown in the table.inside function how to make joining b/w three tables please make the join table using DB class because it will be easy to understand for me thanks.
Does Anyone have an idea ?
please check product sizes image
https://i.stack.imgur.com/Yt6Jk.png
Database
product_sizes table
----------------------------------------------------
id | size_name | Body_length | Body_width |
----------------------------------------------------
1 | M | 2.3 | 3.4
2 | L | .5 | 4.4
3 | s | 2.5 | 3.2
4 | xs | 3.5 | 2.4
products table
----------------------------------------
id | product_name | Description |
----------------------------------------
1 Ultraclub 1 | Ultraclub 300 |
2 Ultraclub 2 | Ultraclub 200 |
3 Ultraclub 3 | Ultraclub 500 |
4 Ultraclub 4 | Ultraclub 600 |
5 Ultraclub 5 | Ultraclub 400 |
available_product_sizes table
----------------------------------------
id | product_id | product_size_id
----------------------------------------
1 | 2 | 1
2 | 3 | 4
3 | 4 | 3
4 | 1 | 2
5 | 2 | 3
Controller
public function single_product($product_slug){
$Sizes=DB::table('product_sizes')->get();
$single_product=DB::table('products')->where('product_slug',$product_slug)->get();
return view('front_end/single_product',compact('single_product','Sizes'));
}
html view
<div class="product-tabs__pane product-tabs__pane--active" id="chart">
<table class="table table-bordered text-center">
<tr style="background-color:#3366cc;color:white">
<td><b>Sizes</b>
</td>
<td><b>Body length</b>
</td>
<td><b>Body width</b>
</td>
<td><b>Sleeve length</b>
</td>
</tr>
#foreach($Sizes as $size)
<tr>
<td><b>{{$size->sizes_name}}</b>
</td>
<td>{{$size->Body_length}}</td>
<td>{{$size->Body_width}}</td>
<td>{{$size->Sleeve_length}}</td>
</tr>
#endforeach
</table>
</div>
I assume you're already has/desfined many-to-many relationship between products and product_sizes tables
//App\Product.php
public function sizes()
{
return $this->belongsToMany('App\ProductSize', 'available_product_sizes', 'product_id', 'product_size_id');
}
//App\ProductSize.php
public function products()
{
return $this->belongsToMany('App\Product', 'available_product_sizes', 'product_size_id', 'product_id');
}
In order to get available product sizes you can use :
$sizes = App\Product::find(1)->sizes()->orderBy('size_name')->get();
Controller fix
public function single_product($product_slug) {
$single_product = Product::with('sizes')->where('product_slug',$product_slug)->first();
return view('front_end/single_product',compact('single_product'));
}
View Fix
//...
#foreach($single_product->sizes as $size)
<tr>
<td>
<b>{{$size->sizes_name}}</b>
</td>
<td>{{$size->Body_length}}</td>
<td>{{$size->Body_width}}</td>
<td>{{$size->Sleeve_length}}</td>
</tr>
#endforeach
//...
Answer to the question without using eloquent
use Illuminate\Support\Facades\DB; //include this
$products = DB::table('products as p')
->join('available_product_sizes as aps','aps.product_id','p.id')
->join('product_sizes ps','ps.id','aps.product_size_id')
->where('product_slug',$product_slug)->get();
But it is highly recommended to use eloquent relationships.
I am asked to load data from a single table like the following:
=================================
|id |dept | response | created_at |
=================================
|1 |FO | GOOD | 2018-05-20 |
|2 |FO | GOOD | 2018-05-20 |
|3 |IT | GOOD | 2018-05-20 |
|4 |FO | BAD | 2018-05-20 |
|5 |IT | GOOD | 2018-05-20 |
|6 |LO | BAD | 2018-05-20 |
|7 |IT | GOOD | 2018-05-20 |
|8 |IT | GOOD | 2018-05-21 |
|9 |LO | GOOD | 2018-05-21 |
=================================
they want me to display only records created_at 2018-05-20. and the desired output display in blade is like this:
FO
GOOD 2
BAD 1
IT
GOOD 3
BAD 0
LO
GOOD 0
BAD 1
when I use select distinct, it displays only one row of response data (only GOOD). The same thing happens when I use groupBy
here are my current codes:
Controller:
$date = new Carbon('2018-05-20');
$result = Result::select('dept', 'response')->where('created_at', '=', $date->toDateString())->groupby('dept')->get();
blade:
#foreach($result as $result)
{{$result->dept}}</BR>
{{$result->response}}</br></br>
#endforeach
The output I get:
FO
GOOD
IT
GOOD
LO
GOOD
I don't do the count yet because I still get this undesired result.
Is there a way to achieve the desired request with that single table?
EDIT I:
Based on Yrv16's answer, my controller goes like this:
namespace App\Http\Controllers;
use App\Result;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
public function index()
{
$date = new Carbon('2018-05-20');
$results = Result::select('dept', 'response', \DB::raw('COUNT(*) as count'))
->where('created_at', '=',$date->toDateString())
->groupby('dept','response')
->get();
$results = $results->groupBy('dept');
return view('result.index', compact ('results'));
}
and my blade:
#foreach($results as $result)
{{$result->dept}}</BR>
{{$result->response}}</br></br>
#endforeach
this gives me an empty page. but when i change this where('created_at', '=', $date->toDateString()) to where('created_at', '>=', $date->toDateString()) i get this error Property [dept] does not exist on this collection instance.
Use groupBy for two columns dept, response:
$date = new Carbon('2018-05-20');
$results = Result::select('dept', 'response', \DB::raw('COUNT(*) as count'))
->where('created_at', '=',$date->toDateString())
->groupby('dept','response')
->get();
Also result collection you can groupBy('dept') in order to display it like you want:
$results = $results->groupBy('dept');
Blade:
#foreach($results as $dept => $result)
{{$dept}}</BR>
#foreach($result as $item)
{{$item->response}} {{ $item->count }}
#endforeach
#endforeach
You can use collection manipulation for that:
$results = Result::where('created_at', '2018-05-20')
->get()
->groupBy('dept')
->map(function ($item) {
return $item->groupBy('response')->map(function ($responses) {
return $responses->count();
});
});
It will give you the following results:
print_r($results->toArray());
Array (
[FO] => Array (
[GOOD] => 2
[BAD] => 1
)
[IT] => Array (
[GOOD] => 3
)
[LO] => Array (
[BAD] => 1
)
)
Where is Magento registration form data stored? In which database table(s)?
to find out query used to get the customer data do this, and this is probably EAV structure where all data is kept:
<?php echo Mage::getModel('customer/customer')->getCollection()->getSelect();?>
or this
<?php echo Mage::getModel('customer/customer')->load('customerid')->getSelect();?>
and to find out what fields are available on default collection or load:
<?php print_r(Mage::getModel('customer/customer')->getCollection()->getFirstItem()->getData());?>
or this
<?php print_r(Mage::getModel('customer/customer')->load('customerid')->getData());?>
Anton's answer is correct for accessing data through the Magento framework. To answer your question literally, the following tables store the basic customer data:
+----------------------------------+
| Tables_in_entp (customer%) |
+----------------------------------+
| customer_address_entity |
| customer_address_entity_datetime |
| customer_address_entity_decimal |
| customer_address_entity_int |
| customer_address_entity_text |
| customer_address_entity_varchar |
| customer_entity |
| customer_entity_datetime |
| customer_entity_decimal |
| customer_entity_int |
| customer_entity_text |
| customer_entity_varchar |
+----------------------------------+