I have the typical address book in which I have in my main page a list of my contacts. When I click on the classic magnify glass icon I want to go to another page that will show full info of the contact. I have this code for showing the people on a table:
<table class="table table-hover">
<thead>
<tr>
<td>Name</td>
<td>Surname</td>
<td>Actions</td>
</tr>
</thead>
<?php foreach ($this->entries as $entry): ?>
<tbody>
<tr>
<td><?php echo $this->escape($entry->name) ?></td>
<td><?php echo $this->escape($entry->surname) ?></td>
<td><a href="<?php echo $this->url(
array(
'controller' => 'contact',
'action' => 'viewDetails'
),
'default',
true) ?>">
<span class="glyphicon glyphicon-search"></span></a>
</td>
</tr>
</tbody>
<?php endforeach ?>
</table>
As you see, in the array to call the viewDetails action I'd need to send the ID of the contact I want the full info, but I don't know how to do this. Anyone knows how to send this ID and receive it in the landing page or where to find more info about the fields I can send to this link?
Thank you very much in advance.
You can try something like this:
<a href="<?php echo $this->url(
array(
'controller' => 'contact',
'action' => 'viewDetails',
'id' => $entry->id
),
'default',
true) ?>">
// Output: "contact/viewDetails/id/id_value" with id_value = $entry->id
And in viewDetails action, you can retrieve the id like this:
$id = $this->getRequest()->getParam('id');
I hope it will help you :)
For further information, you can read :
http://framework.zend.com/manual/1.12/en/zend.view.helpers.html
http://framework.zend.com/manual/1.12/en/zend.controller.router.html
Related
this is my first time exploring Codeigniter 4 and I follow some tutorials to understand the basic crud process.
Below are my controller where the model query is used.
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourcePresenter;
class Project extends ResourcePresenter
{
protected $modelName = 'App\Models\ProjectModel';
public function index()
{
return view('projects/index', ['projects' => $this->model->orderBy('created_at', 'asc')->findAll()]);
}
}
Then below are the index page where I want to display the list and the part where I have a problem.
<?php foreach ($projects as $project) : ?>
<tr>
<td><?= $project->name ?></td>
<td><?= $project->description ?></td>
<td>
<form action="<?php echo base_url('/projects/delete/' . $project->id); ?>" method="post">
<a class="btn btn-outline-info" href="<?php echo base_url('/projects/show/' . $project->id); ?>">
Show
</a>
<a class="btn btn-outline-success" href="<?php echo base_url('/projects/edit/' . $project->id); ?>">
Edit
</a>
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
'name' and 'description' are the column names stored in the database.
Can anyone help me check whether the problem is because of code or another external factor since my friend who tried the same tutorial succeeded even though the code is the same?
Below are the errors that show up.
->findAll() returns an array of elements.
Instead of: ❌
<td><?= $project->name ?></td>
<td><?= $project->description ?></td>
Use this: ✅
<td><?= $project['name'] ?></td>
<td><?= $project['description'] ?></td>
Do the same for other sections. I.e :$project->id.
I have created my own dataSource that collects data using an API.
Now I am trying to use the paginate function to get the data. This works fine I am getting the right data however there is a problem!
I simply cannot get the paginator to identify pages. My goal is to make sure that there is only 50 records pr page however no matter what I do I only get ONE page and my links are not correct links they are basicly just text
Here is my view:
index.ctp
<?php
$this->Paginator->options(array(
'update' => '#updateTable',
'evalScripts' => true,
));
?>
<!-- BEGIN EXAMPLE TABLE PORTLET-->
<div id="updateTable">
<div class="portlet box green Report index">
<div class="portlet-title">
</tr>
</thead>
<tbody class="report_data">
<?php foreach ($table['Report']['data']['data'] as $res): ?>
<tr>
<td><?php echo h($res['Offer']['name']); ?> </td>
<td><?php echo h($res['Stat']['clicks']); ?> </td>
<td><?php echo h(round($res['Stat']['conversions'], 2)); ?> </td>
<td><?php echo "€ " . h(round($res['Stat']['payout'], 2)); ?> </td>
<td><?php echo h(round($res['Stat']['ltr'], 2)) . " %"; ?> </td>
<td><?php echo "€ " . h(round($res['Stat']['cpc'], 2)); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div>
<div class="paging">
<?php
echo $this->Paginator->numbers(array('first' => 'First page'));
echo $this->Paginator->prev('< Prev', null, null, array('class' => 'disable'));
echo $this->Paginator->next('Next >', null, null, array('class' => 'disable'));
?>
</div>
</div>
</div>
</div>
</div>
it is worth mentioning that I am using Ajax to paginate (if that matters)
It is also worth mentioning that the API im using to collect data does support pagination however I have not been able to find any documentation on how to "costumize pages" with the paginator
Does anyone have any idea of what I am doing wrong or know how I fix this issue?
What I'm trying to do is give users a way to checkout multiple products from the inventory.
My products index page (lists all available products to be checked out) looks like this:
<?php echo $this->Form->create('multi');?>
<?php foreach ($products as $product): ?>
<tr class="hovertable">
//All the fields go here
<td style="cursor: default">
<?php echo $this->Html->link($this->Html->image('tr/Checkouts_Add.png') . " " . __('Checkout'), array('controller' => 'Checkouts','action' => 'add', $product['Product']['id']), array('escape' => false, 'class' => 'button')); ?>
<?php echo $this->Html->link($this->Html->image('tr/Edit.png'), array('action' => 'edit', $product['Product']['id']), array('escape' => false)); ?>
<?php echo $this->Form->postLink($this->Html->image('tr/Delete.png'), array('action' => 'delete', $product['Product']['id']), array('escape' => false), __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
<?php echo $this->Form->input('Product.id.'.$product['Product']['id'] ,
array('label' => false,
'type' => 'checkbox',
'id'=>'listing_'.$product['Product']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
<?php echo $this->Form->submit(__('Submit'));?>
Then in my checkouts controller I've added a new function to checkout multiple items, I'd like this form to be populated by the checked products
public function multi($count = 1) {
if($this->request->is('post')) {
foreach($this->request->data['Checkout'] as $data) {
//Do not forget this line. you need to create new model for saving each time.
if ($this->request->isPost()) {
$this->Checkout->create();
$this->Checkout->save($data);
} else {
$this->request->data['Checkout']['product_id'] = $productId;
}
}
$this->redirect(array('action' => 'index'));
}
$products = $this->Checkout->Product->find('list');
$users = $this->Checkout->User->find('list');
$this->set(compact('products', 'users'));
$this->set('count', $count);
}
As you see I've tried to add hat I thought might work but the Submit button from the products index page does nothing. Any help would be greatly appreciated!
i just check several times and at least i understand one of the reason of your failure is inside of your foreach you have some error or warning as i don't know about the value of your product array i check this code and it worked well for me :
<?php $products=array('0'=>'11','1'=>'22','2'=>'333');
echo $this->Form->create('User');?>
<?php foreach ($products as $product): ?>
<tr class="hovertable">
//All the fields go here
<td style="cursor: default">
<?php echo $this->Form->input('Product.id.'.$product,
array('label' => false,
'type' => 'checkbox',
'id'=>'listing_'.$product)); ?>
</td>
</tr>
<?php
endforeach;
echo $this->Form->end(__('Submit'));
if you have no error or warning i suggest to you check without
$this->Form->postLink
Hi Im trying to create a function that searches my Uploads model and displays the information that is in the that table.
Notice (8): Undefined variable: uploads [APP/View/Uploads/search.ctp, line 28]
Warning (2): Invalid argument supplied for foreach() [APP/View/Uploads/search.ctp, line 28]
Before I am even allowed to search I get this error,
This is my search.ctp
<?php $uploads = $this->requestAction('uploads/search');
?>
<div id="search">
<?php echo $this->Form->create('Upload',array('action'=>'search'));?>
<fieldset>
<legend><?php __('Upload Search');?></legend>
<?php
echo $this->Form->input('searchupload', array('label' => false, 'class'=>'searchinput'));
$options = array(
'label' => '',
'value' => 'Search',
'class' => 'searchbutton'
);
echo $this->Form->end($options);
?>
</fieldset>
</div>
<div id="contentbox">
<table>
<?php foreach($uploads as $upload) : ?>
<tr>
<td><?php echo $upload['Upload']['name'] ?></td>
<td><?php echo $upload['Upload']['eventname'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
and this is the function in the uploads controller:
function search() {
if (!empty($this->data)) {
$searchstr = $this->data['Upload']['search'];
$this->set('searchstring', $this->data['Upload']['search']);
$conditions = array(
'conditions' => array(
'or' => array(
"Upload.name LIKE" => "%$searchstr%",
"Upload.eventname LIKE" => "%$searchstr%"
)
)
);
$this->set('uploads', $this->Upload->find('all', $conditions));
}
}
Any help would be greatly appreciated!
Thanks in advance
<?php if(!empty($uploads)) : ?>
<?php foreach($uploads as $upload) : ?>
<tr>
<td><?php echo $upload['Upload']['name'] ?></td>
<td><?php echo $upload['Upload']['eventname'] ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<div>
No search matches found
</div>
<?php endif; ?>
This ensures that if an empty set is found then the no search match found is thrown into the webpage
Why are you running the uploads/search twice from the same view?
Try removing this from your view:
<?php $uploads = $this->requestAction('uploads/search'); ?>
It calls the same action you are rendering with search.ctp.
UPDATE:
If you are getting the Undefined Variable notice (that you will not see when in production and debug is set to 0 by the way), all you need to do is set the uploads variable to null in the controller (not the view). Before you run the search do: $this->set('uploads', array()); in the controller so the variable will be defined in the view. You do not want to clutter the view with logic.
i have the following search form
<?php
echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));?>
<?php echo $this->Form->input('SearchTerm',array('label' => 'Find:')); ?>
<?php $options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'); ?>
<?php echo $this->Form->input('options', array('type'=>'select', 'label'=>'Search:', 'options'=>$options)); ?>
<?php echo $this->Form->end('search'); ?>
<?php if($rs!=0){?>
<?php if($rs!=null) { ?>
results found : <?php print $results; //print_r ($result['Order']['full_name']); ?>
<h1 class="ico_mug">Results Matching Term: <?php print '"'.$term.'"' ;?></h1>
<table id="table">
<tr>
<th>Client Name</th>
<th>Order Code</th>
<th>Phone</th>
<th>Received Order</th>
<th>Working On Order </th>
<th>Order Ready </th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($rs as $order): ?>
<tr>
<td>
<?php echo $this->Html->link($order['Order']['full_name'],
array('controller' => 'orders', 'action' => 'view', $order['Order']['id'])); ?>
</td>
<td><?php echo $order['Order']['code']; ?></td>
<td><?php echo $order['Order']['phone']; ?></td>
<td><?php echo $this->OrderStatus->getStatusString($order['Order']['receive_state']); ?></td>
<td><?php echo $this->OrderStatus->getStatusString($order['Order']['working_state']); ?></td>
<td><?php echo $this->OrderStatus->getStatusString($order['Order']['ready_state']); ?></td>
<td> <?php //echo $this->Html->link($this->Html->image("cancel.jpg"), array('action' => 'index'), array('escape' => false));?><?php echo $this->Html->link($this->Html->image("edit.jpg"), array('action' => 'edit',$order['Order']['id']), array('escape' => false));/*echo $this->Html->link('Edit', array('action' => 'edit', $order['Order']['id']));*/?></td>
</tr>
<?php endforeach; ?>
<tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
<?php
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
//$this->Paginator->options($optionss);
$this->Paginator->settings['paramType'] = 'querystring';
?>
<tr ><?php //echo" << ".$this->Paginator->counter(
// 'Page {:page} of {:pages}');
?></tr>
</table>
<?php }else echo"no results found"; ?>
<?php } //endif?>
and this is my controller action
function search($options=null){
$this->set('results',"");
$this->set('term',"");
$this->set('rs',0);
if ((isset($_GET["SearchTerm"]))||(isset($_GET["options"])) ) {
$SearchTerm=$_GET["SearchTerm"];
$options=$_GET["options"];
if (!$options || !$SearchTerm) {
$this->Session->setFlash('Please enter something to search for');
}
else {
$SearchArray = array($options." LIKE " => "%".$SearchTerm."%");
$this->paginate = array('conditions' => $SearchArray,'limit'=>2,'convertKeys' => array($options, $SearchTerm));
$data=$this->paginate('Order');
$this->set('rs', $data);
$this->set('term',$SearchTerm);
}
}
as you can see i am using Get parameters options and SearchTerm. I want theese two to stay in the pagination links. I have tried various fixes that i've found on stackoverflow and on other site, (like ex:
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
yet i still get the following error message :
Indirect modification of overloaded property
PaginatorHelper::$settings has no effect
[APP\View\orders\search.ctp, line 75
why is that? and what about a solution to this ? ( even if i use the wxample from the cookbook
$this->Paginator->settings['paramType'] = 'querystring';
i still get the same error :S can you please help/explain ?
If I understand your code correctly, you should be able to replace:
<?php
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
//$this->Paginator->options($optionss);
$this->Paginator->settings['paramType'] = 'querystring';
?>
with
<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>
Form Fields In Pagination
If you are actually trying to put the form elements into the pagination, you will need to alter your code slightly. You will need to build named parameters for each item in the search options. Then, in the action of the controller, you will need to check for both the request->data and the params['named'] versions of the search to make sure you use them in both cases. I would also clean up the code so it is a little more readable.
First, you need to use the cake methods for getting the data. It will make sure that it cleans the requests for you etc. In addition, you will need to account for the search term and filter options being passed as both a named variable and submitted in a form. So you need to update your controller as follows:
function search($options=null){
$this->set('results',""); // where is this used ??
$this->set('rs', 0);
$this->set('SearchTerm', '');
$this->set('FilterBy', '');
$this->set('options', array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'));
if ($SearchTerm = $this->request->data['Order']['SearchTerm']) or $SearchTerm = $this->params['named']['SearchTerm']) {
if ($FilterBy = $this->request->data['Order']['FilterBy'] or $FilterBy = $this->params['named']['FilterBy'])) {
$this->paginate = array(
'conditions' => array($FilterBy." LIKE " => "%".$SearchTerm."%"),
'limit' => 2,
'convertKeys' => array($FilterBy, $SearchTerm)
);
$this->set('rs', $this->paginate('Order'));
$this->set('SearchTerm', $SearchTerm);
$this->set('FilterBy', $FilterBy);
} else {
$this->Session->setFlash('Please enter something to search for');
}
} else {
$this->Session->setFlash('Please enter something to search for');
}
}
Next, we focus on the view. Remove move this:
$options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail');
It shouldn't be in the view. It belongs in the controller (which is where it is now).
Next, cleanup the form:
<?php
echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));
echo $this->Form->input('SearchTerm',array('label' => 'Find:'));
echo $this->Form->input('FilterBy', array('type'=>'select', 'label'=>'Search:', 'options'=>$options));
echo $this->Form->end('search');
?>
*Note that I changed the options name to FilterBy so it is easier to find in the controller. Also there are other things that could be cleaned up in the view. However, I will only address the things that correspond to the question.
Now you need to replace this code:
<tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
<?php
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
//$this->Paginator->options($optionss);
$this->Paginator->settings['paramType'] = 'querystring';
?>
<tr ><?php //echo" << ".$this->Paginator->counter(
// 'Page {:page} of {:pages}');
?></tr>
</table>
With this code:
</table>
<p>
<?php
$this->Paginator->options(array('url' => array_merge(array('SearchString' => $SearchString, 'FilterBy' => $FilterBy), $this->passedArgs)));
echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%')
));
?>
</p>
<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous'), array(), null, array('class'=>'disabled'));?>
| <?php echo $this->Paginator->numbers();?>
| <?php echo $this->Paginator->next(__('next') . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
You an format it differently of course to fit your needs. But the code should work as expected with the search term and filter option applied to pagination.
Good luck and Happy Coding!