开发者

How to find Index of an array in php while loop [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am fetching data from MySQL database... I want to check index of each row... e.g

while($row=mysql_fetch_array($res开发者_如何学Pythonult)
{
i want index of $row in each row... (current Index).
}

Please Help me. Thanks


If you want the index from the database:

<?php
...
$result = mysql_query("SELECT * FROM whatever");

while($row = mysql_fetch_array($result)) {
    echo $row['index']; 
   # Where index is the field name containing the item ID from your database
}
...
?>

Or, if you want to count the number of items based on the output:

<?php
..
$result = mysql_query("SELECT * FROM whatever");
$index = 0;

while($row = mysql_fetch_array($result)) {
    echo $index.' '.$row['whatever'];
    $index++;
}
..
?>

That's what I understood from your question..


If I understood you correctly you want something like this

<?PHP
$i=0;
while($someThingIsTrue) {
   echo $i;
   $i++;
}
?>


$row is not a member of any array. So, it has no index at all.
And, I suppose, you don't need it either

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜