Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:01:55+00:00 2026-06-10T04:01:55+00:00

Since pagination in Codeigniter (IMHO) lack some good stuff for easier implementation when using

  • 0

Since pagination in Codeigniter (IMHO) lack some good stuff for easier implementation when using filters (beginning letter, category, etc.), even though my pagination is fully functional and working – i have two queries running and the only difference between them is that one of them has LIMIT (and OFFSET) at the end.
This second query is needed because i have to get the total number of rows and with my “main” query i can’t get it because it has LIMIT so it returns limited number of rows.

QUERY 1 (“main” query)

SELECT art.*, songs.numsongs 
        FROM _tbl_artists AS art
        LEFT JOIN 
        (SELECT _ARTIST_ID, COUNT(*) AS numsongs 
        FROM _tbl_songs GROUP BY _ARTIST_ID) AS songs 
        ON art._ID_ORIGINAL = songs._ARTIST_ID 
        WHERE art._artist_type = 0 AND art._ARTIST_NAME LIKE 'C%' AND art.id > 0 ORDER BY art._ARTIST_NAME ASC LIMIT 20, 20

QUERY 2 (this one counts rows)

SELECT art.*, songs.numsongs 
        FROM _tbl_artists AS art
        LEFT JOIN 
        (SELECT _ARTIST_ID, COUNT(*) AS numsongs 
        FROM _tbl_songs GROUP BY _ARTIST_ID) AS songs 
        ON art._ID_ORIGINAL = songs._ARTIST_ID 
        WHERE art._artist_type = 0 AND art._ARTIST_NAME LIKE 'C%' AND art.id > 0 ORDER BY art._ARTIST_NAME ASC

Model code (it’s messy, i know 🙂 ):

function loadArtists($type='', $letter='', $uriOffset=0, $perPage='')
{
    $letterEval = preg_match('[0-9]', $letter);
    switch($letterEval):
        case 1:
            $operator = 'REGEXP';
            $letter = "^[0-9]";
            $s = '';
            break;
        case 0:
            $operator = 'LIKE';
            $letter = "$letter";
            $s = '%';
            break;
        default: 0; break;
    endswitch;

    $query = "SELECT SQL_CALC_FOUND_ROWS art.*, songs.numsongs 
        FROM _tbl_artists AS art
        LEFT JOIN 
        (SELECT _ARTIST_ID, COUNT(*) AS numsongs 
        FROM _tbl_songs GROUP BY _ARTIST_ID) AS songs 
        ON art._ID_ORIGINAL = songs._ARTIST_ID 
        WHERE";
        if($type == 0 || $type == 1)
            $query .= " art._artist_type = $type AND";

        if($letter != '')
            $query .= " art._ARTIST_NAME $operator '$letter$s' AND";
        else
            $query .= '';

    $query .= " art.id > 0 ORDER BY art._ARTIST_NAME ASC";

    if ($uriOffset != '')
        $limited = $query . " LIMIT $uriOffset, $perPage";
    else
        $limited = $query . " LIMIT $perPage";

    $noLimit = $this->db->query($query);
    $withLimit = $this->db->query($limited);

    $numRows = $withLimit->num_rows(); 

    $resultStack = array();
    $resultStack = array($numRows);


    if($withLimit->num_rows() > 0)
    {
        $result = $withLimit->result();
        $message = '';
    }

    else
    {
        $result = NULL;
        $message = 'Nema rezultata';
    }

    array_push($resultStack, $result, $message);

    return $resultStack; //$resultStack;
}

So what i actually need is, instead of having these 2 complicated queries running, i’d rather make one query even if it get more complicated but i want to get total rows count from it.

I read something about sql_calc_found_rows but couldnt implement it because i’m totally unfamiliar with it and also many say that it puts additional strain to the database so it should be used only in rare cases.

Any help will be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:01:57+00:00Added an answer on June 10, 2026 at 4:01 am

    You could do something like this to obtain the total number of (unpaginated) rows:

    (See the output on SQL Fiddle)

    SELECT art.*, songs.numsongs, artist_count.total
        FROM _tbl_artists AS art
        JOIN (
            SELECT COUNT(*) as total
            FROM _tbl_artists as art
            WHERE art._artist_type = 0
            AND art._ARTIST_NAME LIKE 'C%' AND art.id > 0
        ) artist_count
        LEFT JOIN (
            SELECT _ARTIST_ID, COUNT(*) AS numsongs 
            FROM _tbl_songs GROUP BY _ARTIST_ID
        ) AS songs ON art._ID_ORIGINAL = songs._ARTIST_ID 
        WHERE art._artist_type = 0
        AND art._ARTIST_NAME LIKE 'C%' AND art.id > 0
        ORDER BY art._ARTIST_NAME ASC
        LIMIT 20, 20;
    

    The first subquery just uses the conditions you’ve provided for searching for artists and counts the total rows (artists) that meet those conditions. We don’t need to JOIN to the _tbl_songs table here as we don’t care whether these artists have any songs in this subquery.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to combine pagination with filtering. Since I have a lot of filters
Since Intellij does not yet support the Play-Scala-Template-Engine I was thinking about using plain
I want to add Pagination to one of my views using MvcPager3. This would
I am ajax-ifying the pagination in one of me projects and since I want
So I've implemented AJAX pagination. The problem is that since the <%= paginate @videos
I am using a Pagination class that I found on Codecanyon that works wonders
Using Ajax pagination via the will_paginate gem I'm having an issue rendering my collection
I'm using CodeIgniter for a classifieds website. Here's a specific example of what I'm
Context : Since we are developing in C# MVC3, we wanted to have some
Possible Duplicate: OData pagination with WebApi ( $inlinecount ) Since Asp.net WebAPi almost supports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.