Yii2 Basic- How to display data table in looping using model? - php

I'm trying to display a loop of data (in table form) for child data to be display in parent view. Below is the code for the table view:
//display child data loop
<div class="col-md-6 col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Details</h3>
</div>
<div class="box-body">
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th class="text-center">Code</th>
<th>Part Number</th>
<th>Quantity</th>
<th>Size</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
foreach($modelsMaintenanceItem as $i => $modelMaintenanceItem)
{
?>
<tr>
<td class="text-center"><?php echo $modelMaintenanceItem->Code; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Part_Number; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Quantity; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Size; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Description; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
Inside controller for function actionView, the code are as below:
public function actionView($id)
{
$searchModel = new MaintenanceSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
// $model = new Maintenance();
$modelsMaintenanceItem = [new MaintenanceItem]; //pass model to be use in view page
return $this->render('view', [
'model' => $this->findModel($id),
'modelsMaintenanceItem' => $this->findModel($id),
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
// 'modelsMaintenanceItem' => (empty($modelsMaintenanceItem)) ? [new MaintenanceItem] : $modelsMaintenanceItem //pass model to be use in view page
]);
}
Help me please... I'm Stuck here. I think I need to do some changes in controller for function actionView, about 'modelsMaintenanceItem' but I'm not sure what should I put there.

If you want really accessing to dataProvider models you should use
$models = $dataProvider->getModels();
and for html code inside a foreach, suggest also the echo of the html code or use the alternative syntax for control structures eg: <?php foreach(...) : ?> ... <?php endforeach; ?> syntax
eg:
<?php
$models = $dataProvider->getModels();
foreach($models as $i => $modelMaintenanceItem):
?>
<tr>
<td class="text-center"><?php echo $modelMaintenanceItem->Code; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Part_Number; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Quantity; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Size; ?></td>
<td class="text-center"><?php echo $modelMaintenanceItem->Description; ?></td>
</tr>
<?php endforeach; ?>
anyway the simplest way for show a table in Yii2 is based on gridview
<?php
use yii\grid\GridView;
?>
...
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'Code',
'Part_Number',
'Quantity',
'Size',
'Description',
],
]); ?>

Related

Cakephp 3.6.14: Create and submit form with table and checkbox for each row (2nd form - same page)

I want to implement 2 different forms in a page for 2 different actions in the same controller. I am trying to create the second form where the user can check a checkbox for each row of a table. On submit of the form I want to execute some code in the add action of the controller.
This is my AddElement\index.ctp :
<div class="row"> //First form is working fine
<?php echo $this->Form->create('AddElement', array('action' => 'index')); ?>
<?php echo $this->Form->control('element_categories', ['options' => $elementCategories, 'empty' => true]); ?>
<?php echo $this->Form->button(__('Filter'), ['class'=>'btn-success']); ?>
<?php echo $this->Form->end(); ?>
</div>
<h3><?= __('Task Elements') ?></h3>//2nd form not working as expected
<?php echo $this->Form->create('AddElement', array('action' => 'add')); ?>
<?php foreach ($taskElements as $taskElement): ?>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('name') ?></th>
<th scope="col"><?= $this->Paginator->sort('element_category_id') ?></th>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
<tr>
<td><?= $this->Number->format($taskElement->id) ?></td>
<td><?= h($taskElement->name) ?></td>
<td><?= $taskElement->element_category_id != 0 ? $this->Html->link($taskElement->element_category->name, ['controller' => 'ElementCategories', 'action' => 'view', $taskElement->element_category->id]) : '' ?></td>
<td><?= $this->Form->control('selected', ['type' => 'checkbox']);?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table><?php
echo $this->Form->button('Add');
echo $this->Form->end();?>
And this is the add action of the AddElementController.php:
public function add($tasktypeid)
{
$this->autoRender = false; //no need for view
debug($this->request->data());
if($this->request->is('post'))
{
debug($this->request->data());
}
}
But when I click submit it does nothing. Its just loading for a second but it doesn't execute the code in the controller.

How to get total number of records retrieved with pagination in CakePHP 3?

I have a problem in CakePHP 3 to have pagination correctly.
How to get the total number of records retrieved with pagination in CakePHP 3?
This might help you.
In your controller i.e UsersController.php make pagination like this:
public function index()
{
//set your pagination limit 10
$this->paginate = [
'limit' => 10
];
//query all users
$users = $this->paginate($this->Users->find('all'));
//pass to template
$this->set(compact('users'));
}
In you Template View i.e users/index.ctp put this code to display your pagination
<div class="users index large-9 medium-8 columns content">
<h3><?= __('Users') ?></h3>
<table cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('fullname') ?></th>
<th scope="col"><?= $this->Paginator->sort('email') ?></th>
<th scope="col"><?= $this->Paginator->sort('handphone') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $this->Number->format($user->uid) ?></td>
<td><?= h($user->fullname) ?></td>
<td><?= h($user->email) ?></td>
<td><?= h($user->handphone) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p>
</div>
Change above variable accordingly to match your table entities (columns' name).
TIPS: If you use CakePHP bake command, you will get all this
pagination by default

I can't find user by id all my valors is empty in cake php

hello i need find user by id for my reports this is my code.
Now in this part of my code.
<?php echo $this->Html->link('soya existencia', array('controller' => 'Reportesoyaexistencias', 'action' => 'export_xls', $soyareporte['Soyareporte']['id'])); ?>
I set the id of user because just a need the report of this user.In my index just I do a link,this link set my id to reporteexistenciassoya where i need that just show me the id sent and all records . this is all code:
SoyareportesController.php
index.ctp
<table>
<thead>
<tr>
<th><?php echo $this->Paginator->sort('username', 'Nombre de Usuario');?> </th>
<th><?php echo $this->Paginator->sort('nombre', 'Nombre (s)');?> </th>
<th><?php echo $this->Paginator->sort('apellido', 'Apellido (s)');?> </th>
<th><?php echo $this->Paginator->sort('email', 'Email');?></th>
<th><?php echo $this->Paginator->sort('Grupo.categoria', 'Categoria');?></th>
<th><?php echo $this->Paginator->sort('Grupo.subcategoria', 'Sub-Categoria');?></th>
<th><?php echo $this->Paginator->sort('Perfil.codigogeneral', 'Codigo General');?></th>
<th>Soya report</th>
</tr>
</thead>
<tbody>
<?php $count=0; ?>
<?php foreach ($soyareportes as $soyareporte): ?>
<?php $count ++;?>
<?php if($count % 2): echo '<tr>'; else: echo '<tr class="zebra">' ?>
<?php endif; ?>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['username']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Soyareporte']['email']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Grupo']['subcategoria']; ?></td>
<td style="text-align: center;"><?php echo $soyareporte['Perfil']['codigogeneral']; ?></td>
<td><?php echo $this->Html->link('soya existencia', array('controller' => 'Reportesoyaexistencias', 'action' => 'export_xls', $soyareporte['Soyareporte']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
<?php unset($soyarreporte); ?>
</tbody>
</table>
Now in this part of my code. I receipt values the id but when I want to display the valors, but all values ​​are empty.
Reportesoyaexistencias.php
<?php
class ReportesoyaexistenciasController extends AppController {
var $name = 'Reportesoyaexistencias';
function export_xls($id = null) {
$this->loadModel("Reportesoyaexistencia");
//$this->Reportesoyaexistencia->recursive = 1;
$data = $this->Reportesoyaexistencia->findById($id);
if (!$this->request->data) {
$this->request->data = $data;
}
$this->set('soyaexistencias',$data);
//$this->render('export_xls','export_xls');
}
}
?>

cakephp value isnt sent to action

I have the following action:
public function admin_website_status($id = null){
$i = 0;
$this->Paginator->settings = array(
'Website' => array(
)
);
$this->set('websites',$this->Paginator->paginate('Website'));
}
Now i want to send a value to this action so in my view i have the following link:
<?php echo $this->Html->link(__('Active'), array('action' => 'website_status', $website['Website']['website_id'])); ?>
When i debug this no value is sent even though i know that the value should be 1
So what am i doing wrong?
My full view
<div class="portlet box red">
<div class="portlet-title">
<div class="caption"><i class="icon-globe"></i>Website Status</div>
</div>
<div class="portlet-body">
<table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('Website.domain', 'Website domain');?></th>
<th><?php echo $this->Paginator->sort('Website.client_id', 'Affiliate ID');?></th>
<th><?php echo $this->Paginator->sort('User.username', 'E-mail Address');?></th>
<th><?php echo $this->Paginator->sort('Status.status', 'Status'); ?> </th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
</thead>
<?php foreach ($websites as $site): ?>
<tr>
<td><?php echo $site['Website']['domain']; ?></td>
<td><?php echo $site['Client']['client_id'];?></td>
<td><?php echo $site['Client']['user_id'];?></td>
<td><?php echo $site['Status']['Status']; ?></td>
<td class="actions">
<?php echo $this->Html->link(__('Active'), array('action' => 'website_status', $website['Website']['website_id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<div class="paging">
<?php
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
?>
</div>
</div>
$websites['Website']['website_id']
and not
$website['Website']['website_id']
(plural and not singular)
edit: after seeing your whole code
$site['Website']['website_id']
In your $this->Html->link you set 'action' => 'website_status'
Is it supposed to be admin_website_status ?
Also make sure $website['Website']['website_id'] has a value. Maybe it is empty.

Yii form fields rendered twice

The fields of my yii form in the register page are rendered twice!!
can anybody pls help me find out what's causing this?
here is the code from the controller:
public function actionRegister()
{
$form=new Users;
// collect user input data
if(isset($_POST['Users']))
{
$form->attributes=$_POST['Users']; // set all attributes with post
// NOTE Changes to any $form->value have to be performed BEFORE $form-validate()
// or else it won't save to the database.
// validate user input and redirect to previous page if valid
if($form->validate())
{
// save user registration
$form->save();
$this->redirect(Yii::app()->createUrl('site/login'));
}
}
// display the registration form
$this->render('register',array('form'=>$form));
}
here is the code from the view:
<?php $pagetitle = "Hubylon | Register"; ?>
<?php $this->breadcrumbs=array('Register'); ?>
<div class="yiiForm">
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($form); ?>
<div class="row">
<table>
<tr>
<td>*<?php echo CHtml::activeLabel($form,'username'); ?>:</td>
<td><?php echo CHtml::activeTextField($form,'username'); ?></td>
<td><?php echo CHtml::error($form,'username',array('style' => 'color:red;font-size:11px;')); ?></td>
</tr>
</table>
</div>
<div class="row">
<table>
<tr>
<td>*<?php echo CHtml::activeLabel($form,'password'); ?>:</td>
<td><?php echo CHtml::activePasswordField($form,'password'); ?></td>
<td><?php echo CHtml::error($form,'password',array('style' => 'color:red;font-size:11px;')); ?></td>
</tr>
</table>
</div>
<div class="row">
<table>
<tr>
<td>*<?php echo CHtml::activeLabel($form,'confirmPassword'); ?>:</td>
<td><?php echo CHtml::activePasswordField($form,'confirmPassword'); ?></td>
<td><?php echo CHtml::error($form,'confirmPassword',array('style' => 'color:red;font-size:11px;')); ?></td>
</tr>
</table>
</div>
<div class="row">
<table>
<tr>
<td><?php echo CHtml::activeLabel($form,'first_name'); ?>:</td>
<td><?php echo CHtml::activeTextField($form,'first_name'); ?></td>
<td><?php echo CHtml::error($form,'first_name',array('style' => 'color:red;font-size:11px;')); ?></td>
</tr>
</table>
</div>
<div class="row">
<table>
<tr>
<td><?php echo CHtml::activeLabel($form,'birthdate'); ?>:</td>
<td><?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'attribute'=>'birthdate',
'model' => $form,
//'language'=>Yii::app()->language=='en' ? 'en' : null,
'options'=>array(
'dateFormat'=>'yy-mm-dd',
'defaultDate'=>$form->birthdate,
'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
'buttomImageOnly'=>true,
'showAnim'=>'fold',
'showOn'=>'button',
'yearRange'=>'1900',),
'htmlOptions'=>array(
'style'=>'width:80px;vertical-align:top'
),
));
?></td>
</tr>
</table>
</div>
<div class="action">
<?php echo CHtml::submitButton('Register'); ?>
</div>
<?php echo CHtml::endForm(); ?>
thanks!
seems there's no problem from the code you have given,
but maybe you can checkout the layouts and the init() method of the current controller.
i managed to resolve it by adding the following line before the render:
$this->layout = '//layouts/login';
thanks!

Categories