cakephp Cannot use string offset as an array - arrays

hi all in my view I'm getting this error when trying to print out information in my array.
here is my function in the controller
public function view($name)
$fields = $this->Template->Field->find('list',array(
'fields'=> array('name'),
'conditions' => array(
'template_id'=> $name)));
$this->set('field', $fields);
here is the view
</br><h2>Here is your template fields</h2></br>
<?php if(!empty($field))
{
foreach($field as $name)
{?>
<tr>
<td>
<?php echo $name['Field']['name']; ?>
</tr></br>
<?php
}
}
else
{?>
<tr> <td>No Templates Found.</td></tr>
<?php
}?>

As I already answer some of your questions. So what I understood about your project is "Template hasMany Fields" and you already defined an association-ship in the corresponding models.
You should use the following code into your view:
<?php if(!empty($field))
{
foreach($field as $name)
{?>
<tr>
<td>
<?php echo $name; ?>
</td>
</tr>
<?php
}
}
else
{?>
<tr> <td>No Template Fields Found.</td></tr>
<?php
}?>

you have operation with type list so the result will be an array in [field.id] => [field.name]
then you will require following in view
<?php foreach($field as $name): ?>
<?= $name ?>
<?php endforeach ?>

Related

cakephp, link to retrieve status variable

Total n00b in CakePhp
Im trying to link to the view retrieved data from variable $status, defined in the controller, given the simple condition "done" or "pending".
Given the model:
<?php
class Task extends AppModel {
var $name = 'Task';
var $validate = array(
'title' => array(
'rule' => 'notEmpty',
'message' => 'Title of a task cannot be empty'
)
);
}?>
The Task Controller:
function index($status=null) {
if($status == 'done')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '1')));
else if($status == 'pending')
$tasks = $this->Task->find('all', array('conditions' => array('Task.done' => '0')));
else
$tasks = $this->set('Tasks', $this->Task->find('all'));
$this->set('tasks', $tasks);
$this->set('status', $status);
}
And finally The View:
<h2>Tasks</h2>
<?php if(empty($Tasks)): ?>
There are no tasks in this list
<?php else: ?>
<table>
<tr>
<th>Title</th>
<th>Status</th>
<th>Created</th>
<th>Modified</th>
<th>Actions</th>
</tr>
<?php foreach ($Tasks as $Task): ?>
<tr>
<td>
<?php echo $Task['Task']['title'] ?>
</td>
<td>
<?php
if($Task['Task']['done']) echo "Done";
else echo "Pending";
?>
</td>
<td>
<?php echo $Task['Task']['created'] ?>
</td>
<td>
<?php echo $Task['Task']['modified'] ?>
</td>
<td>
<?php echo $this->Html->link('Edit', array('action'=>'edit', $Task['Task']['id'])); ?>
<?php echo $this->Html->link('Delete', array('action'=>'delete', $Task['Task']['id']), null, 'Are you sure you want to delete this task?'); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index','done')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index', 'pending')); ?><br />
Clicking the "Done Task" returns an empty list (There are no tasks in this list). Can anyone see what is wrong on the link? Thanks in advance!
You are mixing up the casing in the variable name $Tasks. In your example you are assigning the array to $tasks, but then in your view, your are trying to check a variable names $Tasks, which will turn up empty because it has never been properly set.
Change this line:
$this->set('tasks', $tasks);
To:
$this->set('Tasks', $tasks);
As mentioned by McWayWeb above to case of the variable is incorrect.
As you are setting lower case vars to use in your view i would make the change there. Change all counts of Task in the view to task.
I would also consider setting all viewVars at the same time using the below
$this->set(compact('tasks','status));

Edit table-cells in a view

Yii Version 1.1.15
I have a model which contains a serialized array (table) in an attribut. Showing this array as a table in a view works fine.
But I want to update the table-cells and then again save the array serialized in the model. And this does not work. The output stops there (*) without errors.
Here you can see what I did:
$model->table contains a serialized array. The unserialized array looks like this:
array(
array('dog', 'cat', 'cow'),
array('meat', 'milk', 'gras'),
)
I unserialize the table in the controller:
controller: contactController.php
$model = Contact::model()->findByPk((int) $id);
$table = unserialize($model->input)
$this->render('contact_form', array(
'table' => $table));
}
And now I want to show and edit the array $table in a form as a html-table:
view: contact_form.php
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'contact-form',
));
?>
<table border="2">
<?php foreach ($table as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td>
<!-- (*) output stops here, without errors -->
<?= $form->textArea($value, '', array('rows' => 3, 'cols' => 20)); ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
<div class="row buttons">
<?php echo CHtml::submitButton("save"); ?>
</div>
<?php $this->endWidget(); ?>
The output stops before "... textArea ...".
If I only show $table in a view (without a form) it works like a charme:
<table border="2">
<?php foreach ($table as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?= CHtml::encode($value) ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
Hope this helps to help me :-) How can I get this nice idea working?
Actually you are missing the arguments of CactiveForm.textArea(.......)
http://www.yiiframework.com/doc/api/1.1/CActiveForm#textArea-detail
textArea($value, '', array('rows' => 3, 'cols' => 20)); ?>
Instead of it your code should be
textArea($model, $value, array('rows' => 3, 'cols' => 20)); ?>

cakephp, How to pass value to session when click to link?

There are four branches in my project, when i click branch1 the session should store branch1, the same way to branch 2,3...
<?php foreach($val as $value) {?>
<tr>
<th><span><?php echo $this->Html->link($value['Branch']['branch_name'], array('controller' => 'branchs', 'action' => 'index','onclick'=> 'update')) ?></span></th>
<?php CakeSession::write('sa',$value['Branch']['branch_name']); ?>
</tr>
<?php } ?>
<tr>
<?php foreach($val as $value) {?>
<tr>
<th><span>
<?php echo $this->Html->link($value['Branch']['branch_name'],array('controller' => 'branchs', 'action' => 'index','onclick'=> 'update')) ?></span>
</th>
<?php $this->Session->write('sa',$value['Branch']['branch_name']; ?>
</tr>
<?php } ?>
My demo code
$val=array('1','3','4');
foreach ($val as $key => $value) {
$this->Session->write('sa',$val);
}
$val1=$this->Session->read('sa');
foreach ($val1 as $key => $value) {
echo $value.'<br>';
}
My demo output

undefined variable in cakephp

So how do I fix this? It gives me:
Notice (8): Undefined variable: products
[APP/View/Categories/category_filter.ctp, line 10] Warning (2):
Invalid argument supplied for foreach()
[APP/View/Categories/category_filter.ctp, line 10]
<? if (!empty($categories)) { ?>
<? foreach($categories as $row): ?>
<h1>Category: <?= $row['Category']['category'] ?></h1>
<table>
<tr>
<th>Name</th><th>Description</th><th>Price</th><th>Category</th>
<th>Thumbnails</th>
</tr>
<? foreach($products as $row): ?>
<tr><<td>
<?=$row['Product']['name']?>
</td><td>
<?=$row['Product']['description']?>
</td><td>
<?=$row['Product']['price']?>
</td><td>
<?=$row['Product']['category_id']?>
</td><td>
<?
if(!empty($row['Picture'])){
?>
<?= $this->Html->image('/img/'. $row['Picture'][0]['filename'], array('width'=>'50', 'alt'=>$row['Picture'][0]['title'])); ?>
<p><?= $row['Picture'][0]['description']; ?></p>
<? } ?>
</td><tr>
<? endforeach; ?>
<? endforeach; ?>
</table>
<? } ?>
In the controller
function category_filter($category_id) {
$this->set('categories',$this->Category->findAllById($category_id));
}
You can simply do this by making a Category Model class.
//app/Model/Category.php
class Category extends AppModel
{
public $name = 'Category';
public $useTable = 'categories';
public $primaryKey = 'id';
public $hasMany = array('Products' => array('className' => 'Product',
'foreignKey' => 'category_id'
));
}
//app/Model/Product.php
class Product extends AppModel
{
public $name = 'Product';
public $useTable = 'products';
public $primaryKey = 'id';
}
In your controller:
function category_filter($category_id) {
$this->set('category_details',$this->Category->findById($category_id));
}
In your view:
<h1>Category: <?php echo $category_details['Category']['category'] ?></h1>
<table>
<tr>
<th>Name</th><th>Description</th><th>Price</th><th>Category</th>
<th>Thumbnails</th>
</tr>
<?php if(!empty($category_details['Products'])): foreach($category_details['Products'] as $row): ?>
<tr><td>
<?php echo $row['Product']['name']?>
<?php echo $this->Html->image('/img/'. $row['Picture'][0]['filename'], array('width'=>'50', 'alt'=>$row['Picture'][0]['title'])); ?>
<p><?php echo $row['Picture'][0]['description']; ?></p>
<?php } ?>
</td><tr>
<?php endforeach;endif; ?>
As I do not much aware about your Picture Model. Kindly do the association for that model also in your Product model.
<?php
function category_filter($category_id) {
$this->set('categories',$this->Category->findAllById($category_id));
$products=$this->Product->find('all');
$this->set(compact('products'));
}
?>
set your product variable

CakePHP 2 How to pass variables through the URL when using pagination?

I have a project in CakePHP application and I want to add pagination to it. I have added pagination to it, but when I click on a page number or go to the next page my application breaks.
The page relies on variables being passed to the controller in order to display the correct data. Here is my code for the view:
<table class="table table-striped">
<thead>
<tr>
<th>Job ID</th>
<th>Date</th>
<th>Site</th>
<th> </th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $jobsheet) { ?>
<tr>
<td>
<?php echo $jobsheet['Jobsheet']['jobnum']; ?>
</td>
<td>
<?php echo date("l, jS F Y", strtotime($jobsheet['Jobsheet']['jobdate'])); ?>
</td>
<td>
<span class="companyname"><strong><?php echo $jobsheet['Siteaddress']['sitename'] ?></strong></span><br />
<?php echo $jobsheet['Siteaddress']['addressline1']; ?>,
<?php echo $jobsheet['Siteaddress']['addressline2']; ?>,
<?php if ( $jobsheet['Siteaddress']['addressline3'] = '') { ?>
<?php echo $jobsheet['Siteaddress']['addressline3']; ?>,
<?php } else { ?>
<?php } ?>
<?php echo $jobsheet['Siteaddress']['city']; ?>,
<?php echo $jobsheet['Siteaddress']['county']; ?>,
<?php echo $jobsheet['Siteaddress']['country']; ?>,
<?php echo $jobsheet['Siteaddress']['postcode']; ?>
</td>
<td><a data-toggle="modal" href="#viewjobsheet<?php echo $jobsheet['Jobsheet']['id']; ?>" onClick="document.getElementById('jbif<?php echo $jobsheet['Jobsheet']['id']; ?>').src='/modal/customers/<?php echo $company['Company']['id']; ?>/jobs/<?php echo $jobsheet['Jobsheet']['id']; ?>/view/';" title="View" class="icon-eye-open"></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="controls">
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<br />
<br />
<?php echo $this->Paginator->counter(array(
'format' => 'Page {:page} of {:pages}, showing {:current} records out of
{:count} total, starting on record {:start}, ending on {:end}'
));
?>
</div>
</div>
This is the controller:
function index(){
$companyid = $this->params['customer'];
$this->set('companyid', $companyid);
$siteid = $this->params['site'];
$this->loadModel('Jobsheet');
// Job Sheets
$jobsheets = $this->Jobsheet->find('all', array('conditions' => array('Jobsheet.company' => $companyid), 'recursive' => 2, 'order' => 'Jobsheet.jobdate DESC', 'limit' => '10'));
$this->set('jobsheets', $jobsheets);
$this->paginate = array(
'limit' => 10
);
$data = $this->paginate('Jobsheet');
$this->set(compact('data'));
}
While the pagination does work, it ends up loosing the variable and this breaks what I'm trying to do. What am i missing?
One method is to use PaginatorHelper::options (see the API)
For example, say we want to pass the variable $pass between Paginator page changes. So first pass the variable to the View.
public function index() {
$pass = 'testString';
$this->set(compact('pass'));
$this->set('posts', $this->paginate());
}
In the View, we can then add this to the parameters that Paginator passes when changing pages using Paginator::options and the url option.
<?php
$this->Paginator->options(array(
'url' => array(
'pass' => $pass
)
));
?>
So if you moved from page 1 to page 2, the resulting url would be something similar to:
http://localhost/cake/posts/index/pass:testString/page:2
This is also how Paginator passes sort orders and sort keys. So while "pass" in pass:testString can be anything you want, be sure not to use sort, direction, and page.
As long as you are changing pages through the Paginator links, the passed variables will remain. Note that you can access the values of the passed arguments in both the View and the Controller by the property:
$this->passedArgs

Resources