简体   繁体   English

插件中的WordPress $ wpdb分页

[英]Wordpress $wpdb pagination in plugin

I hawe some problem with writing a plugin for WP. 我在编写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. 我写了一个代码,显示了我数据库中的查询,我发现了分页问题,​​我为此写了很多帖子(例如: Paginate Wordpress $ wpdb ),但我只是不明白我需要用我的代码进行分页吗。 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 ..."; 这是显示来自我的数据库的一些数据的代码的一部分: $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? 您能帮我分页此代码吗?

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): 该过程的起点是查询,首选解决方案是使用常量(注意使用$ wpdb-> prepare清理查询参数):

$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)
    ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM