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
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/OFFSETwill 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
0tothe length of the longest list of productsand, in a nested loop, iterate through each site. If the site doesn’t have a product for the current index, skip it; otherwise:$offestis greater than 0, skip the current product and decrememnt$offsetby 1.$offsetis less than or equal to 0, output the product!Sample Code: