如何在无序列表中显示mysql数据库条目,使用PHP以降序显示3到一行

时间:2013-08-10 19:43:37

标签: 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_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
}
?>

这是一个指向页面的链接,这正是我想要获得的一个例子:

http://wearepandr.com/blog

如何让我的条目按降序显示,以三行为单位,每页限制为9个条目。以及自动创建下一页等。我看到他们已经使用了li类,并且每行的顺序都是第二个和最后一个。

我也想格式化我的时间戳,以便在我的时区BC温哥华时间表示时间。 任何帮助将不胜感激。提前谢谢。

马里奥

1 个答案:

答案 0 :(得分:0)

这是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)

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

调用函数

通常这是在控制器(MVC)

//Call the function to get posts with no parameters
//The variable "posts" here will become the returned "posts" array from our function
$posts = getPosts();

这位于您网站的HTML部分:

这是在视图(MVC)

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

此代码看起来比实际情况要多得多,因为我在您的网站中实施该代码时添加了相当多的评论供您阅读。

如果您要创建博客,了解模型视图控制器(MVC)以组织和使用您的代码是非常明智的。大多数博客都是以这种方式构建的。