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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:40:43+00:00 2026-06-09T19:40:43+00:00

I have tried a MySQL solution but it does not seem practical and I

  • 0

I have tried a MySQL solution but it does not seem practical and I get a query that takes to long.

I am looking for a PHP solution to this problem:

I have inside my table rows with product data coming from 4 different sites.

I query them using some conditions and I get a result variable:

$result = mysql_query("my query");

Now, I retrieve the data like this:

while($slice = mysql_fetch_assoc($result)){
   $product_data = $slice['productdata'];
   $site     = $slice['site']; 
}

The problem is that I want to display product data by alternating the $site variable:

1. product from site 1;

2. product from site 2;

3. product from site 3;

4. product from site 4;

If all the products from site 2 have been listed, then list the other remaining products like this:

1. product from site 1;

2. priduct from site 3;

3. product from site 4;

And so on.

IMPORTANT: I am paginating the results so the solution cannot break pagination. So i need a total rows number I can paginate.

Could there be a PHP solution?

UPDATE:

$result = mysql_query("SELECT site, product FROM ".$table." WHERE my mysql conditions LIMIT ".$offset.", ".$rowsperpage."");

$rowsperpage = 20;

If site 3 has much more rows then the others, using the code would generate:

Page 1:

  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • Product from site 3;
  • Product from site 3;
  • …..
  • until 20

Page 2:

  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • Product from site 3;
  • Product from site 3;
  • …..
  • until 20

Page 3 would display the remaining site 3 rows:

  • Product from site 3;
  • Product from site 3;
  • Product from site 3;
  • …..
  • until 20

The problem is Page 1 and Page 2 will display more site 3 products than the others.

This is because of the mysql query which has rows mixed and ordered by random. And when I call 20 of them, the sites won’t have equal number of rows.

I want to achieve something like:

Page 1:

  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • …..
  • until 20

Page 2:

  • Product from site 1;
  • Product from site 2;
  • Product from site 3;
  • Product from site 3;
  • Product from site 2;
  • Product from site 3;
  • Product from site 3;
  • Product from site 3;

…..
until 20

What I want to achieve works with the code only if I have the query on each paginated page without the offset and limit

  • 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-09T19:40:46+00:00Added an answer on June 9, 2026 at 7:40 pm

    Here’s (attempt #2) at a solution that may work for you.

    After more thought and comments, it appears that using MySQL to handle pagination via LIMIT/OFFSET will not be an easy task to accomplish considering the ordering you need. That said, the pagination should be handled soley in PHP – the caveat here being that you have to load all of the MySQL results during every page load.

    The idea is to build a 2D-array with each “site” as the index and each sub-array as the list of “products” for each site. After this is built, find the length of the longest list of products – that number will let us know how high we need to iterate to using this “buckets” method.

    Now, iterate from 0 to the length of the longest list of products and, in a nested loop, iterate through each site. If the site doesn’t have a product for the current index, skip it; otherwise:

    • If the current $offest is greater than 0, skip the current product and decrememnt $offset by 1.
    • If $offset is less than or equal to 0, output the product!

    Sample Code:

    // define what page we're on (will probably come from $_GET)
    $page = 1;
    
    // define how many products to display per-page
    $productsPerPage = 20;
    
    // calculate the current offset based on the page-# and the #-per-page
    $offset = (($page - 1) * $productsPerPage);
    
    // get the full results from the database
    $results = mysql_query("your query");
    if (mysql_num_rows($results) <= $offset) {
        // the current page is too high; you could set it to the last page,
        // or loop back to page #1, display an error, etc.
        return;
    }
    
    $sites = array();
    while ($row = mysql_fetch_assoc($result)) {
        if (!isset($sites[$row['site']])) {
            // initialize the products-array for this site
            $sites[$row['site']] = array();
        }
        // add this product to the array for this site
        $sites[$row['site']][] = $row['productdata'];
    }
    
    // get the largest-number of products for a given site
    $maxProducts = 0;
    foreach ($sites as $products) {
        if ($maxProducts == 0) {
            // set the first list of products as the "most"
            $maxProducts = count($products);
        } else if (($count = count($products)) > $maxProducts) {
            // the current list of products is larger than what we've found
            $maxProducts = $count;
        }
    }
    
    // iterate from the $siteOffset to the highest-number of products
    for ($i = 0; $i < $maxProducts; $i++) {
        // iterate through each site and check if it has a product at this "level"
        foreach ($sites as $site => $products) {
            if (isset($products[$i])) {
                // there is a product for this site on this "level";
                // if we haven't reached our offset yet, skip it
                if ($offset-- > 0) continue;
                // otherwise, output it!
                echo $products[$i] . ' from site ' . $site . '<br />';
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried hard searching for the solution to my query but with no
I have tried to search for a solution to my problem, but I'm not
I have tried fetching MySQL query results using mysql_fetch_row() and mysql_result() and numeric values
I have tried the following MySQL update but it only sets the first field
I have a method which does a simple mysql insert, when I tried to
I have tried searching over the internet about this problem but not able to
Rather noobish question but I have tried searching and just cannot find a solution.
I tried the solution given in mysql SELECT COUNT(*) … GROUP BY … not
I have tried several mySQL queries to compare a column of dates stored as
I have tried to use mysql fulltext search in my intranet. I wanted to

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.