如何在无序列表中显示mysql数据库条目,使用PHP按降序从3到一行显示
你好,我正在学习PHP,并决定创建一个博客。我已经知道如何创建数据库并向其中添加条目。但我很难弄清楚如何以有组织的方式显示这些条目 到目前为止,我已经做到了这一点:如何在无序列表中显示mysql数据库条目,使用PHP按降序从3到一行显示,php,mysql,Php,Mysql,你好,我正在学习PHP,并决定创建一个博客。我已经知道如何创建数据库并向其中添加条目。但我很难弄清楚如何以有组织的方式显示这些条目 到目前为止,我已经做到了这一点: <?php mysql_connect ('localhost', 'user', 'password') ; mysql_select_db ('db'); $sql = "SELECT * FROM tbp_blog ORDER BY timestamp DESC LIMIT 5"; $result = mysql_q
<?php
mysql_connect ('localhost', 'user', 'password') ;
mysql_select_db ('db');
$sql = "SELECT * FROM tbp_blog ORDER BY timestamp DESC LIMIT 5";
$result = mysql_query($sql) or print ("Can't select entries from table tbp_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$date = date("l F d Y g:i:s A", $row['timestamp']);
$link = $row['link'];
$title = stripslashes($row['title']);
$description = stripslashes($row['description']);
$entry = stripslashes($row['entry']);
$image_link = $row['image_link'];
$image_alt = $row['image_alt'];
?>
<ul class="submissions">
<li class="first">
<a href="<?php echo $link; ?>">
<img alt="<?php echo $image_alt; ?>" class="blog_image" height="198" src="<?php echo $image_link; ?>" title="<?php echo $title; ?>" width="276" /></a>
<div class="submissions_content">
<h3><a href="<?php echo $link; ?>"><?php echo $title; ?></a></h3>
<p><?php echo $description; ?></p>
</div>
</li></ul>
<div class="hr">
<img class="star" src="images/star.png" width="40" height="12" alt="Star" /></div>
<?php
}
?>
-
这是指向一个页面的链接,该页面正是我试图获取的示例:
如何让我的条目以降序显示,一行三行,每页最多9个条目。以及自动创建下一页等。我看到他们在每一行的顺序上都使用了li类
我还想格式化我的时间戳,以便它指示我所在时区的时间。
任何帮助都将不胜感激。提前谢谢
Mario这里是PDO中的一个示例 首先,连接到数据库:
$server = 'localhost';
$database = 'db_name';
$db_username = 'username';
$db_password = 'password';
$dsn = "mysql:host=$server;dbname=$database";
try {
$db = new PDO($dsn, $db_username, $db_password);
}
catch (PDOException $e) {
//You can do what you like with any error messages (echo, store, etc)
$error_message = $e->getMessage();
}
接下来,创建将包含查询的函数:
模型中的查询函数(MVC)
调用函数
通常,这会进入控制器(MVC)
这位于网站的HTML部分:
这在视图中(MVC)
这段代码看起来比实际情况要复杂得多,因为我添加了大量的注释供您在站点中实现时阅读
如果您打算创建一个博客,那么了解用于组织和使用代码的模型-视图-控制器(MVC)将是非常明智的。大多数博客都是这样建立的。按降序排列的无序列表。呃?到目前为止你有什么问题?“草莓”的意思可能是开玩笑,但他的观点是正确的。无序列表的要点是顺序没有意义。您确定不需要有序列表吗?另外,如果您仍然可以更改。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,。请在进一步检查您的解决方案之后。我发现这并不是我想要的。它并没有真正解决我的问题,即如何像在wearepndr.com网站上那样显示我的帖子。看来你把我送到了一个完全不同的方向。请按照链接,如果你能帮助我实现这一点,我将非常感谢。
//Use a function that can accept parameters (users id, search term, etc)
//For your question, there are no parameters
function getPosts() {
global $db;
global $database;
$sql = "SELECT * FROM $database.tbp_blog ORDER BY timestamp DESC LIMIT 5";
//If your query was more complex, you would need to learn about binding values
try {
$stmt = $db->prepare($sql);
$stmt->execute();
$posts = array();
//Depending on how many posts you have, you can do fetch or fetchAll
//fetchAll is faster to write but here is the approach with fetch
$result = $stmt->fetch();
while ($result != NULL){
$posts[] = $result;
$result = $stmt->fetch();
}
//Free up resources
$stmt->closeCursor();
//return it to where it was called from
//send back the array we created
return $posts;
}
//Catch any errors
catch (PDOException $exc) {
return '0';
}
}
//Call the function to get posts with no parameters
//The variable "posts" here will become the returned "posts" array from our function
$posts = getPosts();
<?php
if (empty($posts)) {
echo 'There are no results';
}
else {
echo '<ul class="submissions">';
//You can decide what type of loop to use
foreach ($posts as $post) { ?>
<li class="first"><a href="<?php echo $post['link']; ?>"> <img alt="<?php echo $post['image_alt']; ?>" class="blog_image" height="198" src="<?php echo $post['image_link']; ?>" title="<?php echo $post['title']; ?>" width="276" /></a>
<div class="submissions_content">
<h3><a href="<?php echo $post['link']; ?>"><?php echo $post['title']; ?></a></h3>
<p><?php echo $post['description']; ?></p>
</div>
</li>
<?php }
echo '</ul>';
}
?>