【发布时间】: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;
}
}
?>
我假设我需要<ul class="notices"> - </ul>(如下所示)代替$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 我需要知道如何将
<ul class="notices">中的所有内容添加到$data = array中,ul 将代替 Hey Hello Goodbye。 “添加我的帖子等于”<ul class="notices">并列出数据。
标签: php oop pdo pagination