Special K.
Special K.

Reputation: 520

Mysql num rows in while loop

I need to display some ordered numbers, from 1 to 15 right before the ID, so basically, I need a new number on every new row, like this:

  1. ID NAME KILLS, etc.
  2. ID NAME KILLS, etc.
  3. ID NAME KILLS, etc. ...
  4. ID NAME KILLS, etc.

Here is the code, I failed with "foreach" for some reason, I got a repeating loop 15x15...

$sql = mysql_query('SELECT * FROM `sc_rank` ORDER BY `kills` DESC LIMIT 15',$connect) or die (mysql_error());
while ($get = mysql_fetch_array($sql))
{
    echo '<tr><td>'.$get["id"].'</td><td>'.$get["name"].'</td><td>'.$get["kills"].'</td><td>'.$get["deaths"].'</td><td>'.$get["hd"].'</td><td>'.$get["eff"].'%</td><td>'.$get["acc"].'%</td><td>'.$get["damage"].'</td><td>'.$get["shots"].'</td><td>'.$get["hits"].'</td></tr>';

Upvotes: 1

Views: 7216

Answers (4)

Dzoki
Dzoki

Reputation: 739

        $sql = mysql_query('SELECT * FROM `sc_rank` ORDER BY `kills` DESC LIMIT 15', $connect) or die(mysql_error());
        $max = mysql_num_rows($sql);
        for($i > 0; $i <= $max; $i++) {
            while($get = mysql_fetch_array($sql)) {
                echo '<tr><td>'.$i.'</td><td>'.$get["id"].'</td><td>'.$get["name"].'</td><td>'.$get["kills"].'</td><td>'.$get["deaths"].'</td><td>'.$get["hd"].'</td><td>'.$get["eff"].'%</td><td>'.$get["acc"].'%</td><td>'.$get["damage"].'</td><td>'.$get["shots"].'</td><td>'.$get["hits"].'</td></tr>'; 
            }
        } 

This one should be fine too:)

Upvotes: -1

Throoze
Throoze

Reputation: 4038

Also, you could try iterating with a for loop, using the mysql_result() php function, something like this:

$sql = mysql_query('SELECT * FROM `sc_rank` ORDER BY `kills` DESC LIMIT 15',$connect) or die (mysql_error());

for ($i = 1; $i <= mysql_num_rows($sql); $i++) {
    $get = mysql_result($sql,$i);
    echo '<tr><td>'.$i.'</td><td>'.$get["id"].'</td><td>'.$get["name"].'</td><td>'.$get["kills"].'</td><td>'.$get["deaths"].'</td><td>'.$get["hd"].'</td><td>'.$get["eff"].'%</td><td>'.$get["acc"].'%</td><td>'.$get["damage"].'</td><td>'.$get["shots"].'</td><td>'.$get["hits"].'</td></tr>';
}

Upvotes: 0

Steve Buzonas
Steve Buzonas

Reputation: 5700

You just need to add an integer to increment.

$sql = mysql_query('SELECT * FROM `sc_rank` ORDER BY `kills` DESC LIMIT 15',$connect) or die (mysql_error());
$i = 0;
while ($get = mysql_fetch_array($sql))
{
    $i++;
    $strDisplay  = "<tr><td>$i.</td><td>$get['id']</td>";
    $strDisplay .= "<td>$get['kills']</td><td>$get['deaths']</td>";
    $strDisplay .= "<td>$get['hd']</td><td>$get['eff']%</td>";
    $strDisplay .= "<td>$get['acc']%</td><td>$get['damage']</td>";
    $strDisplay .= "<td>$get['shots']</td><td>$get['hits']</td></tr>";

    echo $strDisplay;
}

Changed the formatting a bit to make it a little more readable...

Or you can alternatively use a numbered list in HTML.

$sql = mysql_query('SELECT * FROM `sc_rank` ORDER BY `kills` DESC LIMIT 15',$connect) or die (mysql_error());
$i = 0;
$strDisplay = "<ol>";
while ($get = mysql_fetch_array($sql))
{
    $i++;
    $strDisplay  .= "<li><tr><td>$get['id']</td>";
    $strDisplay .= "<td>$get['kills']</td><td>$get['deaths']</td>";
    $strDisplay .= "<td>$get['hd']</td><td>$get['eff']%</td>";
    $strDisplay .= "<td>$get['acc']%</td><td>$get['damage']</td>";
    $strDisplay .= "<td>$get['shots']</td><td>$get['hits']</td></tr></li>";
}

$strDisplay .= "</ol>";
echo $strDisplay;

The numbered list method may not be the best method within a table.

Upvotes: 0

zerkms
zerkms

Reputation: 254926

Initialize a variable $i = 1; right before your loop, echo it and increment $i++; in the end of the loop body

$sql = mysql_query('SELECT * FROM `sc_rank` ORDER BY `kills` DESC LIMIT 15',$connect) or die (mysql_error());

$i = 1;
while ($get = mysql_fetch_array($sql))
{
    echo '<tr><td>'.$i.'</td><td>'.$get["id"].'</td><td>'.$get["name"].'</td><td>'.$get["kills"].'</td><td>'.$get["deaths"].'</td><td>'.$get["hd"].'</td><td>'.$get["eff"].'%</td><td>'.$get["acc"].'%</td><td>'.$get["damage"].'</td><td>'.$get["shots"].'</td><td>'.$get["hits"].'</td></tr>';

    $i++;
}

Upvotes: 2

Related Questions