douyan8413 2012-05-11 18:00
浏览 31
已采纳

通过PHP为回显数据分配一个数字

This code selects the first five rows based on the criteria expressed in the select statement then it fetches the data from the columns specified by number then it echos the returned data to the page.

if( $connect === false )
{
  echo "Could not connect.
";
  die( print_r( sqlsrv_errors(), true));
}

/* Select Statement */
$select = "SELECT TOP(5)name, lv FROM Character WHERE lv > 0 AND name NOT LIKE '%@%' AND name NOT LIKE '%[GM]%'
AND name NOT LIKE '%[GA]%' AND name NOT LIKE '%[DEV]%' ORDER BY lv DESC ";

/* Execute the query. */
$query = sqlsrv_query( $connect, $select);

if ( $query )
{
} 
else 
{
  echo "Error in statement execution.
";
  die( print_r( sqlsrv_errors(), true));
}

/* Iterate through the result set printing a row of data upon each
iteration.*/
while( $row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_NUMERIC))
{
 echo "<b>Name</b>: &nbsp;".$row[0]." <br/>";
 echo "<b>Level</b>: &nbsp;".$row[1]."<br/>";
 echo "<br/>";

}

/* Free statement and connection resources. */
sqlsrv_free_stmt( $query);
sqlsrv_close( $connect);
?>

What I need it to do is the echoed data, must have a number, for example: higher level will be 1st and so on. This already orders the data by level, but I can't manage to echo a number for each name. My thought was to making them to appear inside an ol and each user name will be inside a li so by default you can style list to display numbers in order. Then I can't manage to think how to do that. Could you give me some hints? Thanks!

展开全部

  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2012-05-11 18:04
    关注

    I think this is what you're looking for. It will print a number before each name.

    $counter = 1;
    while( $row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_NUMERIC))
    {
     echo $counter.". <b>Name</b>: &nbsp;".$row[0]." <br/>";
     echo "<b>Level</b>: &nbsp;".$row[1]."<br/>";
     echo "<br/>";
     $counter++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部