CodeIgniter Forums
use paginate() on custom function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: use paginate() on custom function (/thread-77273.html)



use paginate() on custom function - pws - 08-10-2020

Hello,
on my model i create new function returned an array:

public function get_corsi_modulo($tipologia_corsi=null,$id_argomenti=null){
$db = \Config\Database::connect();
$req="SELECT * FROM ".$this->table." where banned='no' and status='si'";
if(!is_null($tipologia_corsi))  $req.=" and tipologia_corsi='".$db->escapeString($tipologia_corsi)."'";
if(!is_null($id_argomenti))  $req.=" and id_argomenti='".$db->escapeString($id_argomenti)."'";
$query = $db->query($req);
$results = $query->getResultArray();
return $results;
}


How i can use the method paginate() on this function?
i try in my controller this:

$CorsiModel->get_corsi_modulo($params['tipologia'],null)->paginate();
but is returned error: 
Call to a member function paginate() on array


RE: use paginate() on custom function - InsiteFX - 08-10-2020

Something like this, I use the Query Builder and Model.

PHP Code:
public function get_corsi_modulo($tipologia_corsi null$id_argomenti null)
{
    $db = \Config\Database::connect();
    
    $pager 
= \Config\Services::pager();
    
    $req
="SELECT * FROM ".$this->table." where banned='no' and status='si'";

    if ( ! is_null($tipologia_corsi))
    {
        $req.=" and tipologia_corsi='".$db->escapeString($tipologia_corsi)."'";
    }

    if ( ! is_null($id_argomenti))
    {
        $req.=" and id_argomenti='".$db->escapeString($id_argomenti)."'";
    }

    $query $db->query($req);

    return $this;
}

// You need to pass them to the view with $data.
$data = [
    'posts' => $posts->getLivePosts()->paginate(3),
    'pager' => $posts->pager,
];

// What ever your view is.
echo view('Insitefx\Blog\Views\posts\index'$data);

$CorsiModel->get_corsi_modulo($params['tipologia'], null)->paginate(10); 

Not sure if this will work with the db query but give it a try.

You also need to add the links in the view file.

PHP Code:
<?= $pager->links() ?>

I just wrote a tutorial on pagination in the CodeIgniter 4 Addins.


RE: use paginate() on custom function - pws - 08-10-2020

(08-10-2020, 05:29 AM)InsiteFX Wrote: Something like this, I use the Query Builder and Model.

PHP Code:
public function get_corsi_modulo($tipologia_corsi null$id_argomenti null)
{
    $db = \Config\Database::connect();
    
    $pager 
= \Config\Services::pager();
    
    $req
="SELECT * FROM ".$this->table." where banned='no' and status='si'";

    if ( ! is_null($tipologia_corsi))
    {
        $req.=" and tipologia_corsi='".$db->escapeString($tipologia_corsi)."'";
    }

    if ( ! is_null($id_argomenti))
    {
        $req.=" and id_argomenti='".$db->escapeString($id_argomenti)."'";
    }

    $query $db->query($req);

    return $this;
}

// You need to pass them to the view with $data.
$data = [
    'posts' => $posts->getLivePosts()->paginate(3),
    'pager' => $posts->pager,
];

// What ever your view is.
echo view('Insitefx\Blog\Views\posts\index'$data);

$CorsiModel->get_corsi_modulo($params['tipologia'], null)->paginate(10); 

Not sure if this will work with the db query but give it a try.

You also need to add the links in the view file.

PHP Code:
<?= $pager->links() ?>

I just wrote a tutorial on pagination in the CodeIgniter 4 Addins.

Not work! return paginate to all data not ones returned by function!



RE: use paginate() on custom function - InsiteFX - 08-10-2020

Try this one, you where missing the semi-colons in the sql statements.

PHP Code:
public function get_corsi_modulo($tipologia_corsi null$id_argomenti null)
{
    $db = \Config\Database::connect();
    
    $req
="SELECT * FROM ".$this->table." where banned='no' and status='si';";

    if ( ! is_null($tipologia_corsi))
    {
        $req.=" and tipologia_corsi='".$db->escapeString($tipologia_corsi)."';";
    }

    if ( ! is_null($id_argomenti))
    {
        $req.=" and id_argomenti='".$db->escapeString($id_argomenti)."';";
    }

    $query $db->query($req);

    return $this;
}

// You need to pass them to the view with $data.
$data = [
    'posts' => $posts->getLivePosts()->paginate(3),
    'pager' => $posts->pager,
];

// What ever your view is.
echo view('Insitefx\Blog\Views\posts\index'$data);

$CorsiModel->get_corsi_modulo($params['tipologia'], null)->paginate(10); 

---------------------------------------------------

// CONTROLLER

public function someFunction()
{
    $pager = \Config\Services::pager();

    // You need to pass them to the view with $data.
    $data = [
        'posts' => $posts->getLivePosts()->paginate(3),
        'pager' => $posts->pager,
    ];

    // What ever your view is.
    echo view('Insitefx\Blog\Views\posts\index'$data);

    $CorsiModel->get_corsi_modulo($params['tipologia'], null)->paginate(10);