I am using cakephp 2.6.7. I want to pass param in url. My expected url is: http://demo.jegeachi.com/tolets/search?page=2
but url is look like:
http://demo.jegeachi.com/tolets/search/%2526page%253D2
My code is:
if ($total_page > 2):
$current_page = 0;
if(isset($this->params['url']['page'])){
$current_page = $this->params['url']['page'];
}
?>
<?php if($current_page>1){
$url = 'page='.--$current_page;
?>
<li> «</li>
<?php }?>
<?php for ($page = 1; $page <= $total_page; $page++):
?>
<?php if ($page == $current_page) { ?>
<li><span><?php echo $page; ?> </span></li>
<?php } else {
$url = '&page='.$page;
?>
<li><?php echo $page; ?></li>
<?php } ?>
<?php endfor;
?>
<?php if($current_page<$total_page){
$url = 'page='.++$current_page;
?>
<li>»</li>
<?php } ?>
<?php endif;
?>
I also tried to use urlencode but no luck.
also you can use
<?php
echo $this->Html->link('Title', array(
'controller' => 'tolets',
'action' => 'search','?page=2')
);
?>
If this is a view code
<?php
echo $this->Html->link('Title', array(
'controller' => 'tolets',
'action' => 'search',
'?' => array('page' => 2))
);
?>
It will output
Title
Related
How to fix this issue
Error: userHelper could not be found.
this is my search.ctp inside element which is called in default.ctp
<?php echo $this->Form->create(null, ['url' => ['controller' => 'Users', 'action' => 'search']], array('type' => 'get')); ?>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->button('Search', ['type' => 'submit']); ?>
Below is my search controller
public function search() {
$value = $this->request->getData('username');
$results = $this->Users->find('all', ['fields'=>[
'Users.username',
'Users.email',
'Users.id',
'Users.age',
'Users.address',
'Users.gender'
],
'order' => 'Users.id ASC',
'conditions' => array(' username LIKE' => "%".$value."%")
]);
$this->set('user', $results);
$this->set('_serialize', ['user']);
}
search.ctp inside users
<?php
use Cake\ORM\TableRegistry;
use Cake\Filesystem\Folder;
use App\Controller\AppController;
?>
<?php foreach ($user as $users): ?>
<?php echo $this->users->username;?>
<?php endforeach;?>
What is the line inside loop? It shouldn't be.
$this->users->username;
I'm not so sure returning as a array or object in cakephp 3.
But, I'm sure that it should be like that,
$users->username;
or
$users['username'];
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();
}
}
I'm trying to paginate Workers which belong(s)To Job within a JobsController.
class JobsController extends AppController {
var $name = 'Jobs';
var $helpers = array('Html', 'Form', 'Js');
var $paginate = array(
'Worker' => array(
'limit' => 5,
'recursive' => 0,
'model' => 'Worker',
'order' => array('age' => 'ASC')
),
);
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Job.'));
$this->redirect(array('action'=>'index'));
return;
}
$this->Job->id = $id;
$workers = $this->paginate('Worker', array('Worker.job_id' => $id));
if ($workers) {
$this->set('workers', $workers);
}
}
In view.ctp:
<?php
$this->Html->script(array('jquery.min'), array('inline' => false));
$this->Paginator->options(array(
'update' => '#content',
'evalScripts' => true,
));
?>
<?php if (isset($workers)): ?>
<?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>
<table>
<tr>
<th>Age</th>
<th>Info</th>
</tr>
<?php foreach ($workers as $worker): ?>
<tr>
<td>
<?php echo $worker['Worker']['age']; ?>
</td>
<td>
<?php echo $worker['Worker']['info']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->Paginator->numbers(array('model' => 'Worker')); ?>
<p>
<?php
echo $this->Paginator->counter(array(
'model' => 'Worker',
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total.')
));
?></p>
<?php endif; ?>
<?php echo $this->Js->writeBuffer(); ?>
I'm getting the correct list of workers. But the links generated by numbers are not working. They look like /view/2/page:2/sort:Worker.age/direction:ASC
What am I doing wrong? cakephp version is 2.4.1.
Try this in your view action, by setting the order at run time.
$this->paginate['Worker']['order'] = array('Worker.age' => 'ASC')
Now your function look like this
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Job.'));
$this->redirect(array('action'=>'index'));
return;
}
$this->Job->id = $id;
$this->paginate['Worker']['order'] = array('Worker.age' => 'ASC');
$workers = $this->paginate('Worker', array('Worker.job_id' => $id));
if ($workers) {
$this->set('workers', $workers);
}
}
Hope this helps you.
I have 2 views, postview.ctp and usercomment.ctp, calling the same comment.ctp element. This element shows image using UploadPack helper. But image on usercomment.ctp doesn't show and has this error message
Notice (8): Undefined index: User [APP\Plugin\upload_pack\Model\Behavior\UploadBehavior.php, line 222]
line 222: $settings = self::$__settings[$modelName][$field];
The self::$__settings in usercomment.ctp is empty , but in postview.ctp it's not empty and the image showed up correctly.
comment.ctp:
<?php echo $this->Html->link($this->upload->image(
$comment['User'],
'User.avatar',
array('style' => 'thumb'),
array('class' => array('img-responsive', 'img-rounded'))
),
array('controller' => 'users',
'action' => 'view',
$comment['User']['id']),
array('escape' => false)
) ?>
And this is code to call comment.ctp on from the both view.
<?php if (!empty($comments)): ?>
<?php foreach ($comments as $comment): ?>
<?php echo $this->element('comment',array('comment' => $comment));?>
<?php endforeach; ?>
<?php endif; ?>
I've checked the $comment array and they're identical. How to fix it?
Load User model on usercomment action in Comment controller.
public function usercomments($id) {
$this->loadModel('User');
if($this->Auth->loggedIn()){
.....
when user enter the full url..i want to save only youtube id... pregmatch examine and extract video id and then it will be saved into database..the problem is how to make this pregmatch check and extract youtube id before save the full url
thanks for helping
// this is add() function in videos_controller
function add() {
if (!empty($this->data)) {
$this->Video->create();
if ($this->Video->save($this->data)) {
$this->Session->setFlash(__('The Video has been saved', true));
$this->redirect(array('action' => 'admin_index'));
} else {
$this->Session->setFlash(__('The Video could not be saved. Please, try again.', true));
}
}
$vcats = $this->Video->Vcat->find('list');
$this->set(compact('vcats'));
}
// this is add.ctp file
<div class="videos form">
<?php // echo $this->Form->create('Image');?>
<?php echo $form->create('Video'); ?>
<fieldset>
<legend><?php __('Add Video'); ?></legend>
<?php
echo $this->Form->input('vcat_id');
echo $this->Form->input('title');
$url= $this->Form->input('link');
echo $url
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true)); ?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Videos', true), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Vcats', true), array('controller' => 'vcats', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Vcat', true), array('controller' => 'vcats', 'action' => 'add')); ?> </li>
</ul>
</div>
// we get the unique video id from the url by matching the pattern but where i put this code to match before save
preg_match("/v=([^&]+)/i", $url, $matches);
$id = $matches[1];
Here
function add() {
if (!empty($this->data)) {
$this->Video->create();
$url = $this->data['Video']['link'];
/*assuming you have a column `id` in your `videos` table
where you want to store the id,
replace this if you have different column for this*/
preg_match("/v=([^&]+)/i", $url, $matches);
$this->data['Video']['id'] = $matches[1];
//rest of the code
}
}
I guess a better place for it is in the Model's beforeSave or beforeValidate method:
class Video extends AppModel {
...
public function beforeSave() {
if (!empty($this->data[$this->alias]['link'])) {
if (preg_match("/v=([^&]+)/i", $this->data[$this->alias]['link'], $matches)) {
$this->data[$this->alias]['some_id_field'] = $matches[1];
}
}
return true;
}
...
}