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?
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 this part of code in my view file:
<?php foreach ($posts as $post) { ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['title']; ?></td>
<td><?php echo $post['content']; ?></td>
<td>
<div class="btn-group">
Edit
Delete
</div>
</td>
</tr>
<?php } ?>
Well .. how to send GET parameter with site_url function, this is my anchor part:
a href="<?php echo site_url('admin/blog/show'); ?>" class="btn btn-success">Edit</a>
where to send $post['id'] ?
I see some question in SO, but answers was with hardcode...
here we have a example
echo anchor('news/local/'. $post['id'], 'any text', array('title' => 'The best news!'));
In your controller you will get something like.
echo $this->uri->segment('3');
Also load url helper/library as well.
Should be like this :
Edit
The given code should be like this :
<?php foreach ($posts as $post) { ?>
<tr>
<td><?=$post['id']; ?></td>
<td><?=$post['title']; ?></td>
<td><?=$post['content']; ?></td>
<td>
<div class="btn-group">
Edit
Delete
</div>
</td>
</tr>
<?php } ?>
In your show method access post id like this :
public function show($post_id)
{
echo $post_id;
}
For more : https://www.codeigniter.com/user_guide/helpers/url_helper.html
I am doing ajax pagination in cakephp 3.2
I have done code for forward move of pagination, by getting the last id .
If i want to go backward ,the pagination will not work ,i know .
How can i do it in a proper way so that it will work for both direction as well as direct click on any pagination index.
Below i have attached some of my codes ,which is working properly only for forward move of pagination.
I know the code won't work for backward move.
How can i do it?
///////////////////////////////////PAGINATION STARTS HERE/////////////////////////////////////////////////
if(isset($_POST["page"])){
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
$page_number = 1; //if there's no page number, set it to 1
}
$item_per_page=5;
$get_total_rows = $this->Orders->find('all')->where($condition)->count(); //hold total records in variable
$total_pages = ceil($get_total_rows/$item_per_page);
$page_position = (($page_number-1) * $item_per_page);
if($page_number>1)
{
$condition[] = ['Orders.id >' => $_POST["lastId"]];
}//this one fetch all list greater than last id
$Lists = $this->Orders->find('all')->where($condition)->order(['Orders.id' => 'ASC'])->limit($item_per_page)->toArray();
Thank you
Your Controller Action code should be
$this->paginate = [
'order'=>[
'field_name'=>'desc'
]
];
$condition = [];
$query = $this->YourModel->find()->where($conditions);
$this->set('records', $this->paginate($query));
In view your code should be for listing part only, I dont know whta is your HTML structure but you can follow this, and dont forget about id=pagination_list_container in parent of your table and pagination link code.
<div class="panel-body" id="pagination_list_container">
<div class="inner-spacer">
<table class="table table-striped table-hover margin-0px">
<thead>
<tr>
<th><?php echo $this->Paginator->sort('field_1', 'Column 1') ?></th>
<th><?php echo $this->Paginator->sort('field_2', 'Column_2') ?></th>
<th><?php echo $this->Paginator->sort('field_3', 'Column_3') ?></th>
</tr>
</thead>
<tbody>
<?php
if (empty($records->toArray())) {
?>
<tr><td colspan="100%" class="text-danger text-center">No record found</td></tr>
<?php
} else {
foreach ($records as $record):
?>
<tr>
<td><?php echo $record->field_1 ?></td>
<td><?php echo $record->field_2; ?></td>
<td><?php echo $record->field_3; ?></td>
</tr>
<?php endforeach; ?>
<?php } ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-md-6">
<ul class="pagination">
<?php
$this->Paginator->templates([
'current' => '<li class="active"><a>{{text}}</a></li>',
'number' => '<li>{{text}}</li>'
]);
echo $this->Paginator->prev('«');
echo $this->Paginator->numbers();
echo $this->Paginator->next('»');
?>
</ul>
</div>
<div class="col-md-6 text-right">
<div class="mt30">
<?php
echo $this->Paginator->counter(
'Page {{page}} of {{pages}}, showing {{start}} to {{end}} of {{count}}'
);
?>
</div>
</div>
</div>
</div>
Make AJAX behaviour in your view
You have to Apply Some Javascript event for ajax behaviour into #pagination_list_container
$(document).ready(function(){
$("document").on('click','#pagination_list_container .pagination li a, #pagination_list_container table th > a', function(e){
e.preventDefault();
var link= $(this).attr('href');
if(link!="")
{
$("#pagination_list_container").load(link+ "#pagination_list_container", function(){
console.log("data loaded");
})
}
return false;
});
});
I have a weird problem with Codeigniter and Datamapper,
I am making an CMS system and I am trying to edit an article from the Database using Datamapper. When I click to edit the article I get the message 'You have succesfully saved the article ' without clicking on the SAVE button, also all the data becomes empty and leaves 0. When I enter the data and click on SAVE it saves it but again when I try edit it again it goes back to 0 ... Can someone help out please
Here is my edit function in the controller
public function edit($id = NULL)
{
// Get articles by ID
$articles = new Article_model();
$article = $articles->where('id', $id)->get();
if ($id)
{
$id == NULL || $article;
count($article) || $error = 'Page not found ';
}
else
{
$article = $this->article_model->get_new();
}
$article->title = $this->input->post('title');
$article->text = $this->input->post('text');
if ($article->save())
{
echo 'You have succesfully saved the article';
}
else
{
echo 'Sorry something went terribly worng';
}
$data = array(
'admin_content' => 'admin/article/edit',
'article' => $article,
);
$this->parser->parse('admin/template_admin', $data);
}
and here is my view
<?php if ($this->tank_auth->is_logged_in()): ?>
<div class="container">
<div class="row">
<div class="col-md-9">
<h3><?php echo empty($article->id) ? 'Add an article' : 'Edit a Page ' . $article->title ;?></h3>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>Publication Date</td>
<td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
</tr>
<tr>
<td>Title</td>
<td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
</tr>
<tr>
<td>Body</td>
<td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
</div>
<?php else: redirect('/auth/login'); ?>
<?php endif; ?>
that's not weird at all...every time that controller is loaded it's going to execute the save() method of the article class.
you should probably include some form validation and only save the article if the form was submitted and validation passes.
Thanks for looking on this problem.
I have a page that is totally valid page, and there is a PHP loop that brings in a <li> for each entry of the table.
When i check this page locally it looks 100% OK, but when veiwing the page online the left side bar (which creates this markup is broken randomly mixing <div>'s and <li>'s and i have no clue what the problem is.
This problem is on FF mac and PC (safari looks good)
See page (problem is on the left side)
php code
<?php do { ?>
<li class="clear-block" id="<?php echo $row_Recordset1['penSKU']; ?>">
<a title="Click to view the <?php echo $row_Recordset1['penName']; ?> collection" rel="<?php echo $row_Recordset1['penSKU']; ?>">
<img src="prodImages/small/<?php echo $row_Recordset1['penSKU']; ?>.png" alt="" />
<div class="prodInfoCntnr">
<div class="basicInfo">
<span class="prodName"><?php echo $row_Recordset1['penName']; ?></span>
<span class="prodSku"><?php echo $row_Recordset1['penSKU']; ?></span>
</div>
<div class="secondaryInfo">
<span>As low as .<?php echo $row_Recordset1['price25000']; ?>¢ <!--<em>(R)</em>--></span>
<div class="colorPlacholder" rel="<?php echo $row_Recordset1['penColors']; ?>"></div>
</div>
</div>
<div class="additPenInfo">
<div class="imprintInfo"><span>Imprint area: </span><?php echo $row_Recordset1['imprintArea']; ?></div>
<div class="colorInfo"><span>Available in: </span><?php echo $row_Recordset1['penColors']; ?></div>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<th>Amount</th>
<th>500</th>
<th>1,000</th>
<th>2,500</th>
<th>5,000</th>
<th>10,000</th>
<th>20,000</th>
</tr>
<tr>
<td>Price <span>(R)</span></td>
<td><?php echo $row_Recordset1['price500'];?>¢</td>
<td><?php echo $row_Recordset1['price1000'];?>¢</td>
<td><?php echo $row_Recordset1['price2500'];?>¢</td>
<td><?php echo $row_Recordset1['price5000'];?>¢</td>
<td>Please Contact</td>
<td>Please Contact</td>
</tr>
</table>
</div>
</a>
</li>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
You cannot put a <div> inside of <a>.
Divs are block level elements. Anchors are not. Basically, it's like putting <span> outside of <div>. Doesn't make any sense.
Solution: Move the anchors to inside the divs.
(In the future, if different browsers are displaying it differently, it's probably not the PHP but the HTML.)