Welcome Guest, Not a member yet? Register   Sign In
Problems with pagination
#1

(This post was last modified: 04-14-2020, 03:07 AM by SigveR.)

Hello,

I have been trying to figure out how to set up pagination. I've read the documentation several times, but can't understand why it is not working. It should be simple from my understanding.

This is the error I'm receiving

PHP Code:
Argument 1 passed to CodeIgniter\Database\BaseResult::getResult() must be of the type stringnull givencalled in 

This is my controller at the moment:

PHP Code:
  public function my_listings() {
        $jobs = new \App\Models\Jobs();

        if (!$this->ionAuth->loggedIn())
        {
            return redirect()->to('/logg-inn');
        }

        $this->meta =
            (object)array(
                'site_name' => 'Lynt',
                'title' => 'Innstillinger',
                'description' => 'test',
            );

        $data = ([
            'meta' => $this->meta,
            'uri' => $this->uri,
            'session' => $this->session,
            'ionAuth' => $this->ionAuth,
            'validation' => $this->validator,
            'jobs' => $jobs->paginate(10)->get_users_jobs(),
            'pager' => $jobs->pager()->get_users_jobs(),

        ]);


        echo view("moonlanding/header_moonlanding"$data);
        echo view("moonlanding/userhome/my_listings"$data);
        echo view("moonlanding/footer_moonlanding"$data);

    

And my model:

PHP Code:
    public function get_users_jobs() {
        $this->categories = new \App\Models\Categories();
        $builder $this->db->table('jobs');
        $builder->select('*')
                ->join('users''users.id = jobs.jobs_u_id')
                ->join('categories''category_id = jobs.jobs_category')
                ->where('jobs_u_id ='$this->current_user->id);
        $builder->orderBy('jobs_id''desc');
        $query $builder->get();
        if ($builder->countAllResults() > 0)
        {
            return $query;

        } else {
            return false;
        }
    

It is working perfectly before trying to mess it up with adding the pagination. I appreciate any help on this.
Reply
#2

(This post was last modified: 04-14-2020, 04:38 AM by wdeda.)

Hi!
You will probably have to use 2 models.
In my case, for example, I have a record where the four main genres are listed, when I click on one of the genres, all the albums I have of that genre are listed.

Model where I collect the DB data:
PHP Code:
<?php namespace App\Models;

// 20200408

use CodeIgniter\Model;


class 
AlbunsDataModel extends Model
{
    
    
protected $table 'albuns';
    protected $allowedFields = ['id''title''ano'];
    protected $returnType 'object';    
    


Model with the research arguments:
PHP Code:
<?php namespace App\Models;

//20200408

use CodeIgniter\Model;

class 
AlbunsListModel extends Model
{
    public function getGenre1()
    {
        $request = \Config\Services::request();
        $uri $request->uri;
        $id $uri->getSegment(3);
        $db db_connect();
        $query $db->table('albuns')
        ->where('id'$id)
        ->get();

        foreach($query->getResult() as $row);
      
        
return $row->style1;
        
    
}

    public function numGenre1()
    {
        $request = \Config\Services::request();
        $uri $request->uri;
        $id $uri->getSegment(3);
        $db db_connect();
        $query $db->table('albuns')
        ->where('id'$id)
        ->get();

        foreach($query->getResult() as $row);
        
        $numgnr1 
$db->table('albuns');
        $numgnr1->where('style1'$row->style1);
        $numgnr1->orderBy('ano''asc');
        $data $numgnr1->countAllResults();
        
        
return $data;
    }
    
    
//and so on... 

Controller:
PHP Code:
<?php namespace App\Controllers;
 
 
//20200410
 
    use CodeIgniter\Controller;
    use App\Models\AlbunsDataModel;
 
  use App\Models\AlbunsListModel;

    class Albumlists extends Controller {    
        
    
public function genre1()
    {        
        $dataModel 
= new AlbunsDataModel();
        $listModel = new AlbunsListModel();

        $style $listModel->getGenre1();
                
        $data 
= [
            'genre1' => $dataModel->orderBy('player_id''ano''title''asc')
            ->where('style1'$style)
            ->paginate(20),
            'pager' => $dataModel->pager,
            'total' => $listModel->numGenre1(),
            'genre' => $listModel->getGenre1()
        ];
        
        $genre 
$listModel->getGenre1();

      $header = array ( 
 
'icon' => 'favicon'
 
'css' => 'albumlists',
 
'title' => $genre' records - wdeda',
 
'search' => '/ci4/search/allmedia/',
 
'placeholder' => 'Pesquisar'
 
);

 echo 
view('templates/header'$header);
 echo 
view('content/albums/genre1'$data); 
 echo 
view('templates/footer');
    }
    
    
//and so on...
    
Reply
#3

I also have problem with pagination. I'm new to codeigniter and just started learning last 2 weeks and I'm stuck on how to make the pagination work on my project. I have also read the documentation and still can't understand how it works for paginating a query where I joined a table with other tables. I also tried paginating first the main table but how could i print out the data of a specific user from other tables? Please can someone help me.

This is my Controller:
Code:
class SubjectCode extends BaseController
{

    public function index()
    { ...}

    public function users()
    {   
            helper(['form']);
            $db = db_connect();
            $model = new CustomModel($db);

            $data = [
                'users' => $model->getUsers()->get()->getResultArray(), //I got an error if I paginate this line: Call to a member function paginate() on array
                'pager' => $model->pager
            ];
         
            return view('/subj_students', $data);
    }

Then this is my Model
Code:
    function getUsers(){
       
        $builder =  $this->db->table('users')
                        ->orderBy('users.usr_lname', 'ASC')
                        ->join('classes', 'usrs.class_id = classes.class_id')
                        ->join('subjectCode', 'users.usr_id = subjectCode.subjectCode_id');
        return $builder;

    }

and here is my View
Code:
           <tbody>
              <?php if($students): ?>
                  <?php $i = 1; ?>
                      <?php foreach($users as $student): ?>
                      <tr>
                           <td align="center"><?php echo $i; ?></td>
                         <td><?php echo $student['usr_lname'].", ".$student['usr_fname']; ?></td>
                         <td><?php echo $student['usr_id_no']; ?></td>
                         <td><?php echo $student['course'].$student['year'].$student['section']; ?></td>
                         <td><?php echo $student['subjectCode_name']; ?></td>
                      </tr>
                     <?php $i++;
                     endforeach; ?>
                 <?php else: ?>
                 <tr>
                     <td colspan='5'>No record found.</td>
                 </tr>
             <?php endif; ?>
           </tbody>
         </table>
         <div class="row">
          <div class="col-md-12">
            <div class="row">
              <?php if ($pager) :?>
              <?php $pagi_path= '/subj/subj_students'; ?>
              <?php $pager->setPath($pagi_path); ?>
              <?= $pager->links() ?>
              <?php endif ?>       
            </div>
          </div>
        </div>

Please send help.
Reply
#4

(This post was last modified: 07-03-2020, 11:10 AM by captain-sensible.)

maybe i'm old school but if i can't get something to work i start first with minimalist code; when that works i build on it.

this is my blog model :

<?php namespace App\Andy;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;


class BlogModel extends Model

{

protected $table = 'blog';
protected $primaryKey = 'Id';
protected $allowedFields = ['title','article','image','slug','date'];
protected $limit;
protected $offset;
protected $Id;
protected $title;
protected $article;
protected $slug;


this is my controller
public function doPaginate()
{
$handle = new BlogModel();
$data = [
'title'=>'paginate',
'blogs' => $handle->paginate(5),
'pager' => $handle->pager,
'date'=>$this->myDate
];

echo view('header',$data);
echo view('listBlogsPaginate',$data);
echo view('footer',$data);


}


//and this is the nitty gritty of the view listBlogsPaginate


<?php



foreach($blogs as $blogy)
{



echo "<h11><a href = blogArticle/" . $blogy['slug'] . "> ". $blogy['slug'] ." </a> </h11> <br> ";



}


so this just lists the blogs i have , i limit blogs for each page to 5


then the bit showing links on view is just :
<h3 class ="links"><?= $pager->links() ?></h3>



//so basically paginate is a method that does a lot of work behind the scene . My advice when working with a new concept strip everything back to focus on the new concept . Once you get it working then build on it . My understanding is that basically paginate gets the total records in a database and splits it up as per :

$handle->paginate(5); // groups of five listings

//the pager in a view takes you to the next group

and if you loo at this line : 'blogs' => $handle->paginate(5),

'blogs' is an array holding the field data, which i access using a standard " foreach

...as "



where you want to filter stuff i've done this :


$result= $handle->where('category','Mens')->paginate(5);
$pager=$handle->where('category','Mens')->pager;



this is about a database where there is a field called "category"
Reply




Theme © iAndrew 2016 - Forum software by © MyBB