pagination not working when clicked next button - cakephp

Controler
public function search() {
$this->Paginator->settings = $this->paginate;
$this->loadmodel('Usermgmt.User');
if ($this->request -> isPost()) {
$this->User->set($this->data);
$keyword=$this->data['Doctors']['search'];
//$this->loadmodel('Usermgmt.User');
$cond=array('OR'=>array("User.username LIKE '%$keyword%'","User.email LIKE '%$keyword%'", "User.first_name LIKE '%$keyword%'", "User.last_name LIKE '%$keyword%'"));
//$result = $this->paginate('User',array('conditions'=>$cond));
$result = $this->paginate('User',array($cond));
$this->set('result', $result);
}
}
View
<?php
if (!empty($result)) { $sl=0;
foreach ($result as $row1) {
//print_r($row1);
$sl++; ?><div style="width:100%;display:inline-block;">
<div style="float:left">
<?php
//echo $row1['id'];
echo $this->Html->link($this->Html->image('../files/user/photo/'.$row1 ['User']['photo_dir'].'/'.$row1 ['User']['photo'], array('width' => '180', 'height' => '180')),
array('controller'=>'Profiles','action'=>'index',$row1['User']['id']),
array('escape' => false));
?>
</div>
<div>
<?php echo h($row1['User']['first_name'])." ".h($row1['User']['last_name'])."</br>";
echo h($row1['User']['username'])."</br>";
echo h($row1['User']['email'])."</br>";
echo h($row1['User']['mobile'])."</br>";
echo h($row1['UserGroup']['name'])."</br>";
?></div>
<div style="clear:both;"></div>
</div>
<?php }?>
<?php echo $this->Paginator->prev('previous'); ?>
<?php echo $this->Paginator->numbers(); ?>
<?php echo $this->Paginator->next('Next'); ?>
<?php }?>
here am search the user name or user details like fname, email like and display in view page
here i get output with pagination like 1 2 3 4 only first page displays when i click next page that shows empty pages may be $result getting unset how to solve this ??

The variable result is only sometimes set
if ($this->request->isPost()) {
...
$result = $this->paginate('User',array($cond));
$this->set('result', $result);
}
The variable result is only set for POST requests - clicking a link is not a post request, therefore the result variable is undefined.
Ensure you are paginating a GET request
There are several solutions, but the simplest solution to "How to paginate post data" is to not do so. Change your search form to use GET, and ensure the get parameters persist when paginating a request.
At the very least the controller code needs to call paginate and set for the variables in the view to exist irrespective of how the controller action was reached.

Related

How to get HashTag in Cakephp Form Submit?

I am working in cakephp 2.x
my current url is http://sitename.com/users/edit_profile#step-2 when i submit page i am not getting #setp-2
Code:
//Setp-2
<?php echo $this->form->create('User',array('action'=>'edit_profile')); ?>
<label>Smoke</label>
<?php $smoke_option = array('Heavy'=>'Heavy','Moderate'=>'Moderate','Light'=>'Light','Occasional'=>'Occasional','Non Smoker'=>'Non Smoker','Herb'=>'Herb');
echo $this->form->input('smoke', array('options' => $smoke_option,'default'=>$getProfile['User_detail']['smoke'],'label'=>false,'div'=>false,'empty'=>'No Answer'));?>
</div>
<?php echo $this->form->end();?>
UsersController.php
public function edit_profile(){
$this->layout='default';
if($this->request->is('post')){
$data = $this->request->data;
$data['User']['id']=$this->Session->read('Auth.User.id');
$updateProfile=$this->User->add_details($data);
if($updateProfile){
$this->Session->setFlash(__('Your Profile Updated Successfully'), 'success_message');
$this->redirect($this->referer());
}
}
}
How to getting hashtag in cakephp form submit?
Use
array('action'=>'edit_profile', '#' => 'step-2')
It appends #step-2 as you need in the url, similarly you can use your for ? also
The hash is never sent to the server.
ref - http://stackoverflow.com/questions/940905/can-php-read-the-hash-portion-of-the-url
though you can set a hidden field and set the value = .
you can access the hash by document.location.hash

Show calendar to specific users based on custom field

I use the calendar plugin from http://tri.be/ on a worpress website for a private website (it’s like an intranet..).
On this calendar I put all the planned meetings, but I would like that the current logged in user can see only the events he/she is supposed to join.
I thought that I can create a custom field, named, for example, “mustjoin” and put there a list of registered usernames as values.
Then do a check on the page.
If the current logged in username is in that list, he/she can see the event on the calendar.. otherwise not.
Something like :
if ( $current_user->user_login == get_field(mustjoin)) { ?>
//CODE IF OK
<?php } else { ?>
//CODE ELSE<?php } ?>
This of course is working when I have only one username in the ACF box, as soon as I put more than one username.. it doesn't work anymore because it is not an Array..
How can I create an array with ACF? what function and how can I interrogate that array?
This is the part of the code I need to show IF the current user username is in the value list ‘mustjoin’
<?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
<?php tribe_get_template_part('month/single', 'event') ?>
<?php endwhile; ?>
On another forum a guy told me to use this snippet :
function searchForName($name, $array) {
foreach ($array as $key => $val) {
if ($val['nickname'] === $name) {
return true;
}
}
return null;
}
$current_user = wp_get_current_user();
if(searchForName($current_user->user_login,get_field('mustjoin'))):
//CODE GOES HERE
endif;
But it doesn't work..
Thank you for your comment. This is the code, and it works at 99%.
<?php $current_user = wp_get_current_user(); ?>
<?php global $user_login;
get_currentuserinfo(); ?>
<?php $lookforuser = get_field('mustjoin') ?>
<?php if (strpos($lookforuser, $user_login) !== false) { ?>
<?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
<?php tribe_get_template_part('month/single', 'event') ?>
<?php endwhile; ?>
<?php } ?>
But I need to ask you if this code is "clean" or if I can make it better/faster.
Because when I open the calendar page, I can't see anything.. as soon as I refresh the page, all the events (where I am supposed to join) appear.
How can I fix it? (I tried with different computers/browsers, no changes..)
Thank you.
Thanks to another guy on another website, finally it works at 100%!
I just needed to check the user before the events loop :
<?php $current_user = wp_get_current_user(); ?>
<?php global $user_login;
get_currentuserinfo(); ?>
<?php while ($day['events']->have_posts()) : $day['events']->the_post(); ?>
<?php $lookforuser = get_field('mustjoin'); ?>
<?php if (strpos($lookforuser, $user_login) !== false)
tribe_get_template_part('month/single', 'event'); ?>
<?php endwhile; ?>
Thank you anyway for the right suggestion and help.

Redirect causing blank screen error cakephp

$this->redirect called from any controller method seems to result in a blank screen error, despite turning debug to 2 in my core.php.
In my AdminAppController, located in App/Plugin/Admin/Controller, was this piece of code originally, in my beforefilter():
public function beforeFilter() {
parent::beforeFilter ();
$rusername = $this->Auth->user ( "Username" );
if ($rusername != "admin") {
$this->redirect ( "/" );
return;
}
This results was an admin wide blank screen error, regardless of username. By commenting out the redirect, it allowed me to enter the admin area again.
However, now whenever I use $this->redirect I get blank screen errors.
For example here is my Prices view through my Product controller:
<?php echo $this->Session->flash('flash');?>
<?php echo $this->Form->create('Product', array('action' => 'change')); ?>
<fieldset>
<h3>Products</h3>
<table>
<?php
foreach($products as $k=>$v){
echo $this->Form->hidden("Product.{$k}.id", array('value'=> $v["Product"]['id']));
echo $this->Form->input("Product.{$k}.name", array('value' => $v["Product"]["name"], 'disabled' =>'disabled'));
echo $this->Form->hidden("Product.{$k}.slug", array('value'=>$v["Product"]['slug']));
echo $this->Form->hidden("Product.{$k}.description", array('value'=>$v["Product"]['description']));
echo $this->Form->hidden("Product.{$k}.cateID", array('value'=>$v["Product"]['cateID']));
echo $this->Form->input("Product.{$k}.price", array('value' => $v["Product"]['price']));
echo $this->Form->hidden("Product.{$k}.photo", array('value'=>$v["Product"]['photo']));
echo $this->Form->hidden("Product.{$k}.photo_dir", array('value'=>$v["Product"]['photo_dir']));
echo $this->Form->hidden("Product.{$k}.active", array('value'=>$v["Product"]['active']));
echo $this->Form->hidden("Product.{$k}.views", array('value'=>$v["Product"]['views']));
echo $this->Form->hidden("Product.{$k}.created", array('value'=>$v["Product"]['created']));
echo $this->Form->hidden("Product.{$k}.modified", array('value'=>$v["Product"]['modified']));
}
?>
</table>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
My Products controller contains this method:
public function prices(){
$products = $this->Product->find ("all");
$this->set ("products", $products );
}
And this method is called from this view:
public function change(){
//create() resets model, precaution
$this->Product->create();
$data = $this->request->data;
$this->Product->saveMany($data['Product']);
$this->Session->setFlash( "Prices Saved.");
$this->redirect ( '/' );
return;
}
On submitting a change of price (which does save and alter the database, the redirect line causes the screen to go blank, with no errors, or any other indication of what has gone wrong.
Error logs show no sign of what is happening.
Thanks.
Apologies for the late Answer.
After much struggle it turned out to simple.
AdminAppController included some whitespace before an opening

Calling a element after login

I have a post index view calling a element which content input field for comment.
I call it in this way
<?php echo $this->element('addcomment', array('post_id' => $post['Post']['id'])); ?>
This work fine, I pass the post id parameter, in the addcomment element, because the post_id input field is hidden in the addcomment. and of course I dont want that the user type the post id.
I have set up the authorization mechanism in order to allow adding comment to the user identified (connected).
When a non-connected user try to add a comment, he receives the login screen.
After login, he is redirected to the add commment form. The problem is that in the mean time it loose the value of the post_id variable.
Rem: If the user is connected before adding comments to the post, it works.
Dont hesitate to contact me in case my explanation is not clear or if you need more information.
This is my addcomment element
<div class="Addcomment form">
<?php
echo $this->Form->create('Comment', array(
'url' => array('controller' => 'comments', 'action' => 'add')
)); ?>
<fieldset>
<legend><?php echo __('Add Comment'); ?></legend>
<?php if (isset($current_user['id']) && isset($post_id)): ?>
<?php $this->request->data['Comment']['user_id'] = $current_user['id']; ?>
<?php $this->request->data['Comment']['post_id'] = $post_id; ?>
<?php echo $this->Form->input('post_id', array('type' => 'hidden')); ?>
<?php echo $this->Form->input('user_id', array('type' => 'hidden')); ?>
<?php else: echo $this->Form->input('post_id'); ?>
<?php endif; ?>
<?php echo $this->Form->input('content', array('class' => 'comment')); ?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
As per i understand your problem. You can use a session variable to store a post id which should not affected if user logs in. And using this session you can redirect a user to particular post after login.
Set a session value before redirecting user to login.
$this->Session->write('last_post_id',YOUR_POST_ID);
Redirect user to the post if user successfully logged in and if you find a session value not empty.
if ($this->Session->check('last_post_id') && $this->Session->read('last_post_id') != '') {
$this->redirect(YOUR_URL_TO_POST . '?postid=' . $this->Session->read('last_post_id'));
exit;
}else{
//NORMAL REDIRECTION
}
Hope this will help you. Unset session last_post_id after redirection and if no longer required.

cakephp: how to display associated records of associated records

My objective is to display records for a related child model (once removed) in the view. I understand that setting the recursive value to '1' returns the current model, its owner(s), plus their associated models.
What I don't know how to do is display this in the view.
My approach has been to create a varialable in the controller holding the value of the associated model ID, but that hasn't work.
*I should also mention that I'm using a sluggable behavior for tournaments, so it's not $id that is used but $slug.
Here are the model view controller details:
Model
tournament - has many tournamentDetails
tournamentDetails - has many updates
updates - belongs to tournamentDetails
tournaments controller
action = view
class TournamentsController extends AppController
function view($slug = null) {
if (!$slug) {
$this->Session->setFlash(__('Invalid Tournament.', true));
$this->redirect(array('action'=>'index'));
}
$this->Tournament->recursive = 1;
$tournament = $this->Tournament->findBySlug($slug);
$updates = $this->Tournament->TournamentDetail->Update->find('all', array('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )) );
$this->set(compact('tournament','updates' ));
Tournament view. This is display the 'tournament details' and (ideally) related tournament detail 'updates'
<h2><?php echo $tournament['Tournament']['name']; ?></h2>
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<h1><?php echo $tournamentDetail ['title']; ?></h1>
<p><?php echo $tournamentDetail ['body']; ?></p>
<?php endforeach; ?>
<?php foreach ($updates['Update'] as $update): ?>
<h4>Update: <?php echo $update ['date']; ?></h4>
<p> <?php echo $update ['Update'] ['title']; ?></p>
<p><?php echo $update ['Update'] ['body']; ?></p>
<?php endforeach; ?>
when I debug $updates, this is all I see:
Array
(
)
I don't know what I'm doing wrong here.
Is it not correct to construct the find conditions by using:
('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )
Thanks for the help.
Paul
You can use the Containable Behavior to specify in detail what fields you want to obtain from associated models, associated models to associated models and so on. Then you will find all data inside the array $tournament, like in the examples in the link.
EDIT (in response the OP comment). I think that the problem may be using the magic findBy method. You would need to substitute that with the standard find, like in this example
$tournament = $this->Tournament->find('first', array(
'conditions' => array(
'slug' => $slug
),
'contain' => array(//Put here the settings for Containable
)
));

Resources