duandie5707 2016-08-09 10:11
浏览 84

是否可以使用PHP PDO以每页5 x网格的方式对Mysql查询结果进行分页?

I want to query a database of urls to images and then display them using php. So far I haven't been able to find any examples without using javascript (dont want to use since it loads all the images on the same page and the javascript controls just what you see).

Other pagination examples just simply don't allow you to output the results like this....

image1 image2 image3 image4 image5
image6 image7 image8 image9 image10

1 2 3 4 5 Next

...without doing away with pagination via php and having to hardcode the pagination. Which doesn't make much sense because the images may be more or less at any given time.

Anyone know if this is possible?

For example...

In this script, the loop can only control the amount of records shown per page. I need to be able to break the td after a specified number of records have been output.

<?php

require_once'connect.php';
$query = $db->query("SELECT COUNT(id) FROM table");
$rows = $query->fetchColumn();
$items_per_page = 25;
$last_page = $rows/$items_per_page;

if(isset($_GET['p']) && ctype_digit($_GET['p']) && $_GET['p'] > 0) {
    $page = $_GET['p'];
}
else {
    $page = '1';
}

if($page > $last_page) {
    $page = $last_page;
}

$page_controls = '<p class="page_controls">';
$start = ($page -1) * $items_per_page;

if($items_per_page > $rows) {
    $page_controls .= '<span>1</span>';
}
else {
    if($page > 1) {
        $prev = $page - 1;
        $page_controls .= '<a href="?p='. $prev .'">Prev</a>';

        for($i = ($page - 3); $i < $page; $i++) {
            if($i > 0) {
                $page_controls .= '<a href="?p=' . $i .'">' . $i . '</a>';
            }
        }
    }

$page_controls .= '<span>'. $page .'</span>';

if($page < $last_page) {
    for($i = $page+1; $i <= $last_page; $i++) {
        $page_controls .= '<a href="?p=' . $i . '">' . $i . '</a>';
            if($i >= $page+3) {
                break;
            }
    }

    $next = $page + 1;
    $page_controls .= '<a href="?p=' . $next . '">Next</a>';
    }
}

$page_controls .= '</p>';

$query = $db->prepare("SELECT * FROM table limit ?, ?");
$query->bindParam(1, $start, PDO::PARAM_INT);
$query->bindParam(2, $items_per_page, PDO::PARAM_INT);
$query->execute();

echo '<table>';

$cols = 5;

foreach($query->fetchAll(PDO::FETCH_ASSOC) as $data) {


    echo '<tr>
    <td><a href="' . $data['url'] . '"><img src="' . $data['image_url'] . '"  width=200 height=100></a></td>
    </tr>';

}

echo '</table>';

echo $page_controls;

?>
  • 写回答

1条回答 默认 最新

  • douqie6454 2016-08-09 19:24
    关注

    Someone posted a solution earlier but then deleted it, so I can't give proper credit. However, here it is with a small modification that completes my question...

    echo '<table>';
    
    $cols = 5;
    
    foreach($query->fetchAll(PDO::FETCH_ASSOC) as $cnt => $data) {
        if ( $cnt % $cols == 0 ) {
            echo '<tr>';
        }
    
        echo '<td><a href="<' . $data['url'] . '"><img src="' . $data['image_url'] . '"></a></td>
        ';
    
        if (  $cnt % $cols == 5 ) {
            echo '</tr>';
        }    
        $cnt++;
    }
    
    echo '</table>';
    

    Overall, I agree it is best to control the layout of the images with CSS though. If you prefer that then the previous code is best.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看