我正在尝试将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>
我做错了什么?
答案 0 :(得分:1)
将$li_class = 'first';
移到您的while条件之外。你在每次迭代时重置它。
答案 1 :(得分:1)
$li_class = 'first';
将此行放在while
循环的外部:
while($row = mysql_fetch_array($result)) {