【问题标题】:Converting old pagination mysql_query php to PDO将旧分页 mysql_query php 转换为 PDO
【发布时间】:2014-03-31 00:23:27
【问题描述】:

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

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

谁能帮我得到这个结果:$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;

        }
        }



?>

我假设我需要&lt;ul class="notices"&gt; - &lt;/ul&gt;(如下所示)代替$data = array("Hey","Hello","Good-Bye"); DOWN BELOW 不知道该怎么做?

    <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>'; 
        }

?>

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

【问题讨论】:

  • 不太明白,你问的是$data = array("Hey","Hello","Good-Bye", "add my post here");吗?
  • @ailvenge 我需要知道如何将&lt;ul class="notices"&gt; 中的所有内容添加到$data = array 中,ul 将代替 Hey Hello Goodbye。 “添加我的帖子等于”&lt;ul class="notices"&gt; 并列出数据。

标签: php oop pdo pagination


【解决方案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>

【讨论】:

  • 感谢您向我展示了如何做到这一点。不幸的是,我认为这就是让分页在帖子上工作所需要的,但事实并非如此。我会继续,除了这个作为答案,因为这就是我所问的,这就是我想要实现的。谢谢。
猜你喜欢
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
相关资源
最近更新 更多