-1

我正在尝试将来自 MySQL 数据库的博客文章显示为无序列表。我希望每一行都显示标题、图像和 3 个条目的简短描述。这是一个例子:

<ul class="submissions">
    <li class="first">
        <a href="url"> <img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a>            <div class="submissions_content">
            <h3><a href="url">blog title</a></h3>
            <p> blog discription .</p>
        </div>
</li>       <li class="second">
        <a href="url">
                <img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a>            <div class="submissions_content">
            <h3><a href="url">blog title</a></h3>
            <p> blog discription .</p>
        </div>
</li>       <li class="last">
        <a href="url">
                <img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a>            <div class="submissions_content">
            <h3><a href="url">blog title</a></h3>
            <p> blog discription .</p>
        </div>
</li>       <li class="first">
        <a href="url">
                <img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a>            <div class="submissions_content">
            <h3><a href="url">blog title</a></h3>
            <p> blog discription .</p>
        </div>
</li>       <li class="second">
        <a href="url">
                <img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a>            <div class="submissions_content">
            <h3><a href="url">blog title</a></h3>
            <p> blog discription .</p>
        </div>
</li>       <li class="last">
        <a href="url">
                <img alt="image Alt" class="blog_image" height="198" src="image url" title="blog title" width="276" />
</a>            <div class="submissions_content">
            <h3><a href="url">blog title</a></h3>
            <p> blog discription .</p>
        </div>
</li></ul>

你看到每个条目的 li 类是如何变化的吗?我尝试了这段代码,但 li 类最终对于每个条目都是相同的:

<ul class="submissions">
<?php
mysql_connect ('localhost', 'root', '072868') ;
mysql_select_db ('test');

$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'];
$id = $row['id'];

$li_class = 'first';

?>
<li class="<?php echo $li_class ; ?>">
<a href="<?php echo $link; ?>#blog_nav"><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; ?>#blog_nav"><?php echo   $title; ?></a></h3>
<p><?php echo $description; ?></p>
        </div>

</li>
<?php if ($li_class == 'first') {
        $li_class = 'second';
    } elseif ($li_class == 'second') {
        $li_class = 'last';
    } elseif ($li_class == 'fist') {
        $li_class == 'first';
    }
    ?>
<?php       
 }
 ?>
</ul>

我究竟做错了什么?

4

2 回答 2

1

移出$li_class = 'first';你的 while 条件。您在每次迭代时都重置它。

于 2013-08-17T18:48:45.820 回答
1

$li_class = 'first';

将此行放在while循环之外:

while($row = mysql_fetch_array($result)) {
于 2013-08-17T18:48:48.967 回答