WordPress $wpdb pagination in plugin

I hawe some problem with writing a plugin for WP. I need to show some info in content from my db. I write a code that show query from my db dut i hawe problem with pagination, i reed many posts for it (like this:Paginate WordPress $wpdb) but i just don’t understand what i need to do with my code for pagination it. Here is part of my code that show some data from my db:
$sSql = "SELECT * FROM pam INNER JOIN images ON pam.id_fake=images.id_zapis WHERE id_type = $type LIMIT ... OFFSET ...";

$myData = array();
$myData = $wpdb->get_results($sSql, ARRAY_A);

$count = $wpdb->num_rows;

echo '<div class="tableCont"> <table>';

for ($i = 0; $i<$count; $i++) {
    echo '<tr>
            <td>';
            if($myData[$i+1]['id_fake']==$myData[$i]['id_fake']){

            echo '<a class="gallery" rel="group" title="'.$myData[$i]['description'].'" href="'.get_option('siteurl').'/wp-content/uploads/'.$myData[$i]['image'].'">
                  <img src="'.get_option('siteurl').'/wp-content/uploads/'.imgsmall($myData[$i]['image']).'" class="images" alt=""></a>';
                while($myData[$i+1]['id_fake']==$myData[$i]['id_fake']){
                    echo '<a class="gallery" rel="group" title="'.$myData[$i]['description'].'" href="'.get_option('siteurl').'/wp-content/uploads/'.$myData[$i]['image'].'">
                          <img src="'.get_option('siteurl').'/wp-content/uploads/'.imgsmall($myData[$i]['image']).'" class="images" alt=""></a>';
                    $i++;
                }
            }
            else{
                echo '<a class="gallery" title="'.$myData[$i]['description'].'" href="'.get_option('siteurl').'/wp-content/uploads/'.$myData[$i]['image'].'">
                  <img src="'.get_option('siteurl').'/wp-content/uploads/'.imgsmall($myData[$i]['image']).'" class="images" alt="" ></a>';
            }
    echo   '</td>
            <td class="description">'.$myData[$i]['description'].'</td>
            <td class="price">Price:<br><br>'.$myData[$i]['price'].'</td>
            </tr>';
}
echo '</table></div>';

can you help me to paged this code?

Related posts

1 comment

  1. The starting point of the process is the query, the preferred solution is the use of a constant with it (pay attention to the use of $wpdb->prepare to sanitize the query parameters):

    $count = 10;
    $from = (isset($_GET['current_page'])) ? $_GET['current_page'] : 0
    $sSql = "SELECT SQL_CALC_FOUND_ROWS * FROM pam INNER JOIN images ON pam.id_fake=images.id_zapis WHERE id_type = %d LIMIT %d,%d";
    $myData = $wpdb->get_results($wpdb->prepare($sSql, array($type,$from,$count)), ARRAY_A);
    $total = $wpdb->get_var("SELECT FOUND_ROWS()");
    
    <YOUR LOOP HERE TO DISPLAY THE RESULTS>
    
    /* To include the pagination links */ 
    echo paginate_links(array(
        'base'         => '%_%',
        'format'       => '?current_page=%#%',
        'total'        => ceil($total/$count),
        'current'      => max($from,1)
        ));
    

Comments are closed.