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.
Related
I have two tables
users
id
name
posts
id
name
user_id
All posts should be in the same column of the owner user of these posts, but the foreach in laravel print just in rows and cant thinks any way to do in this way.
The code is:
<?php
$search = \App\Post::all()->groupBy('user_id');
$total_users = \App\User::count();
$total_posts = 0;
?>
<table>
<tbody>
#foreach($search as $user_id => $posts)
#if ($loop->first)
<tr>
#endif
<td>
{{ $posts[0]->user->name }}
<?php $total_posts += $posts->count(); ?>
</td>
#if ($loop->last)
</tr>
#endif
#endforeach
#for($c = 0; $c < floor($total_posts/$total_users); $c++)
<?php
$users_done = 0;
$tr_done = false;
?>
#foreach($search as $user_id => $posts)
#if(isset($posts[$c]))
#if ($loop->first)
<tr>
#endif
<td>{{ $posts[$c]->name }}</td>
<?php
$users_done++;
$g++;
?>
#if ($loop->last)
<?php
$tr_done = true;
?>
</tr>
#endif
#endif
#endforeach
#if(!$tr_done)
</tr>
#endif
#if($user_done>0 && $users_done<$total_users)
#for($c = 0; $c < $total_users-$users_done; $c++)
<tr><td></td></tr>
#endfor
#endif
#endfor
</tbody>
</table>
I have 2k posts rows and i cant to print in this way!
Any help Please!!!!
You can try creating table like this?
$table = [];
$users = 0;
$posts = 0;
foreach($users as $user){
foreach($user->posts){
$table[$posts][$users] = $post->name;
$posts++;
}
$users++;
}
i am new to Codeigniter, i have two tables named as Users and Amount, there are multiple amounts in Amount Table across one user have same user_id which is foreign key. i want to get data and sum up the amounts value if user_id is same.
My Modal is:
public function client_list(){
$this->db->select_sum('*', sum('amount'));
$this->db->from('users');
$this->db->join('amount', 'amount.user_id = users.ID');
$query=$this->db->get();
$result=$query->result();
return $result;
}
Controller is:
public function index(){
$data['records'] = $this->Admin_model->client_list();
$this->load->view('admin/Client_list', $data);
}
View part is:
<?php
foreach($records as $record):
?>
<tr class="odd gradeX">
<td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes" value="1" />
<span></span>
</label>
</td>
<td> <?php echo $record->firstname; ?> </td>
<td> <?php echo $record->lastname; ?> </td>
<td> <?php echo $record->phone;?> </td>
<td> <?php echo $record->email;?> </td>
<td> <?php echo $record->amount;?> </td>
<td> <?php echo $record->dateofbirth;?> </td>
</tr>
<?php endforeach; ?>
Kind Help would be highly appreciated.
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
I have a problem in cakephp
controller
$due = $this->Issue->find('all' , array('conditions' => array ('Issue.user_id'=>6)));
$this->set('due',$due);
$id = $due['0']['Issue']['book_id'];
$b = $this->Bookmaster->find('first' , array ('conditions' => array ('Bookmaster.id' => $bookid)));
$book = $this->Book->find('all',array('conditions' => array ('Book.id' =>$id)));
$bookid = $book['0']['Book']['bookmaster_id'];
Ctp contain
<h1> ISSUED BOOK DETAILS </h1>
<table width="200" border="2" bordercolor="#009933">
<tr>
<td> BOOK ID </td>
<td> TITLE </td>
<td> AUTHOR </td>
<td> EDITION </td>
<td>ISSUED ON </td>
<td> DUE DATE </td>
<td> POTENTIAL FINE</td>
</tr>
<?php foreach ($due as $data ) { ?> <tr>
<td> <?php echo $data['Issue']['book_id']; ?> </td>
<td> <?php echo $b ['Bookmaster']['title'] ;?></td>
<td> <?php echo $b['Bookmaster']['author'];?></td>
<td> <?php echo $b ['Bookmaster']['edition'];?> </td>
<td> <?php echo $data['Issue']['issue_date_time']; ?> </td>
<td> <?php echo $data['Issue']['due_date']; ?> </td>
<td> <?php echo $out['Fine']['fineamount'];?> </td>
</tr> <?php }?>
</table>
My problem is how can I manipulate with this with for loop.
My loop is not iterating for bookmaster.. so how can I do that..
How can i increment the value of bookid
The problem is you are looping on $due, and the $b data is a key valued array (i.e. $b[0]['bd'], $b[1]['bd'], $b[2]['bd'], etc.) Therefore, the key (or the index) for the array you are searching for will not ever be aligned with the $due loop.
A better solution is to write a join so each record contains the information you require and the loop will be able to display the data you require. More requirements / information is required on your part to enable anyone here to offer a suggestion.
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>';
}