Pagination custom url in codeigniter-4 - php

I created a method in a model of CI4 to query a search results. I need to paginate search results but the URL not provide results. I need to standard way for pagination in CI4.
Here is model method -
<?php
public function search($key)
{
$builder = $this->table('apps_common_all');
$builder->Like('name',$key,'after');
$query = $builder->paginate(51);
return $query;
}
?>
controller method is -
<?php
public function result()
{
$key = $this->request->getVar('s');
$apps = new AppsModel();
$data = [
'items' => $apps->search($key),
'pager' => $apps->pager,
];
return view('search/index',$data);
}
?>
Pagination code in view page
<?php if ($pager) :?>
<?php $pagi_path=getenv('pagi_path').'search_result'; ?>
<?php $pager->setPath($pagi_path); ?>
<?= $pager->links() ?>
<?php endif ?>
when pagination link clicked the url not provide results
http://myshahadat.com/demo/apkdownload/search_result?s=a&page=2
and my route is
<?php $routes->get('/search_result', 'Search::result'); ?>

Related

How to pass data controller to view in codeigniter

I am getting user profile fields from the database and want to display in my view but don't know to do this with the master layout.
this is my model
function fetchProfile($id,$page){
$query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
return $query->result();
}
this is my controller:
public function edit($id,$page){
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
$this->load->view('template_master',$data);
}
I am also trying to find a solution. I am passing user Id and another by URL (get method).
You are overwriting $data['query'] when you assign the array next:
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
Either do:
$data= array('content'=>'view_staff_edit');
$data['query'] = $this->StaffModel->fetchProfile($id,$page); // note position
Or:
$data = array(
'content' = 'view_staff_edit',
'query' => $this->StaffModel->fetchProfile($id,$page),
);
Access in view via $query and $content.
Unrelated:
You are also missing $page in your query, and its generally a good idea to declare gets as null if not set or you will get a notice: public function edit($id=null,$page=null){
Your overriding your first declaration of variable $data what you can do is to initialize them both at the same time.
Controller
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id,$page),
'content' => 'view_staff_edit'
);
$this->load->view('template_master',$data);
}
Then access it on your View file
<h1><?php echo $content ?></h1>
<?php foreach($query as $result): ?>
<p><?php echo $result->id ?></p>
<?php endforeach; ?>
Try doing something like this:
public function edit($id,$page) {
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data['content']= 'view_staff_edit';
$this->load->view('template_master',$data);
}
YOUR MODEL
function fetchProfile($id){
return $this->db->get_where('Id' => $id)->result_array();
}
YOUR CONTROLLER
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id),
'content' => 'view_staff_edit',
);
$this->load->view('template_master',$data);
}
YOUR "template_master" VIEW
<?php foreach ($query as $res){?>
<span><?php echo $res['Id'];?></span> //User html content according to your requirements ALSO you can add all columns retrieved from database
<?php } ?>

CakePHP and conditions

So in my controller MenuController.php I have the following code:
class MenuController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('menus', $this->Menu->find('all'));
$userSpecific = $this->Menu->find('all', array(
'conditions' => array('Menu.user_id' => '20')
));
}
}
and in my view, I am doing the following:
<?php foreach ($menus as $menu): ?>
<?php echo $menu['Menu']['id']; ?>
<?php echo $menu['Menu']['user_id']; ?>
<?php endforeach; ?>
update
To better understand this in my browser I changed my view to the following:
<?php foreach ($menus as $menu): ?>
<p>Menu id <?php echo $menu['Menu']['id']; ?> is for user: <?php echo $menu['Menu']['user_id']; ?></p>
<?php endforeach; ?>
end update
Now in the view, it is currently using the $this and returning all values from the database table, How do I change the view to use $userSpecific rather than $this I managed to get this far (making the $userSpecific part) by using the cookbook but I could only find the controller side and not the view side. I'm sorry if it's a bad question, just trying to learn.
You need to send the data to the view from the controller at the end of the index() function.
You can do it like this:
$this->set('userSpecific', $userSpecific);
or like this (my preferred way)
$this->set(compact('userSpecific');
Once you've done this, you can then modify your view to show the user specific fields as shown:
<?php foreach ($userSpecific as $menu): ?>
<?php echo $menu['Menu']['id']; ?>
<?php echo $menu['Menu']['user_id']; ?>
<?php endforeach; ?>

link_to and passing variables

I have a page: my-account/my-templates/view/1 - which simply uses the id field of 1 to display the item
What I'm wanting to do is to create a copy action, that simply takes all the values in this item and creates a new item
I have a link_to() which holds the following:
<?php echo link_to('Copy', '#copy_template', array('id' => $template->getId())); ?>
I'm passing in the id of the template.
Can I access this id in the copy action?
EDIT:
Actions:
public function executeViewTemplate(sfWebRequest $request)
{
$this->template = Doctrine_Core::getTable('UserTemplate')->getUserTemplate($request->getParameter('id'));
}
public function executeTemplateCopy(sfWebRequest $request)
{
$this->id = $request->getParameter('id');
// get the passed template id
}
Templates:
viewTemplateSuccess.php
<?php echo link_to('Copy', '#copy_template', array('id' => $sf_request->getParameter('id'), 'class'=>'button green')); ?>
templateCopySuccess.php
<?php echo "ID". $id;?> --> doesn't return the passed id
In an action, you can retrieve a parameter using:
$id = $request->getParameter('id');
For example, if you have this action:
public function executeCopyTemplate(sfWebRequest $request)
{
$id = $request->getParameter('id');
}
And from a template:
<?php $id = $sf_request->getParameter('id') ?>
In your case:
<?php echo link_to('Copy', '#copy_template?id='.$sf_request->getParameter('id')); ?>
The array at the end of the link_to function alters HTML attributes like class and id. It doesnt pass url parameters normally. THis will work:
<?php echo link_to('Copy', '#copy_template?id='. $template->getId(), array()); ?>

Implementing pagination in codeigniter, can't get count

I am having difficulties implementing the codeigniter pagination class. I have created my model, view and controller for getting my news articles and data is echoed in the view successfully.
My problem is that when I attempt to implement pagination it seems like I am unable to get the correct count of fields in my database. Can somebody show me what I have done wrong?
The pagination links display perfectly, but the content echoed does not appear to be limited. How can I count the rows of the query?
Required classes for the pagination are autoloaded
Model:
class News_model extends CI_model {
function get_allNews()
{
$query = $this->db->get('news');
foreach ($query->result() as $row) {
$data[] = array(
'category' => $row->category,
'title' => strip_tags($row->title),
'intro' => strip_tags($row->intro),
'content' => truncate(strip_tags( $row->content),200),
'tags' => $row->tags
);
}
return $data;
}
Controller
// load pagination class
$config['base_url'] = base_url().'/news/index/';
$config['total_rows'] = $this->db->get('news')->num_rows();
$config['per_page'] = '5';
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$viewdata['allnews'] = $this->News_model->get_allNews($config['per_page'],$this->uri->segment(3));
View
<?php if (isset($allnews)): foreach ($allnews as $an): ?>
<?php echo heading($an['title'], 2); ?>
<?php echo $an['content']; ?>
<?php endforeach;
else: ?>
<h2>Unable to load data.</h2>
<?php endif; ?>
<?php echo $this->pagination->create_links(); ?>
In your controller, you're passing parameters to your get_allNews method, but your method doesn't make use of those parameters:
$viewdata['allnews'] = $this->News_model->get_allNews($config['per_page'],$this->uri->segment(3));
So you are getting all records, and expecting the limited result set. You need to change the beginning of your get_allNews method like this:
class News_model extends CI_model {
// make use of the parameters (with defaults)
function get_allNews($limit = 10, $offset = 0)
{
// add the limit method in the chain with the given parameters
$query = $this->db->limit($limit, $offset)->get('news');
// ... rest of method below

Codeigniter -> Edit Page

I am getting the following error when I try and access:
domain.co.nz/admin/editpage/home/
I get the following error:
PHP Fatal error: Call to a member function getCMSPage() on a non-object in controllers/home.php on line 22
The issue with this is that I cannot understand why it is being passed back into the main “home” controller - which is the main controller.
All of my models are loaded by default - http://cl.ly/2U1F3a2B0s2K0i3k3g13
Ideal Situation
What I am trying to do with this is load the content into a text area on for editing and when submit is clicked I would like it to go back to the same page with a message saying content updated.
Admin Template
<li><?php echo anchor('#','Edit Pages');?>
<?php if(is_array($cms_pages)): ?>
<ul>
<?php foreach($cms_pages as $page): ?>
<li><a >permalink?>"><?=$page->name?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
Page Model
function updatePage($data){
$data = array('content' => $content);
$this ->db->where('id',$id);
$this->db->update('pages',$data);
}
View:
<?php
//Setting form attributes
$formpageEdit = array('id' => 'pageEdit', 'name' => 'pageEdit');
$formInputTitle = array('id' => 'title', 'name' => 'title');
$formTextareaContent = array('id' => 'content', 'name' => 'content');
?>
<section id = "validation"><?php echo validation_errors();?></section>
<h4><?= $title ?> </h4>
<?php
echo form_open('admin/editpage/'.$page->permalink, $formpageEdit);
echo form_fieldset();
echo form_label ('Content', 'content');
echo form_textarea("content", $page['content']);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
?>
Controller:
function index(){
if($this->session->userdata('logged_in')){
}else{
redirect('admin/home');
}
$page = $this->navigation_model->getCMSPage($this->uri->segment(3));
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = $page->name;
$data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE));
$this->load->view('admintemplate', $data);
}
I haven't really tested this yet but it should give you a good start. What you're asking is quite a bit of work to fully code out quickly.
This is the page_model model it's essentially what you posted with a few minor tweaks. Its best to use id's rather than strings. You may also want to do some htmlspecialchars or mysql hijack validation prior to passing these to your DB.
<?php
/**
* This is the Page_model model, this model handles the retrieval and modification of all pages.
*
**/
class Page_model extends CI_Model {
/**
* the getCMSPage function retrieves the data from the db given the ID that was passed via $id.
**/
function getCMSPage($id = NULL) {
$this->db->where('permalink', $permalink);
$query = $this->db->get('pages', 1);
#check to make sure row's were returned, if so continue, otherwise return false.
if ($query->num_rows() > 0){
#set the results into an array and return $row, should return $row['content'], and $row['id'];
#if you were editing more than one page this is where you would use a foreach($query)
$row = $query->result_array();
return $row;
}else{
return false;
}// END if ($query->num_rows() > 0)
}// END function getCMSPage()
}// END Page_model class
?>
This is the site controller and for sake of time i just echo'd your form_textarea straight from the edit function.. you shouldn't do this as it goes against MVC standards. You should create a view for that portion of the section.
<?php
/**
* This is the Site controller, primary controller for your site.
*
**/
class Site extends CI_Controller {
/**
* construct function, in our case we are going to load the Posts_model , you may not want to do this.
*
**/
function __construct()
{
parent::__construct();
#load Page_model, should be located in app/models/page_model.php
$this->load->model('Page_model');
}//END function __construct();
/**
* edit function, this function handles retrieval of the page from the URI, and the page's content for editing.
*
* This function uses $id which auto retrieves the page's ID from the uri. Your URL should look similiar to:
* http://yourdomain.com/site/edit/3/yourunecessaryinformationhere
* everything after the id is not really required but could help with SEO.
*
**/
function edit($id){
#retrieve the page's content in array form.
$page = $this->Page_model->getCMSPage($id);
echo form_textarea("content", $page['content']);
}
}//END Site Class
Keep in mind i wrote this up in 5-10 minutes so its rushed and not very well commented or even tested but itll give you a great head start. This only gives you an example of how to retrieve information from the DB and echo it into a textarea. You would still need to create another function within your page_model to insert/update the information and another snippet of code in your controller to pass the edited content to the model.
You can hit me up on AIM or twitter if you have more questions ThirdGenSup, twitter is #gorelative
// this line
$data['content'] = $this->load->view('admin/editpage', $data);
// needs to be
$data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE);

Categories