Error in fetching values from database in cakephp - cakephp

I am fetching value from table sessionapps in my cakephp application.
I have index.ctp file as follows
<table>
<tr><td>ID</td><td>Title</td><td>Post</td><td>Actions</td><td>Created On</td></tr>
<?php foreach ($sessionapps as $post): ?>
<tr><td><?php echo $post[Sessionapp][id];?></td>
<td><?php echo $data->Html->link($post[Sessionapp][title],array('controller'=>'sessionapp','action'=>'view',$post[Sessionapp][id]));?></td>
<td><?php echo $post[Sessionapp][post];?></td>
<td><?php echo $this->Html->link('Edit Post ', array('action' => 'editarticle', $post[Sessionapp][id]));?></td>
<td><?php echo $post[Sessionapp][created];?></td></tr>
<?php endforeach; ?>
</table>
And i have created its controller sessionapps_controller.php and added action index in it as follows
function index()
{
$this->set('sessionapps', $this->Sessionapp->find('all'));
}
But it is not working and giving error
Notice (8): Use of undefined constant Sessionapp - assumed 'Sessionapp' [APP/views/sessionapps/index.ctp, line 14]
Notice (8): Use of undefined constant id - assumed 'id' [APP/views/sessionapps/index.ctp, line 14]
My array $sessionapps as follows
Array ( [0] => Array ( [Sessionapp] => Array ( [id] => 1 [title] => The title [body] => This is the post body. [postby] => [created] => 2013-01-15 11:35:13 [modified] => ) ) [1] => Array ( [Sessionapp] => Array ( [id] => 2 [title] => A title once again [body] => And the post body follows. [postby] => [created] => 2013-01-15 11:35:13 [modified] => ) ) [2] => Array ( [Sessionapp] => Array ( [id] => 3 [title] => Title strikes back [body] => This is really exciting! Not. [postby] => [created] => 2013-01-15 11:35:13 [modified] => ) ) ) 1

I think it should be like this.
<?php foreach ($sessionapps as $post): ?>
<tr><td><?php echo $post['Sessionapp']['id'];?></td>
<td><?php echo $data->Html->link($post['Sessionapp']['title'],array('controller'=>'sessionapp','action'=>'view',$post['Sessionapp']['id']));?></td>
<td><?php echo $post['Sessionapp']['post'];?></td>
<td><?php echo $this->Html->link('Edit Post ', array('action' => 'editarticle', $post['Sessionapp']['id']));?></td>
<td><?php echo $post['Sessionapp']['created'];?></td></tr>
<?php endforeach; ?>

Related

php code to upload multiple images cakephp

i am new in cakephp and try to learn.
my table structure is
table name - drivers
fields - id,name,
table - drivers_uploads
fields - id,driver_id,documentname,documentpath
i try to upload multiple files in my table.
My code is working well in folder it uploads multiple images but in table i want to insert all rows
i want to insert multiple file for same id and the id goes in drivers tables and the remaining files insert multiple rows for same id in drivers_upload table.
thanks in advance.
for ex
I need data to be inserted in below tables format.
**drivers tables **
id , name
16 lucky
driver_uploads tables
id,driver_id, document_name
1 16 mo.docx
2 16 ro.docx
controller code
public function add() {
$this->Driver->create();
if ($this->request->is('post')) {
for($i=1;$i<4;$i++)
{
if(empty($this->data['Driver']['document'.$i]['name'])){
unset($this->request->data['Driver']['document'.$i]);
}
if(!empty($this->data['Driver']['document'.$i]['name']))
{
$file=$this->data['Driver']['document'.$i];
$ary_ext=array('jpg','jpeg','gif','png','xlsx','docx'); //array of allowed extensions
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
if(in_array($ext, $ary_ext))
{
move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. time().$file['name']);
//move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/posts/' . mktime().$file['name']);
$this->request->data['Driver']['document'.$i] = time().$file['name'];
}
}
}
if ($this->Driver->save($this->request->data))
{
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
else
{
$this->Session->setFlash('Unable to add your post.');
}
}
}
view code
<h1>Add Post</h1><?php
echo $this->Form->create('Post', array('url' => array('action' => 'add'), 'enctype' => 'multipart/form-data'));
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
for($i=1; $i<4; $i++)
{
?>
<div id="attachment<?php echo $i;?>" <?php if($i !=1) echo "style='display:none;'";?> >
<div>
<?php echo $this->Form->input('image'.$i,array('type'=>'file','label' => false,'div' => false));?>
</div>
<div id="attachmentlink<?php echo $i;?>" <?php if($i==3) echo "style='display:none;'";?>>Add Another Attachment</div>
</div>
<?php } ?>
<?php
echo $this->Form->end('Save Post');
?>
<script>
function show(target){
document.getElementById(target).style.display = 'block';
}
function hide(target){
document.getElementById(target).style.display = 'none';
}
</script>
Below is my print_r data
echo "<pre>";print_r($this->request->data); exit();
if ($this->Driver->save($this->request->data))
Array
(
[Driver] => Array
(
[name] => raj
[mobile] => 9960181380
[email] => raj#gmail.com
[licenceno] => 512203615803
[address] => 452,nagpur
[document1] => Array
(
[name] => findmelogistics_Phase_III_Project Plan v1.2_8.xls
[type] => application/vnd.ms-excel
[tmp_name] => /tmp/phpXGkLvj
[error] => 0
[size] => 29184
)
[document2] => 1461815092FML Phase 3 Specification 2.docx
[document3] => 1461815092Phase 3 - SmartData questions 310316.docx
)
)

cakephp pagination controller

what shoud i write in the controller to have pagiation
this is my index
<?php foreach ($bien['Servicebien'] as $servicebien): ?>
<tr> <td><?php echo $servicebien['dateServiceBienDu']; ?>
<td><?php echo $servicebien['dateServiceBien']; ?></td>
<td><?php echo $servicebien['montantServiceBien']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($servicebien); ?>
</table>
<div>
<?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}'))); ?>
Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers();
echo $this->Paginator->next(' Next >> ', null, null, array('class' => 'disabled'));
?>
$data = $this->Paginator->paginate('model_name');
$this->set('data', $data);
Try this
class FeedsController extends AppController {
public $components = array( 'Search.Prg');
public function beforeFilter() {
parent::beforeFilter();
}
function indes(){
$this->Prg->commonProcess();
$this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs;
$parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs);
$this->paginate = array(
'conditions' => array(),
'limit' => 10,
'fields' => array(),
'order' => 'id DESC'
);
$result = $this->paginate();
}
}

Print data of one column differently

Controller Code:
$this->loadModel('Social');
$this->set('social', $this->Social->find('all',array('fields'=>array('Social.id','Social.semfieldvalue'))));
View Code:
<?php foreach ($social as $socials): ?>
<td><?php echo $socials['Social']['semfieldvalue']; ?></td>
<?php endforeach; ?>
Now I get all field value of semi-field column.
But now in my view file I want to print. 1st row value at different place and 2nd row value at diff place.. so how to do this in cakephp.
my out put when i <?print_r($social); ?>
Array ( [0] => Array ( [Social] => Array ( [id] => 1 [semfieldvalue] => http://www.facebook.com/new ) ) [1] => Array ( [Social] => Array ( [id] => 2 [semfieldvalue] => http://www.twitter.com/new ) ) [2] => Array ( [Social] => Array ( [id] => 3 [semfieldvalue] => http://www.linkedin.com/new ) ) [3] => Array ( [Social] => Array ( [id] => 4 [semfieldvalue] => http://www.google.com/new ) ) ) http://www.facebook.com/new
so i got all value but i want to print id 1 at diff place and id 2 at diff place in same view file
to print 1st value in Array : Use
echo $social[0]['Social']['semfieldvalue'];
to print 2nd val
echo $social[1]['Social']['semfieldvalue'];

How cakePHP build form data

i've created a form that loop in my table and give me some images:
id1: ->Image path, title etc
id2: -> Image path title etc
i've created my view in this way:
<div>
<?php echo $this->Form->create('Gallery',
array( 'action' => 'save')); ?>
<table cellpadding="0" cellspacing="0">
<?php foreach($images as $image): ?>
<tr>
<td>
<?php echo $this->PhpThumb->thumbnail($image['medias']['file'], array('w' => 75, 'h' => 75)); ?>
</td>
<td><?php echo $this->Form->input('title'.$image['medias']['id'], array('value' => $image['medias']['title']));?></td>
<td><?php echo $this->Form->input('description'.$image['medias']['id'], array('value' => $image['medias']['description']));?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->Form->end(__('Submit')); ?>
After submit, i have an array like this:
array(
'Gallery' => array(
'title19' => 'test',
'description19' => '',
'title20' => '',
'description20' => '',
'title21' => '',
'description21' => '',
'title22' => '',
'description22' => ''
)
)
For sure, i cant use the save() method because the array isnt well formatted. I think the right format should be:
array(
'Gallery' => array(
array('id' => 19,
'title => 'test',
'description => ''),
array( 'id' =>20,
title => '',
description => '')
)
)
but, how can i get this ? Or some array that is right formatted?
When you iterate over them put the iterator or id in the right place:
$this->Form->input('Model.' . $i . 'field');
$this->Form->input('Model.' . $i . 'other_field');
References:
Saving your data
Form Helper

CakePHP find('neighbors') use [next] and [prev] from array

I am trying to get the neighbors Ids of the current Id.
What I have below works, but I cannot separate the [prev] from the [next] link.
The function in the controller looks as follows:
function prevNext() {
return $this->System->find('neighbors', array('fields' => 'name', 'value' => 3));
}
Which will output something like this:
Array
(
[prev] => Array
(
[System] => Array
(
[name] => Aegerter
[id] => 4004
)
)
[next] => Array
(
[System] => Array
(
[name] => Canonica
[id] => 4006
)
)
)
This gets passed as a requestAction -
<?php $systems = $this->requestAction('systems/prevNext'); ?>
<?php foreach($systems as $system): ?>
<?php echo $html->link($system['System']['id'],array('action'=>'view', $system['System']['id']));?>, <?php echo $system['System']['name'];?>
<?php endforeach; ?>
into the view as an element:
<?php echo $this->element('systems'); ?>
How do you call the [prev] and [next] link separately (not a foreach output)?
Tia
If i understand correctly:
<?php echo $html->link($systems['prev']['System']['id'],array('action'=>'view', $systems['prev']['System']['id']));?>, <?php echo $systems['next']['System']['name'];?>
<?php echo $html->link($systems['next']['System']['id'],array('action'=>'view', $systems['next']['System']['id']));?>, <?php echo $systems['next']['System']['name'];?>

Resources