-2

您好我刚刚学习 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 类。

我还想格式化我的时间戳,以便它指示我所在时区温哥华不列颠哥伦比亚省的时间。任何帮助将不胜感激。提前致谢。

马里奥

4

1 回答 1

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) 以组织和使用您的代码将是非常明智的。大多数博客都是这样构建的。

于 2013-08-12T06:12:29.393 回答