Iterate tr fmod 2 in Jade - laravel-5

I have a problem for iterate td into tr using fmod() in Jade using elixir jade in Laravel 5
<?php $i = 1; ?>
#foreach($fields_base as $field)
#if(fmod($i,2))
tr.left
#else
td.bold Name:
td Smith
#endif
<?php $i++; ?>
#endforeach
But when jade compile this I have
#foreach($fields_base as $field)
#if(fmod($i,2))
<tr class="left"></tr>
#else
<td class="bold">Name:</td>
<td>Smith</td>
#endif
#endforeach
The td not into tr, HOw I can make this in Jade template :( I need this result
#foreach($fields_base as $field)
#if(fmod($i,2))
<tr class="left">
#endif
<td class="bold">Name:</td>
<td>Smith</td>
#if(!fmod($i,2))
</tr>
#endif
#endforeach
Based this into this example:
if (fmod($num,2)) echo '<tr>';
echo "<td>" . $num . "</td>";
if (!fmod($num,2)) echo '</tr>';
Regards!

Related

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.

Issue in Laravel pagination

I am trying to apply pagination on my laravel project. But when I put pagination code, it makes numeric pagination and when I click button for next page to show other data. Its does not work. My URL right now is
: http://localhost/buildcrm/public/staff
and here is my URL when I click for the second page : http://localhost/staff?page=2 .
Here is my pagination code:
<?php
$users = DB::table('staff')->paginate(4);
$i = 0;
foreach($users as $row) {
$city =$row->city.','.$row->pincode;
$name = explode(',',$row->name);
$i = $i + 1;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $name[0]; ?> <?php echo $name[1]; ?> <?php echo $name[2]; ?></td>
<td>
Delete
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<div align="center">
<?php echo $users->render(); ?>
</div>
Did you try setting custom path like this?
$users = DB::table('staff')->paginate(4);
$users->setPath('custom/url');

Laravel Eloquent Group By Dates

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

Codeigniter include file variables come null

I have a foreach inside a table that loads a file with a row <tr>.
That problem is that all the variables come null and I've already tried to send the variable with the load as you can see in the next code line but no success:
$this->load->view('pedidos/tarefas/tarefa_table', $task);
This is my table:
<table>
<thead>
<tr>
<th>ID</th>
<th>Utilizador</th>
<th>Entidade</th>
<th>Descrição</th>
<th>Data Limite</th>
<th>Estado</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
<?php
foreach ($tasks as $task) {
$this->load->view('pedidos/tarefas/tarefa_table', $task);
}
?>
</tbody>
</table>
This is the file I'm including in the load->view:
tarefa_table.php
<tr class="odd gradeX">
<td><?php print_r($task->id); ?></td>
<td><?php echo substr($task->user->name, 0, 20); if(intval(strlen($task->user->name)>=20))echo ".."; ?></td>
<td><?php echo substr($task->entidade->name, 0, 20); if(intval(strlen($task->entidade->name)>=20))echo ".."; ?></td>
<td><?php echo substr($task->descricao, 0, 30); if(intval(strlen($task->descricao)>=30))echo ".."; ?></td>
<td style="<?php
if ($task->status_id <= 2 || $task->status_id >= 6) {
if (date('Y-m-d', strtotime($task->data_fim)) == date('Y-m-d')) {
echo "color: orange";
} elseif (date('Y-m-d', strtotime($task->data_fim)) < date('Y-m-d')) {
echo "color: red";
}else{
echo "color:#274156";
}
} ?>"><?php echo date('d-m-Y', strtotime($task->data_fim)) ?>
</td>
<td><?php
if($task->status_id <= 2 && date('Y-m-d', strtotime($task->data_fim)) < date('Y-m-d')){
echo "Atraso";
}else{
switch ($task->status_id) {
case 1: echo "Ativo"; break;
case 2: echo "Atraso"; break;
case 3: echo "Pendente"; break;
case 4: echo "Concluído"; break;
case 5: echo "Cancelado"; break;
case 6: echo "Ativo"; break;
case 7: echo "Atraso"; break;
default: echo "Não Definido"; break;
}}?>
</td>
<td class="center">
<a href="<?php echo base_url() . 'pedidos/view_task/' . $task->id ?>" title="Ver" class="tip">
<button class="btn btn-link btn-mini"><span class="icon16 icomoon-icon-eye"></button>
</a>
<a href="<?php echo base_url() . 'pedidos/edit_task/' . $task->id ?>" title="Editar" class="tip">
<button class="btn btn-link btn-mini"><span class="icon16 icomoon-icon-pencil-5"></button>
</a>
<!--<a href="<?php //echo base_url() . 'pedidos/deletetask/' . $task->id ?>" class="confirm-delete tip" title="Apagar">
<button class="btn btn-link btn-mini"><span class="icon16 typ-icon-trashcan red"></button>
</a>-->
</td>
</tr>
why not load a view in controller , loop through your data and append in your variable like
$tablecontent and just echo it in your first view
//controller
function your_fucntion(){
$tablecontent="";
foreach ($tasks as $task) {
$tablecontent.=$this->load->view('pedidos/tarefas/tarefa_table', $task,true);
}
$data['tablecontent']=$tablecontent;
$this->load->view('your view path', $data);
}
//view
<table>
<thead>
<tr>
<th>ID</th>
<th>Utilizador</th>
<th>Entidade</th>
<th>Descrição</th>
<th>Data Limite</th>
<th>Estado</th>
<th>Ações</th>
</tr>
</thead>
<tbody>
<?php
echo $tablecontent;
?>
</tbody>
</table>

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