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 8337283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:15:38+00:00 2026-06-09T04:15:38+00:00

I want to prepare a listing page in PHP with multiple mysql tables. So

  • 0

I want to prepare a listing page in PHP with multiple mysql tables. So that can be N numner of tables and the records in each table can be anything and I want to implement paging for that. Here is the information which is avaialbe to me before writing the paging logic:

define('RECORDS_PER_PAGE_SPLIT', 15);
$htmlTableList = array(
    '_split_1' => 25,
    '_split_2' => 4,
    '_split_3' => 60
);

$currentPage = 1;
if (isset($_REQUEST['page'])) {
    $currentPage = $_REQUEST['page'];
}
if ($currentPage == '' || $currentPage == '0') {
    $currentPage = 1;
}
$offset = ($currentPage - 1) * RECORDS_PER_PAGE_SPLIT;

I want a PHP function (which will contain the logic of multiple-tables-paging) which will return me following array:

//For page=1 in request:
Array{
    0 => "SELECT * FROM _split_1 LIMIT 0, 15"
}

//For page=2 in request:
Array{
    0 => "SELECT * FROM _split_1 LIMIT 15, 25",
    1 => "SELECT * FROM _split_2 LIMIT 0, 4",
    2 => "SELECT * FROM _split_3 LIMIT 0, 1",
}

//For page=3 in request:
Array{
    0 => "SELECT * FROM _split_3 LIMIT 1, 16"
}

//For page=4 in request:
Array{
    0 => "SELECT * FROM _split_3 LIMIT 16, 31"
}

Please provide the function if someone has already written this type of paging logic.
Note: This is just in example, but in my live application number of records in each table will be in lacs. And number of tables are also not fixed. It can be 1 to 5 anything.

Please help.

  • 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-09T04:15:40+00:00Added an answer on June 9, 2026 at 4:15 am

    To elaborate on my answer above… what you want to do is UNION all the result sets together, then you can let MySQL do all the hard work for you.

    So your code would be something like this:

    define('RECORDS_PER_PAGE_SPLIT', 15);
    
    $currentPage = !empty($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
    
    $offset = ($currentPage - 1) * RECORDS_PER_PAGE_SPLIT;
    
    $query = "
      SELECT SQL_CALC_FOUND_ROWS *
      FROM (
        SELECT *
        FROM _split_1
        UNION ALL
        SELECT *
        FROM _split_2
        UNION ALL
        SELECT *
        FROM _split_3
      ) u
      LIMIT $offset, ".RECORDS_PER_PAGE_SPLIT."
    ";
    
    // This contains results for the page
    $result = mysqli_query($conn, $query);
    
    // If you want the total number of results to display or for other calulations
    $query = "
      SELECT FOUND_ROWS()
    ";
    $foundRows = mysqli_query($conn, $query);
    

    If you’re not bothered about knowing the total number of results, you can scrap the second query and remove the SQL_CALC_FOUND_ROWS from the first query.

    Here is more information about SQL_CALC_FOUND_ROWS/FOUND_ROWS()

    Note also that if you are going to apply a LIMIT you should also apply an ORDER BY to guarantee sensible results.

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

Sidebar

Related Questions

I want to create a function that has a select query inside that can
Short What I want to do is, to prepare PHP generated result for print
I want to detect and prepare a list of unique class names that are
I want to do a mvn release:prepare, that will remove the -SNAPSHOT from the
I am graduating this June, so before that I want to prepare myself for
I have a SqlCommand that I want to call Prepare() on whose CommandType =
I want to prepare a demo (that will play sequentially in clicks) for a
For my comfort in Windows, I want to prepare some PHP tools launched like
I want to prepare a text for the use in a LaTeX document. I
I want to use ADO.NET Prepare command will increase performance if query is repeatevely

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.