在帖子列表中突出显示当前帖子项目-PHP


Highlight current post item in a list of posts - PHP

我有一个正在建设中的网站,使用PHP MYSQL,其中包含一个有三列的新闻页面。

  • col1(中间列)显示新闻标题和新闻文本。

  • col2(左列)显示10个最新新闻条目的标题。

  • col3(右栏)-是一个显示月份和年份的归档栏。

正如我上面所说,最近的新闻专栏(col2)显示了10个最新新闻条目的标题。当我点击一个特定的标题时,col1会显示与该标题相对应的主要内容。我想做的是突出显示第2栏中的活动标题。有没有一种方法可以使用css&PHP?

数据库查询如下:

mysql_select_db($database_admin_conn, $admin_conn);
$query_getArchives = "SELECT DISTINCT DATE_FORMAT (news.updated, '%M %Y') AS archive, DATE_FORMAT (news.updated, '%Y-%m') AS link FROM news ORDER BY news.updated DESC";
$getArchives = mysql_query($query_getArchives, $admin_conn) or die(mysql_error());
$row_getArchives = mysql_fetch_assoc($getArchives);
$totalRows_getArchives = mysql_num_rows($getArchives);
mysql_select_db($database_admin_conn, $admin_conn);
$query_getRecent = "SELECT news.news_id, news.title FROM news ORDER BY news.updated DESC LIMIT 10";
$getRecent = mysql_query($query_getRecent, $admin_conn) or die(mysql_error());
$row_getRecent = mysql_fetch_assoc($getRecent);
$totalRows_getRecent = mysql_num_rows($getRecent);

$var1_getDisplay2 = "-1";
if (isset($_GET['archive'])) {
  $var1_getDisplay2 = $_GET['archive'];
$query_getDisplay = sprintf("SELECT news.news_id, news.title, news.news_entry, DATE_FORMAT (news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE DATE_FORMAT(news.updated, '%%Y-%%m') = %s ORDER BY news.updated DESC", GetSQLValueString($var1_getDisplay2, "text"));
} elseif (isset($_GET['news_id'])) {
  $var2_getDisplay3 = $_GET['news_id'];
$query_getDisplay = sprintf("SELECT news.news_id, news.title, news.news_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE news.news_id=%s ", GetSQLValueString($var2_getDisplay3, "int"));
} else {
mysql_select_db($database_admin_conn, $admin_conn);
$query_getDisplay = "SELECT news.news_id, news.title, news.news_entry, DATE_FORMAT (news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT 3";
}
$getDisplay = mysql_query($query_getDisplay, $admin_conn) or die(mysql_error());

以下是新闻页面的html/css的相关摘录:

<div class="col1">
<div class="news">
<?php  while($newsItem = mysql_fetch_array($getDisplay))
{ ?>
<?php $arrayNews[$getDisplay['news_id']] = $newsItem; ?>
<?php foreach($arrayNews AS $newsItem)
{  ?>   
<h1> <?php echo $newsItem['title'] ; ?> <h3><?php echo $newsItem['formatted'];?></h3></h1>
<?php $query_getPhotoDetails = "SELECT *
FROM photos INNER JOIN news2photo USING (photo_id)
WHERE news2photo.news_id = '" . $newsItem['news_id'] . "' LIMIT 3
";
$getPhotoDetails = mysql_query($query_getPhotoDetails, $admin_conn) or die(mysql_error());
while($photo = mysql_fetch_array($getPhotoDetails))
{ ?>
<div class="imagefield">
<p>&nbsp</p>
<p> <img src="../images/<?php echo $photo['filename']; ?>" width="200" />
</p>
</div> 
<?php }
?>
<?php $newsItem['news_entry'] = preg_replace("/((http)+(s)?:'/'/[^<>'s]+)/i", "<a href='"''0'" target='"_blank'">''0</a>", $newsItem['news_entry'] ); ?>
<div class="newsitem">
<p>&nbsp</p>
<p> <?php echo nl2br($newsItem['news_entry']);
?>  
</p>
</div>
<?php } ?>
<?php } ?>
</div><!--end news-->         
</div>
<div class="col2">
<h3>Recent News</h3>
<ul> 
  <?php do { 
 ?> <li class="seperator"><a href="news.php?news_id=<?php echo $row_getRecent['news_id'];
     ?>"><?php echo $row_getRecent['title']; ?></a></li> 
    <?php } while ($row_getRecent = mysql_fetch_assoc($getRecent)); ?>
</ul>

</div>
<div class="col3"><h3>News Archives</h3>
<ul>
  <?php do { ?>
    <li class="seperator"><a href="news.php?archive=<?php echo $row_getArchives['link']; ?>"><?php echo $row_getArchives['archive']; ?></a></li>
    <?php } while ($row_getArchives = mysql_fetch_assoc($getArchives)); ?>
</ul>
</div>

事情进展顺利,但我需要帮助在"最近的新闻"(第2栏)中突出显示当前列表项。我确实搜索了很多,但几乎所有的解决方案都是特定于单词的。提前感谢:)

我不太确定你想要强调什么。

我认为您想做的是,当用户在col2div中时,为<li>添加一些效果?

你可以通过在你的<head></head>标签之间放这样的东西来轻松实现这一点:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
  $(document).onLoad(function(){
    $('.col2 li').hover(function() { this.toggleClass('hovered'); });
  });
</script>
<style type="text/css">
  .hovered { background-color: black; color: white; }
</style>

这样做的目的是在页面加载后,在col2下的<li>标签上打开或关闭CSS类。

编辑:要在col2div中标记当前选定的新闻项目,请在标题中为其创建一个类,并根据提交的news_id:应用它

<head>
  <style type="text/css">
    .currentItem { background-color: darkorange; }
  </style>
</head>
...
<h3>Recent News</h3>
<ul>
  <?php do { ?>
  <li class="seperator
      <?php echo (isset($_GET['news_id']) && $_GET['news_id'] == $row_getRecent['news_id']
                  ? 'currentItem' : '') ?>">
    <a href="news.php?news_id=<?php echo $row_getRecent['news_id']?>">
      <?php echo $row_getRecent['title'] ?>
    </a>
  </li>
  <?php } while ($row_getRecent = mysql_fetch_assoc($getRecent)); ?>
</ul>

EDIT2:要在col3中标记当前选定的月份,您必须这样标记<li>

<li class="seperator
           <?php echo (isset($_GET['archive'] && $row_getArchives['link'] == $_GET['archive']
                       ? 'currentItem' : '') ?>">
  <a href="news.php?archive=<?php echo $row_getArchives['link'] ?>">
    <?php echo $row_getArchives['archive']; ?>
  </a>
</li>