0

我正在尝试从一个表中选择多行,具体取决于另一个表中给出的 ID。

我已经使用下面的代码进行了一半的工作,但是根据分配给它的不同标签的数量,它会多次回显每个博客,我该怎么做才能在博客文章的一份副本上显示多个标签?

$sqlCommand = "SELECT blogid, blogtitle, content, blogtime, category, blogseourl, author FROM blog ORDER BY blogtime DESC";
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$blogDisplay = '';
        while ($row = mysqli_fetch_array($query)) {
        $blogid = $row["blogid"];
        $blogtitle = $row["blogtitle"];
        $content = $row["content"];
        $blogtime = $row["blogtime"];
        $category = $row["category"];
        $blogseourl = $row["blogseourl"];
        $author = $row["author"];
        $contentshort =  substr($content, 0, 250);
    $sqlCommand2 = "SELECT tag FROM blogtags WHERE blogid='$blogid'";
    $query2 = mysqli_query($myConnection, $sqlCommand2) or die (mysqli_error());        
        while ($row = mysqli_fetch_array($query2)) {
        $tag = $row['tag'];

$blogDisplay .= '<h1><a href="/blog/'. $blogseourl .'"> ' . $blogtitle . ' </a></h1> ' . $contentshort . '... <a href="/blog/'. $blogseourl .'">Read More...</a><br /><br /> ' . $author . ' posted on ' . $blogtime . ' &#124;  Category: ' . $category . ' &#124;  Tags: ' . $tag . ' &#124; <a href="/blog/'. $blogseourl .'#disqus_thread"></a>'; 
        }
        }
mysqli_free_result($query); 

因此,除了为每个标签回显多个 $blogDisplay 之外,一切都正常工作。

有人有什么想法吗?

4

1 回答 1

0

您必须将 blogDisplay 分为两个部分,并列出其间的选项卡。或者您必须缓冲 taglist 并将其作为参数插入到 $blogDisplay

第一个最简单:

$sqlCommand = "SELECT blogid, blogtitle, content, blogtime, category, blogseourl, author FROM blog ORDER BY blogtime DESC";
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$blogDisplay = '';
        while ($row = mysqli_fetch_array($query)) {
        $blogid = $row["blogid"];
        $blogtitle = $row["blogtitle"];
        $content = $row["content"];
        $blogtime = $row["blogtime"];
        $category = $row["category"];
        $blogseourl = $row["blogseourl"];
        $author = $row["author"];
        $contentshort =  substr($content, 0, 250);
    $sqlCommand2 = "SELECT tag FROM blogtags WHERE blogid='$blogid'";
    $query2 = mysqli_query($myConnection, $sqlCommand2) or die (mysqli_error());        

/*first part, all the html before the taglist */
$blogDisplay .= '<h1><a href="/blog/'. $blogseourl .'"> ' . $blogtitle . ' </a></h1> ' . $contentshort . '... <a href="/blog/'. $blogseourl .'">Read More...</a><br /><br /> ' . $author . ' posted on ' . $blogtime . ' &#124;  Category: ' . $category . ' &#124;  Tags: ';

        while ($row = mysqli_fetch_array($query2)) {
        $tag = $row['tag'];
/**add the taglist*/
$blogDisplay .= $tag.' ';
        }

/**last part, all the html after the taglist*/
$blogDisplay .= ' &#124; <a href="/blog/'. $blogseourl .'#disqus_thread"></a>';

        }
mysqli_free_result($query); 
于 2013-04-17T13:21:20.693 回答