I have some datas stored in my table Jobs. How can I display them in a tabular form in my view section? I have visited the Yii forums but have not got any specific options to perform the action.
The action for view job:
public function actionViewJob() {
$criteria = "";
$model = new ViewJob();
/* What Should I do Here */
$this->render('viewjob', array(
'model' => $model
));
}
And the corresponding view to list data from database:
/* What should I do Here. */
<h1>View Jobs</h1>
<div class="flash-success"></div>
<div class="form">
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="filtertable" style="float: left;">
<thead>
<tr>
<th width="11%" class="rowtop">No</th>
<th width="24%" class="rowtop">Title</th>
<th width="10%" class="rowtop">Description</th>
<th width="10%" class="rowtop">No_Vacancy</th>
<th width="10%" class="rowtop">Contact Email</th>
<th width="10%" class="rowtop">Edit</th>
<th width="10%" class="rowtop">Delete</th>
</tr>
</thead>
<tbody>
<?php // foreach ($posts as $post): ?>
<tr>
<td width="11%">
<?php ?>
</td>
<td width="24%">
<?php ?>
</td>
<td width="14%">
<?php ?>
</td>
<td width="14%">
<?php ?>
</td>
<td width="14%">
<?php ?>
</td>
</a>
</td>
</a>
</td>
</tr>
<?php //endforeach; ?>
</tbody>
</table>
<!-- form -->
In your action, call your view.
public function actionViewJob() {
$model = new Job('search');
$params =array('model'=>$model,
);
$this->render('viewjob', $params);
}
Get your data as a CActiveDataProvider object.
In your model, create a search function
public function search() {
$criteria=new CDbCriteria;
// TODO: Add other search criteria here
$criteria->compare('username','Tom',true);
return new CActiveDataProvider('Jobs', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'username ASC',
),
));
}
Then, in your view
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'title',
'type' => 'raw',
'value' => 'CHtml::encode($data->title)'
),
array(
'name' => 'description',
'type' => 'raw',
'value' => 'description',
),
),
));
At first you should get table data in controller via CActiveDataProvider. And then in view you can use widget - CGridView. Using this widget you can set params that you need. Columns, filters etc. Also you can paste your code, and we will try to decide your problem. Yii is very easy :)
Related
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',
],
]); ?>
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.
I am trying to make selecting one checkbox from a total of four checkboxes required but it's not working. Whether I select a checkbox or not, it always passes validation.
Here is my method in my controller Cargo_inquiry() {}
function step_two() {
$country_to_select = $this->session->userdata('freight_address_country');
$this->data['users'] = $this->My_Users_Model->get_companies_list($country_to_select);
// load up the validation rules for blog Info form
$this->config->load('validate_cargo');
$this->form_validation->set_rules($this->config >item('validate_cargo_create_step_two') );
if ($this->form_validation->run('validate_cargo_create_step_two') === FALSE) {
echo "FALSE";
// die;
$this->load->view('_layouts/main/template1', $this->data);
} else {
echo "TRUE";
// die;
redirect('/Cargo_inquiry/step_three');
}
}
Here is my validation rules located in validate_cargo.php:
$config['validate_cargo_create_step_two'] = array(
'user_id' => array(
'field' => 'user_id',
'label' => '',
'rules' => 'required',
'errors' => array(
'required' => 'Please select at least one company.',
),
),
);
Here is my view/form:
<?php echo form_open('Cargo_inquiry/step_three',array('class'=>'form-horizontal'));?>
<table class="table table-bordered table-striped" id="mytable">
<thead>
<tr>
<th width="3%">Select</th>
<th>company</th>
<th>location</th>
<th>rating</th>
<th>web</th>
<th>Associations/serv_hhgpe</th>
</tr>
</thead>
<tbody>
<?php
foreach ($users as $user) { ?>
<tr>
<td>
<?php // echo form_checkbox('user_id[]', 'user_id[]', set_checkbox("user_id[]"), "Company"); ?>
<input type="checkbox" name="user_id" value="<?php echo $user->id; ?>" />
</td>
<td>
<?php echo $user->company ?>
</td>
<td>
<?php echo $user->city ?>
</td>
<td>
<?php echo $user->state_province ?>
</td>
<td>
<?php echo $user->name ?>
</td>
<td style="text-align:center" width="200px">
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo form_submit('cargo_create_step_two', 'Next->', 'class="btn btn-primary btn-lg btn-block"');?>
<?php echo form_close();?>
Arrrgg!!! I was submitting to the wrong form :( So very sorry.
Hey everyone I am facing this problem the form is not updating when I click submit. It goes in if($this->request->is("post")){....} block but it is not updating.
Here is the code.
PagesController.php
public function admin_edit($id=NULL){
$this->request->data = $this->Page->find("first",array("conditions"=>array("Page.id"=>$id)));
if($this->request->is('post')){
$this->request->data["Page"]["id"] = $id;
if($this->Page->save($this->data)){
echo "Done";
}
}
}
admin_edit.php
URL -> http://localhost/cakephp_practice/admin/pages/edit/4
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="DataTable">
<tr>
<th>Administrator > Edit Page</th>
</tr>
<tr>
<td>
<?php
echo $this->Form->create('Page', array("action" => "edit", "method" => "Post",'enctype' => 'multipart/form-data', 'id' => 'editPage'));
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"> <span class="require">* Please note that all fields that have an asterisk (*) are required. </span></td>
</tr>
<tr>
<td>Name: <font color="red">*</font></td>
<td align="left"><?php echo $this->Form->text('Page.name', array('maxlength' => '254', 'size' => '20', 'label' => '', 'div' => false, 'class' => "form-inbox required")) ?>
</td>
</tr>
</table>
<table>
<tr>
<td> </td>
<td>
<?php echo $this->Form->submit('Submit', array('maxlength' => '50', 'size' => '30', 'label' => '', 'div' => false)) ?>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<?php echo $this->Form->end(); ?>
</td>
</tr>
</table>
I have used Configure::write('Routing.prefixes', 'admin'); to automatically resolve url like localhost://project_name/admin/pages/edit to admin_edit action of pages controller.
Edit: When I print_r($this->request->data); in if block then the name field is still containing the old value not the new one I entered. Why is that?
This line:-
if($this->Page->save($this->data)){
Should be:-
if($this->Page->save($this->request->data)){
However, the first line of your method is overwriting $this->request->data which should contain your form data!
I'm trying to implement CGridView in my website which only uses Yii framework without creating Yii application.
So here is the content of index.php:
require_once(dirname(__FILE__).'/../framework/yii.php');
$dbConf = array(
'components'=>array(
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=yii_tour',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
),
)
);
Yii::createWebApplication($dbConf);
Yii::import('zii.widgets.grid.*');
$message = new Message();
$dataProvider = new CActiveDataProvider($message);
$grid = new CGridView();
$grid->dataProvider = $dataProvider;
$grid->run();
This code works without any errors. The only problem is that it only outputs "Total 10 result(s)." and that's it. I can't see the grid.
I checked in html and this is what I got:
<div>
<div class="summary">Total 10 result(s).</div>
<table class="items">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr class="odd"></tr>
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
</tbody>
</table>
<div class="keys" style="display:none" title="/democms/grid.php"><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span></div>
</div>
I guess I'm missing something important here. Please help!
You have to init columns for grid by
$grid = new CGridView();
$grid->dataProvider = $dataProvider;
$grid->init();
$grid->run();