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'];
Related
This is the assigning part of the smarty variable in controller file.
$objSmarty->assign( 'seleted_customer_subsidy_race_types', $this->getRequestData( array( 'customer_subsidy_race_types' ) ) );
We get this displayed:
Array
(
[1] => Array
(
[subsidy_race_type_id] => 1
)
[2] => Array
(
[subsidy_race_type_id] => 2
[subsidy_race_sub_type_id] => 2
)
)
How to access subsidy_race_type_id and subsidy_race_sub_type_id fields specifically?
Follow this
foreach($arr as $item) {
print_r($item['subsidy_race_type_id']);
print_r($item['subsidy_race_sub_type_id']);
}
When I fire this:
$this->model->find('all',array());
I get an array with the data of the model:
Array
(
[0] => Array
(
// some data
)
[1] => Array
(
// some data
)
[2] => Array
(
// some data
)
...
)
Now is it possible instead of 0,1,2 to get the id as key for each of the data?
For example:
Array
(
[365] => Array
(
[model] => Array
(
[id] => 365
// some data
)
)
[442] => Array
(
[model] => Array
(
[id] => 442
// some data
)
)
[1000] => Array
(
[model] => Array
(
[id] => 1000
// some data
)
)
...
)
I know that find('list') can do something like that but only for 2 fields max (one key and one value).
you can build it maybe this code can do it :
$final_array = array();
foreach($returned_array as $k => $v){
$final_array[$v['id']] = $v;
}
print_r($final_array);
It's still an after-find operation but take a look at Set::combine (http://book.cakephp.org/2.0/en/core-utility-libraries/set.html) as a more elegant solution. Something like:
$newArray = Set::combine($yourArray, '{n}.Model.id');
/* $newArray now looks like:
Array
(
[365] =>
[442] =>
[1000] =>
)
*/
(more or less just the the docs example) might work in your case.
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; ?>
I got a query result like this : -
Array(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Japan
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => Nepal
)
)
}
How can I convert it to
Array
(
[0] => Japan
[1] => Nepal
)
The query used to get first array is 'select id , name from country;'
Please help me.
foreach ($element as $array) {
$element = $element[0]["name"];
}
Or,
function extractName($element) {
return $element[0]["name"];
}
$newArray = array_map("extractName", $array);
Extract!
$newArray = Set::extract($oldArray, '{n}.0.name');
More here.
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'];?>