Jump to content

Combine SQL query for pagination


petenaylor

Recommended Posts

Hi all

 

I have written a bit of code that I need to change from 4 seperate SQL queries into 1 so I can paginate the results. My code is below:

 

<!-- Get Trader premium adverts -->
   
   <?php 
    $gettraderads = mysql_query("SELECT * FROM `trade-adverts` WHERE type = 'premium' AND paid = 1 AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");
    $numtraderads = mysql_num_rows($gettraderads);
    while ($showtraderads = mysql_fetch_array($gettraderads)) {
   include('trader-ad-cell.php'); 
    }
   ?>
    
    <!-- Get Trader standard adverts -->
   
   <?php 
    $gettraderads = mysql_query("SELECT * FROM `trade-adverts` WHERE type = 'standard' AND categoryid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");
    $numtraderads = mysql_num_rows($gettraderads);
    while ($showtraderads = mysql_fetch_array($gettraderads)) {
   include('trader-ad-cell.php'); 
    }
   ?>
    
    <!-- Get premium adverts -->
    
    <?php 
    $getads = mysql_query("SELECT * FROM `adverts` WHERE categoryid = 1 AND type = 'premium' AND paid = 1 AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");
    while ($showads = mysql_fetch_array($getads)) {
    include('ad-cell.php'); 
    }
   ?>
    
    <!-- Get standard adverts -->
   
    <?php 
    $getads = mysql_query("SELECT * FROM `adverts` WHERE categoryid = 1 AND type = 'standard' AND live = 1 AND approved = 1 AND dateexpired >= '".$todaysdate."' ORDER BY id DESC ");
    while ($showads = mysql_fetch_array($getads)) {
     $standarduserid = $showads['userid'];
     $getstandarduserinfo = mysql_query(" SELECT * FROM `users` WHERE id = '".$standarduserid."'");
     $showstandarduserinfo = mysql_fetch_array($getstandarduserinfo);
    include('standard-ad-cell.php'); 
    }
   ?>

 

Can anyone help me?

 

Many thanks

 

Pete

Link to comment
https://forums.phpfreaks.com/topic/238190-combine-sql-query-for-pagination/
Share on other sites

Your looking at a lot of logic going into pagination on this. You'd have to count how many of each you have and determine how many you want to show on each page. You can't just combine it because you are displaying each result differently. What I would do is implement an AJAX pagination individually for each section.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.