使模板与变量循环的问题


Issues making a template loops with variables

我正在尝试为我的网站制作一个简单的博客,但没有关于如何正确制作博客的教程,所以我尝试从头开始制作。我现在的问题是在一个带有变量的循环中显示一个帖子模板。以下是我表格中的列:

表格博客

  1. id
  2. user_id
  3. 日期
  4. 所有权
  5. 内容

变量代码:

<?php 
// query //
$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
$row = mysql_fetch_array($sql);
// variables //
$user_id = $row['user_id']; //only the id, not the name
$date = $row['date'];
$title = $row['title'];
$content = $row['content'];
// get user_id name //
$sql2 = mysql_query("SELECT name FROM users WHERE id = '$user_id'");
$row2 = mysql_fetch_array($sql2);
$author = $row2['name']; //last variable
?>

我想做的是,垂直显示下一个模板的id值的所有后代:

<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php echo $content;?></div>
</div>
<hr>

如何在一个简单的页面中循环使用不同变量值的模板?

试试这个:

   <?php
        // query //
        $sql = mysql_query ( "
              SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
              FROM blog b, users u 
              WHERE b.user_id = u.id
              ORDER BY b.id DESC" );
        while ( $row = mysql_fetch_array ( $sql ) ) {
            // variables //
            $user_id = $row ['user_id']; // only the id, not the name
            $date = $row ['date'];
            $title = $row ['title'];
            $content = $row ['content'];
            $author = $row ['name']; // last variable
        ?>
        <div id="container">
            <div align="center" style="background-color: gray">
                <b><?php echo $title;?></b>
            </div>
            <div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
            <div><?php echo $content;?></div>
        </div>
        <hr>
<?php
}
?>

首先进行一个信息查询,如果你有很多行,它会吃掉资源

SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
FROM blog b, users u 
WHERE b.user_id = u.id
ORDER BY b.id DESC

然后您需要使用循环查询

while ( $row = mysql_fetch_array ( $sql ) ) {}

首先,您需要将博客表放在一个数组中:

$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
while($row = mysql_fetch_array($sql)) {
    $blog[] = $row;
}

然后,在模板中:

<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php
foreach($blog as $val) {
    echo "<h1>".$val['title']."</h1>"M
    echo $val['content'];
}
?></div>
</div>
<hr>
$records = mysql_fetch_array($sql);
$blogs_table = '<table> <tr> <th>User id</th> <th>Date</th> <th>Title</th> <th>Content</th></tr>';
foreach($records as $row) {
   $blogs_table .= '<tr>';
   $blogs_table .= '<td>' . $row ['user_id'] .'</td>';
   $blogs_table .= '<td>' . $row ['date'] .'</td>';
   $blogs_table .= '<td>' . $row ['title'] .'</td>';
   $blogs_table .= '<td>' . $row ['content'] .'</td>';
   $blogs_table .= '</tr>';
}
$blogs_table .= '</table>';

最后,您可以将$blogs_table表格打印到任何类似的位置

<div align="right"><?php echo $blogs_table ?></div>