简体   繁体   English

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

[英]Assign a number to echoed data via 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. 该代码根据select语句中表达的条件选择前五行,然后从数字指定的列中提取数据,然后将返回的数据回显到页面。

if( $connect === false )
{
  echo "Could not connect.\n";
  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.\n";
  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. 我需要做的是回显的数据,必须有一个数字,例如:更高级别将是1st,依此类推。 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. 我的想法是使它们显示在ol内,并且每个用户名都将在li内,因此默认情况下,您可以设置样式列表以按顺序显示数字。 Then I can't manage to think how to do that. 然后,我无法思考该怎么做。 Could you give me some hints? 你能给我一些提示吗? Thanks! 谢谢!

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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM