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();
Related
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 have two seperate arrays I am using in my php page.
The first one holds all of the field names that I will be using to create my html table headers on the UI.
The array of data for this looks like so:
Array
(
[0] => Array
(
[fieldID] => 2
[fieldName] => Project Title
[fieldAlias] => initiativeTitle
)
[1] => Array
(
[fieldID] => 4
[fieldName] => Project Description (preview)
[fieldAlias] => initiativeDescriptionPreview
)
)
Next, I have a data set of all the records I need to print to the table. The key in this array matches the fieldAlais from the header array.
My goal here is to loop over the header array and get the fieldAlias, then loop over the data and when the fieldAlias from the header row matches a the key in the data row, it prints it out.
Here is how I populate the header array:
$primaryArray = Array();
if(isset($dashboardDetails->results->primary->fields)){
foreach($dashboardDetails->results->primary->fields as $p){
$primaryArray[] = array(
'fieldID' => (int)$p->fieldID,
'fieldName' => (string)$p->fieldName,
'fieldAlias' => (string)$p->alias
);
}
}
This is an example of the data object:
SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[initiativeDescriptionPreview] => This is a test description
[initiativeTitle] => Test
)
Here is the mess I am working with on the HTML table:
<table class="table table-hover table-striped">
<thead>
<tr>
<?php
// Loop over the primary fields
for ($i = 0; $i < count($primaryArray); ++$i) {
echo '<th class="small">'.$primaryArray[$i]['fieldName'].'</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
// For each field in our primary array
for ($i = 0; $i < count($primaryArray); ++$i) {
// Set our alais
$a = $primaryArray[$i]['fieldAlias'];
echo '<tr>';
// Loop over all of the records
foreach($dashboard->data as $value){
foreach($value as $key => $val){
if($key == $a){
echo '<td class="small">'.$val.'</td>';
}
}
}
echo '</tr>';
}
?>
</tbody>
</table>
The result of this is that its printing two rows of data when this should be the same row:
The short end of this is: I have two separate objects, headers and data. I need to print the table headers and then print the data from the other array to its corresponding header.
First of all it isn't clear whether or not the data array, is an array of arrays, which it should be.
You can then loop the data array, the value of which is the row you're working with. Then you can loop the headers array, and print out the element of the row array who's key matches the value of the 'fieldAlias' element of the headers array element that your currently in.
An example:
$headers = Array(
Array(
'fieldID' => 2,
'fieldName' => 'Project Title',
'fieldAlias' => 'initiativeTitle'
),
Array(
'fieldID' => 4,
'fieldName' => 'Project Description (preview)',
'fieldAlias' => 'initiativeDescriptionPreview'
)
);
$results = new StdClass();
$results->data = Array(Array('initiativeDescriptionPreview' => 'This is a test description', 'initiativeTitle' => 'Test'));
?>
<table cellspacing="10" cellpadding="10">
<tr>
<?php foreach($headers as $headerField): ?>
<th style="border:1px solid red">
<?php echo $headerField['fieldName']; ?>
</th>
<?php endforeach; ?>
</tr>
<?php foreach($results->data as $row): ?>
<tr>
<?php foreach($headers as $headerField): ?>
<td>
<?php echo $row[$headerField['fieldAlias']]; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Looks to me that you're creating a new <tr> element for each of your columns.
I think your HTML looks something like this:
<table class="table table-hover table-striped">
<thead>
<tr>
<th class="small">some name</th>
<th class="small">some other name</th>
</tr>
</thead>
<tbody>
<tr>
<td>value of column 1</td>
</tr>
<tr>
<td>value of column 2</td>
</tr>
</tbody>
</table>
Please notice that you probably would expect only one <tr> element in your <tbody>
I tried to create the arrays and objects, which you have described with direct PHP Code, so that I can test my approach. I think you don't need $primaryArray at all. Here is my complete solution:
<?php
$headers = array(
array("fieldID" => 2, "fieldName" => "Project Title", "fieldAlias" => "initiativeTitle"),
array("fieldID" => 4, "fieldName" => "Project Description (preview)", "fieldAlias" => "initiativeDescriptionPreview"),
);
class SimpleXMLElementObject {
public
$data;
}
$mySimpleXMLElementObject1 = new SimpleXMLElementObject;
$mySimpleXMLElementObject2 = new SimpleXMLElementObject;
$mySimpleXMLElementObject1->data = array(
"initiativeDescriptionPreview" => "This is a test description",
"initiativeTitle" => "Test"
);
$mySimpleXMLElementObject2->data = array(
"initiativeDescriptionPreview" => "This is a test description2",
"initiativeTitle" => "Test2"
);
$mySimpleXMLElementObjects = array();
$mySimpleXMLElementObjects[] = $mySimpleXMLElementObject1;
$mySimpleXMLElementObjects[] = $mySimpleXMLElementObject2;
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<table class="table table-hover table-striped">
<thead>
<?php foreach($headers as $header) { ?>
<th class="small"><?= $header["fieldName"] ?></th>
<?php } ?>
</thead>
<tbody>
<?php foreach($mySimpleXMLElementObjects as $anObject) { ?>
<tr>
<?php foreach($headers as $header) { ?>
<td class="small"><?= $anObject->data[$header["fieldAlias"]] ?></th>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
The resulting table is below:
<html>
<head>
<title>Test</title>
</head>
<body>
<table class="table table-hover table-striped">
<thead>
<th class="small">Project Title</th>
<th class="small">Project Description (preview)</th>
</thead>
<tbody>
<tr>
<td class="small">Test</th>
<td class="small">This is a test description</th>
</tr>
<tr>
<td class="small">Test2</th>
<td class="small">This is a test description2</th>
</tr>
</tbody>
</table>
</body>
</html>
I have a portion of my website where I can give awards to members. Right now I am working on creating individual description pages for each award. On that page I want to include which members have already received this award and display this data in an HTML table.
I have already passed the data into a Multidimensional array as seen below.
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$currentAward = $awardid; // Current Page Award. Local value to the page.
$result = $conn->query("SELECT * FROM vms_awardsgranted WHERE awardid='$currentAward'");
$awardsList = array();
while($pilotsList = $result->fetch_assoc()){
$awardsList[ $pilotsList["id"] ] = $pilotsList;
}
echo nl2br("DEBUG: $currentAward \n");
print_r(array_values($awardsList));
$conn->close();
?>
Example Result
DEBUG: 8
Array ( [0] => Array ( [id] => 28 [awardid] => 8 [pilotid] => 4 [dateissued] => 2015-10-14 20:12:21 ) [1] => Array ( [id] => 32 [awardid] => 8 [pilotid] => 1 [dateissued] => 2015-10-14 20:14:14 ) )
From here I am trying to parse this info and add it into the HTML table below but I honestly can't find the correct way of doing this with a Multidimensional array. Can anyone out here give me some insight? My HTML tables looks like below.
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
<thead>
<tr>
<th>Pilot ID</th>
<th>Pilot Name</th>
<th>Date Awarded</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">
</td>
<td align="center">
</td>
<td align="center">
</td>
</tr>
</tbody>
</table>
Basically, to loop through the result, your code could look like this:
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
<thead>
<tr>
<th>Pilot ID</th>
<th>Pilot Name</th>
<th>Date Awarded</th>
</tr>
</thead>
<tbody>
<?php foreach($awardsList as $member):?>
<tr>
<td align="center">
<?=$pilot['pilotid']?>
</td>
<td align="center">
</td>
<td align="center">
<?=$pilot['dateissued']?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
A couple of remarks, though.
You don't return the pilot name from the query. You should join on the table of members/pilots to get their name.
Your naming is confusing. $pilotList suggests a list of pilots, but instead that variable contains only the reference/junction between the award and one pilot. Dito for awardsList which actually contains a list of people that won the award.
Something like
<tbody>
<?php foreach($awardsList as $record){ ?>
<tr>
<td align="center">
<?php echo $record['id']; ?>
</td>
<td align="center">
<?php echo $record['awardid']; ?>
</td>
<td align="center">
<?php echo $record['pilotid']; ?>
</td>
</tr>
<?php } ?>
</tbody>
i need to create zend form like this HTML form.
<table cellspacing="2" class=text12 cellpadding="2" width="100%" border="0">
<tbody>
<tr>
<td width="20%">Reference to Appear <span class="required">*</span></td>
<td width="30%">
<input type="text" name="reference">
</td>
<td width="18%">Account Holder Name <span class="required">*</span></td>
<td width="32%">
<input type="text" name="reference">
</td>
</tr>
</table>
I am unable to create Zend Form like above HTML. I tried like this using Zend Form
public function testbenForm()
{
$this->setMethod('post');
$formelements=array();
$ef_reference = new Zend_Form_Element_Text('ef_reference');
$ef_reference->setAttrib('class', 'transaction_elements_input');
$ef_reference->addFilter('StringTrim')
->addFilter('StripTags')
->setLabel('Account Holder Name')
->clearDecorators();
array_push($formelements, $ef_reference);
$ef_account_holder_name = new Zend_Form_Element_Text('ef_account_holder_name');
$ef_account_holder_name->setAttrib('class', 'transaction_elements_input');
$ef_account_holder_name->addFilter('StringTrim')
->addFilter('StripTags')
->setLabel('Account Holder Name')
->clearDecorators();
array_push($formelements, $ef_account_holder_name);
$this->addElements($formelements);
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
$this->setFormDecorators(array(
'FormElements',
array(array('data'=>'HtmlTag'),array('tag'=>'table','class'=>'text12')),
'Form'
));
}
and in HTML form i displayed form like this
<table cellspacing="2" cellpadding="2" width="99%" border="0">
<tbody>
<tr>
<td>
<fieldset>
<legend>Beneficary Bank Details</legend>
<?php echo $this->form;?>
</fieldset>
</td>
</tr>
<tr>
<td class="td_height" colspan="2"> </td>
</tr></tbody>
</table>
Could you any one explain me how to create 2 columns in single row using zend form decorators.
I am very uncleared about Zend decorators every time while i read the Zend Form Decorators in the Zend Manual http://framework.zend.com/manual/1.12/en/zend.form.standardDecorators.html. Please any one clear the point of Zend Decorators how it will works and how to define our own decorators. Thanks in advance.
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 :)