i'm try to foreach inside the foreach to get the count data with this code
Controller.php
$jadwals = DB::table('tb_jadwal')->get();
foreach ($jadwals as $key => $jadwal) {
$tgl = date("Y-m-d", strtotime($request->tgl));
$cek_tiket = array();
$cek_tiket = DB::table('tb_tiket')
->select(DB::raw('count( distinct id_tiket) as jumlah_kapasitas'))
->where('Tgl_Berangkat', '=', "'$tgl'")
->where('id_jadwal', '=', $jadwal->id_jadwal)
->get();
$jadwals[$key]->jumlah_kapasitas = $cek_tiket;
}
view.blade.php
#foreach($cek_tiket as $cek_tiket)
<?php
if ($cek_tiket->jumlah_kapasitas <= 70) {
echo "There data";
dd($cek_tiket);
}
?>
#endforeach
but the result of dd is 0
Related
I am trying to update a laravel collection with another collection. Here are my collections :
$acc = DB::table('accounts')
->where('accounts.branchid', $branch_id)
->select('code','title','opbal','clbal')->get();
$rslt = DB::table('journal')
->where('journal.branchid', $branch_id)
->where(function($q) {$q->where('journal.cancel','!=',1)->orWhereNull('journal.cancel');})
->select('code',DB::raw('sum(IFNULL(dr,0) - IFNULL(cr,0)) as total'))->groupBy('code')->Get('code','total');
What I want is to update $acc->clbal with $rslt->total where $acc->code = $rslt->code
I tried this but it does not work
foreach ($rslt as $row) {
$getacc = $acc->where('code',$row->code);
$getacc->clbal = $row->total;
}
Here is the solution.
$acc = DB::table('accounts')
->where('accounts.branchid', $branch_id)
->select('code','title','opbal','clbal')->get();
$rslt = DB::table('journal')
->where('journal.branchid', $branch_id)
->where(function($q) {$q->where('journal.cancel','!=',1)->orWhereNull('journal.cancel');})
->select('code',DB::raw('sum(IFNULL(dr,0) - IFNULL(cr,0)) as total'))->groupBy('code')->Get('code','total');
foreach ($rslt as $row) {
$getacc = $acc->where('code',$row->code)->first();
$getacc->update(['clbal' => $row->total]);
}
I want to work on a while loop in a foreach loop but I can't convert it to Laravel controller system. Please help.
foreach (range('a', 'z') as $i)
{
echo $i = strtolower($i);
$sql = "SELECT * FROM `list` Where name like '$i%' Limit 30";
$result = mysqli_query($con, $sql);
echo"<hr><h3>Name Starting with ".strtoupper($i)."</h3>";
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = $row['id'];
$name = $row['name'];
echo "<li>".$row['name']."</li>";
}
echo "<li>More</li>";
echo "</ul>";
}
The code in Laravel that I have is
foreach (range('a', 'z') as $i)
{
$list = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
return view('pages.all', ['list' => $list]);
}
This code only gives data for names starting with alphabet "a" but won't proceed with the rest of the characters.
Store it on the array based on your search.
$list = array();
foreach (range('a', 'z') as $i) {
$list[$i] = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
}
return view('pages.all', ['list' => $list]);
Customize your view, to showing the list against each character (a-z).
[Edit]
View file can be changed like below,
#foreach ($list as $keyI => $moredata)
<hr>
<h3>Author's Name Starting with "{{strtoupper($keyI)}}"</h3>
<ul class="tags-list">
#foreach ($moredata as $data)
<li>{{$data->name}}</li>
#endforeach
</ul>
#endforeach
Iam try to run a query in codeigniter model.Its working but when I echo model function query is like below.
SELECT * FROM `table1` WHERE `id` = '17'
SELECT * FROM `table1` WHERE `id` = '20'
SELECT * FROM `table1` WHERE `id` = '21'
SELECT * FROM `table1` WHERE `id` = '22'
SELECT * FROM `table1` WHERE `id` = '23'
My model function is given below
function get_quick_navi_menu($q_code)
{
$this->db->select("*");
$this->db->where('q_id',$q_code);
$this->db->from("table0");
$q = $this->db->get();
//echo $this->db->last_query();
$final = array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $row) {
$this->db->select("*");
$this->db->from("table1");
$this->db->where("id",$row->id);
$q = $this->db->get();
echo $this->db->last_query();
if ($q->num_rows() > 0) {
$row->children = $q->result();
}
array_push($final, $row);
}
}
I want to run query like below
SELECT * FROM `table1` WHERE `id` = '17,18,19..'
Table Structure
Table0
id q_id value1
1 2 4
2 2 5
3 2 6
Table1
t1_id id value1 value2
1 1 2 2
2 2 5 6
3 3 8 12
View
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1" style="margin-left: 1px; opacity: .9;">
<?php foreach ($menus as $menu) { ?>
<li class="dropdown-submenu"><?php echo $menu->title;?>
<ul class="dropdown-menu"> <?php
if (isset($menu->children)) {
foreach ($menu->children as $child) {?>
<li><?php echo $child->menu_item;?></li> <?php
}
}
?></ul></li><?php } ?>
</ul>
Controller
$menus = $this->Home_model->get_quick_navi_menu($q_code);
$data = array('menus' => $menus);
Required Output
Selecting value1 and value2 from table1 according to id from table0.
How to solve this please help.
Use this function in controller
public function getTableData()
{
$this->db->select('GROUP_CONCAT(id) as id');
$tbl0 = $this->db->get('table0')->row_array();
if($tbl0) {
$ids = explode(',', $tbl0['id']);
$this->db->where_in('id', $ids);
$tbl1 = $this->db->get('table1')->result_array();
echo "<pre>"; print_r($tbl1);
}
}
Hope this will help you;
Get all ids in an array name here ids and use where_in outside the loop
public function get_quick_navi_menu($q_code)
{
$this->db->select("*");
$this->db->where('q_id',$q_code);
$this->db->from("table0");
$q = $this->db->get();
$final = array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $key => $row)
{
$ids[$key] = $row->id;
$data[$key] = $row;
}
$this->db->select("*");
$this->db->from("table1");
$this->db->where_in("id",$ids);
$q = $this->db->get();
//echo $this->db->last_query();
if ($q->num_rows() > 0)
{
if ( ! empty($data))
{
foreach ($data as $key => $item)
{
$item->children = $q->result();
$final[] = $item;
}
}
}
}
return $final;
/*print_r($final);*/
}
In your controller class:
make sure you have loaded your model and database in controller or in autoload.php
$q_code = 'q_code_value';
$data['menus'] =$this->Home_model->get_quick_navi_menu($q_code);
/* pass the $data in the view like this*/
$this->load->view('your_view_file_path',$data);
In your view :
<div><?php print_r($records);?></div>
For more : https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data
I use the following code to get data from two related tables:
$arr = [];
$objectModel = new ProductCategory();
$objectModel::$language = 2;
$subcategories = $objectModel::with("translate", "parent")->get();
foreach($subcategories as $key => $item) {
$arr[$item->translate()->first()->objectId] = $item->translate()->first()->name;
}
array_unshift($arr, 'Select category');
return $arr;
In result this part of code I get array with key => value to insert this in select list in Blade template.
But I desire to escape a loop:
foreach($subcategories as $key => $item) {
$arr[$item->translate()->first()->objectId] = $item->translate()->first()->name;
}
And get clear collection from request. How can I do it?
You can use Laravel Collections https://laravel.com/docs/5.3/collections
$arr = ProductCategory::with("translate", "parent")->get()
->mapWithKeys(function ($item, $key) {
return [$item->translate()->first()->objectId => $item->translate()->first()->name];
})
->all();
I am using $data = $query->result_array(); but I would need to change my fields name from first_name to first name.
I looked up found this....
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
would I want to do something like this?
foreach ($query->result_array() as $row)
{
$row = str_replace("_", " ", $row);
}
$data = $row;
if you need then a new array you can easly do:
foreach ($query->result_array() as $key=> $value)
{
$data[] = array(
str_replace("_", " ", $key)=>$value
);
}