duanhuo3392 2009-02-23 22:31
浏览 72
已采纳

当使用php和mysql从数据库列出信息时,你会如何使第一行看起来与其他行不同?

Basically I have articles in my database and I want to alter the way the first record displays. I want the lastest (Posted) article to be the focus and the older article just to list, (see F1.com). I need to know how to get the first of my values in the array and get it to display differently but I am not sure how to do this, I can do it so all rows display the same just not how to alter the first row. I also need to know how to tell the rest of the rows to display the same afterwards im guessing you use an if statement there and before that some kind of count for the rows.


Current code:

    $result = mysql_query("SELECT * FROM dbArticle WHERE userID='".$_SESSION["**"]."' ORDER BY timestamp DESC");

while($row = mysql_fetch_array($result))
  {
  echo "<h2 class=\"heading1\">". $row['title'] ."</h2>";
  echo "By:  ".$row['username']."  Type: ".$row['type']."  Posted: ".$row['timestamp']." 
  $body = $row['body'];
  echo "<br/><p>";
  echo substr("$body",0,260);
  echo "...<span class=\"tool\"><a class=\"blue\" href=\"index.php?pageContent=readArticle&id=".$row['id']."\">Read More</a></span></p><hr/>";
  }
mysql_close($con);

Ok I have taken Luke Dennis's code and tried to test it, but I am getting this error: Warning: Invalid argument supplied for foreach() this is the line of the foreach statment. Something that has just come to mind is that I will only want 5 or so of the older articles to display. This is what I have thats creating the error:

<? $con = mysql_connect("localhost","****","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("******", $con);

$result = mysql_query("SELECT * FROM dbArticle ORDER BY timestamp DESC");
$first = true;    
foreach($result as $row){
    if($first)
    {
        echo"".$row['title']."";
        echo"this is the headline";

        $first = false;
    }
    else
    {
        echo"".$row['title']."";

    }
}

?>

Do I need to add mysql_fetch_array somewhere to set the array up?

  • 写回答

8条回答 默认 最新

  • dtgr3392 2009-02-23 22:36
    关注

    I would just iterate through the results and apply a css class to the first entry:

    $first = true;    
    while ($row = mysql_fetch_assoc($result)) {
      $cssClass = '';
      if ($first) {
        $cssClass = 'highlight';
      }
      echo '<p class="' . $cssClass . '">' . $row['text'] . '</p>';
      $first = false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100