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

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'];?>

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
)
)

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'];

Multiple edit in cakephp using mysql

Hii i am trying to multiple update in single table
Below i am describing controller, view coding
In controller
public function multiEdit($oid,$cid){
$this->set('oid',$oid);
$this->set('cid',$cid);
$this->set('oid',$oid);
$this->set('cid',$cid);
$item = $this->Item->find('all',array('conditions' => array('Item.order_id' => $oid,'Item.visible'=>true)));
if($this->request->is('post') || $this->request->is('put')){
if($this->Item->saveAll($this->data)){
$this->Session->setFlash('Item Information has been updated',true);
$this->redirect(array('action' =>'index',$oid,$cid));
}else{
$this->Session->setFlash('Unable to update Item information',true);
}
}
if(!$this->request->data){
$this->request->data = $item;
}
}
In view
<?php echo $this->Form->create('Item');?>
<?php foreach ($this->data as $i => $item): ?>
<?php
echo $this->Form->hidden("Item.$i.id", array('value' => $item['Item']['id']));
echo $this->Form->input("Item.$i.name", array('value' => $item['Item']['name']));
echo $this->Form->hidden("Item.$i.code", array('value' => $item['Item']['code']));
echo $this->Form->hidden("Item.$i.qnty", array('value' => $item['Item']['qnty']));
echo $this->Form->hidden("Item.$i.price", array('value' => $item['Item']['price']));
echo $this->Form->input("Item.$i.sent", array('value' => $item['Item']['sent']));
echo $this->Form->hidden("Item.$i.ac_sent", array('value' => $item['Item']['ac_sent']));
echo $this->Form->hidden("Item.$i.visible", array('value' => $item['Item']['visible']));?
<?php endforeach; echo $this->Form->end('Save Item') ?>

Error in fetching values from database in 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; ?>

Group records in CakePHP based on first letter?

I'm wanting to fetch and group records from a database table by the first letter. So for example:
A
Alfred
B
Brian
C
Christopher
...
Ideally, I'd like my data structure to come out as follows:
Array
(
[A] => Array
(
[0] => Array
(
[Person] => Array
(
[id] => 12
[name] => Alfred
)
)
)
)
I could do this with a raw MySQL query, but not sure if CakePHP supports this out of the box. I'm just literally wanting records grouped so I can iterate over them and create an unordered list for each letter:
<h2>A</h2>
<ul>
<li>Alfred</li>
</ul>
<h2>B</h2>
<ul>
<li>Brian</li>
</ul>
<h2>C</h2>
<ul>
<li>Christopher</li>
</ul>
...
What i would do is: order them by name and then sort them in a loop:
$people = $this->Person->find('all', array(
'order' => 'Person.name ASC'
));
And then you could do:
$letter = '';
foreach($people as $person) {
$firstLetter = strtolower(substr($person['Person']['name'], 0, 1));
if ($firstLetter !== $letter) {
$letter = $firstLetter;
}
$_people[$letter][] = $person;
}
$people = $_people;
Or just do it directly in the view so you don't need to loop it twice:
<ul>
<?php $letter = 'first'; ?>
<?php foreach ($people as $person) : ?>
<?php $firstLetter = strtolower(substr($person['Person']['name'], 0, 1)); ?>
<?php if ($firstLetter !== $letter) : ?>
<?php if ($letter !== 'first') : ?>
</ul></li>
<?php endif; ?>
<?php $letter = $firstLetter; ?>
<li><h2><?php echo $firstLetter; ?></h2><ul>
<?php endif; ?>
<li><?php echo $person['Person']['name']; ?></li>
<?php endforeach; ?>
</ul></li></ul>
I wouldn't know how to get this structure just by using a find call (except for making 26 calls or creating a db table containing all letters) and I don't see why that would be neccesairy.
Hope that helps! (ps code isn't tested... but you get the point. should there be any typos)
Try this:
$peoplebyletter = array();
foreach($people as $person){
$peoplebyletter[strtolower(substr($person['Person']['name'], 0, 1))][] = $person;
}
debug($peoplebyletter);
Not tested. Let me know if it works.

Resources