将旧的分页mysql_query php转换为PDO

时间:2014-03-31 00:23:28

标签: php oop pdo pagination

我正在尝试切换到使用PDO。我在我的网站上创建了一个新闻栏目。 我的数据库连接包含在我的标题

我目前正在使用它来创建分页,但需要帮助添加我的帖子 至$data = array("Hey","Hello","Good-Bye");截至目前,嘿,Hello和再见是唯一与分页合作的项目。

有人能帮我得到这个结果:$data = array("add my post here")

以下是我的代码:我的数据库连接位于标题部分。

<?php
    class Pagination{
        var $data;

        function Paginate($values,$per_page){
            $total_values = count($values);

            if(isset($_GET['page']))
            {
                $current_page = $_GET['page'];

            }
            else
            {
                $current_page = 1;

            }


            $counts = ceil($total_values / $per_page);
            $param1 = ($current_page - 1) * $per_page;
            $this->data = array_slice($values,$param1,$per_page);

            for ($x=1; $x<= $counts; $x++)
            {
                $numbers[] = $x;    
            }

            return $numbers;


        }

        function fetchResult(){
            $resultsValues = $this->data;
            return $resultsValues;

        }
        }



?>

我假设我需要<ul class="notices"> - </ul>(如下所示)$data = array("Hey","Hello","Good-Bye");下方 <ul class="notices"> <?php $query = $db->query("SELECT id, date, title, description FROM htp_news"); while ($row = $query->fetch(PDO::FETCH_ASSOC)) { ?> <li data-id="id-<?=$row["id"] ?>"> <article class="box white-bg mb-25"> <div class="notices-title"><?=$row["title"] ?></div> <div class="newsdate" style="margin: 10px 0 !important;"><?= date("F d, Y", strtotime($row["date"])); ?></div> <div class="articletext"><style>.articletext img{width:100%; height:auto;}</style><?php $string = $row["description"]; $max = 300; // or 200, or whatever if(strlen($string) > $max) { // find the last space < $max: $shorter = substr($string, 0, $max+1); $string = substr($string, 0, strrpos($shorter, ' ')).'...'; } echo $string; ?></div> <div class="btn small green white-tx"><a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/htp-news.php?id=<?=$row["id"] ?>">Continue Reading</a></div> </article> </li> <?php } ?> </ul> <div class="grid_8"> <div class="paginationwrap" align="center"> <!--This is where the pagination numbers are being generated--> <?php $pag = new Pagination(); $data = array("Hey","Hello","Good-Bye");//This is where I'm getting confused..? $numbers = $pag->Paginate($data,1); $result = $pag->fetchResult(); foreach($result as $r) { echo '<div>'.$r.'</div>'; } foreach($numbers as $num) { echo'<a href="hydro-transpak-news.php?page='.$num.'">'.$num.'</a>'; } ?> 不知道如何做到这一点?

{{1}}

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

不知道为什么需要它,但为了避免重复代码和javascript,您可以这样做,然后将数据放在$inside_ul变量中。

<?php
$query = $db->query("SELECT id, date, title, description FROM htp_news");
$inside_ul = '';
while ($row = $query->fetch(PDO::FETCH_ASSOC)) 
{
    $inside_ul .= '
        <li data-id="id-'.$row["id"].'">
            <article class="box white-bg mb-25">
                <div class="notices-title">'.$row["title"].'</div>
                <div class="newsdate" style="margin: 10px 0 !important;">'.date("F d, Y", strtotime($row["date"])).'</div>
                <div class="articletext"><style>.articletext img{width:100%; height:auto;}</style>
    ';
    $string = $row["description"];
    $max = 300; // or 200, or whatever
    if(strlen($string) > $max) {
        // find the last space < $max:
        $shorter = substr($string, 0, $max+1);
        $string = substr($string, 0, strrpos($shorter, ' ')).'...';
    }
    $inside_ul .= $string.'</div>
                <div class="btn small green white-tx">
                    <a href="http://'.$_SERVER['SERVER_NAME'].'/htp-news.php?id='.$row["id"].'">Continue Reading</a>
                </div>
            </article>
        </li>
    ';
}
?>
<ul class="notices"><?= $inside_ul; ?></ul>
相关问题