...

View Full Version : Adding Row Numbers to PHP Table



jcx1028
05-25-2011, 06:22 PM
I have two potentially dumb questions, but I'm kind of a novice when it comes to PHP and MySQL and to this point, my Googling hasn't turned up any answers, so if anyone could help me out, I'd appreciate it.

Here's the page I'm currently working on: http://www.howtomeetbroads.com/battle/stats/. It display the stats from this page: http://www.howtomeetbroads.com/battle/.

While I managed to figure out how to display the name, wins and losses categories, I'd like to also add a row number at the start of each row that would represent the rank of each individual. So the top person would be number one, and the number would increase by one as you go down. And I can't seem to figure out how to do that.

In addition, I'd like to also have a category for Win Percentage, which would be determined by a formula that looked something like: Wins/(Wins+Losses). But I don't know how to write that PHP code.

Below is the code for the Stats page that I'm working on and, again, any help you guys could provide would be greatly appreciated. Thanks!


<?php

include('mysql.php');
include('functions.php');

// Get the top10
$result = mysql_query("SELECT *, ROUND(score/(1+(losses/wins))) AS performance FROM images ORDER BY ROUND(score/(1+(losses/wins))) DESC LIMIT 0,500");
while($row = mysql_fetch_object($result)) $top_ratings[] = (object) $row;

// Close the connection
mysql_close();

?>
</head>

<body>

<table border="1" cellpadding="6" width="85%">
<tr>
<!-- <th width="XX%">Rank</th> -->
<th width="40%">Name</th>
<th width="25%">Wins</th>
<th width="25%">Losses</th>
<!-- <th width="XX%">Win Pct.</th> -->

</tr>
<? foreach($top_ratings as $key => $image) : ?>
<tr style="font-size:14px;">
<!-- <td>PHP code for row numbers?</td> -->
<td><a href="http://www.howtomeetbroads.com/<?=$image->first?>-<?=$image->last?>"><?=$image->first?> <?=$image->last?></a></td>
<td><?=$image->wins?></td>
<td><?=$image->losses?></td>
<!-- <td>PHP code for win pct.?</td> -->
</tr>
<? endforeach ?>
</table>

</body>
</html>

Old Pedant
05-26-2011, 02:46 AM
Well, I am *NOT* a PHP programmer, at all.

But I think I can take a stab at this, based on other systems/languages and my smattering of PHP.



<? $rownum = 0; ?>
<? foreach($top_ratings as $key => $image) : ?>
<tr style="font-size:14px;">
<td><?= ++$rownum ?></td>
<td>
<a href="http://www.howtomeetbroads.com/<?=$image->first?>-<?=$image->last?>">
<?=$image->first?> <?=$image->last?>
</a>
</td>
<td><?=$image->wins?></td>
<td><?=$image->losses?></td>
<td><?= ( $image->wins * 100.0 / ($image->wins + $image->losses) ) ?>%</td>
</tr>
<? endforeach ?>


That percentage is liable to come out looking like 64.8172312% but I'll let a real PHP person tell you how to round that to the number of decimal places you want.

bullant
05-26-2011, 03:56 AM
I'm pretty sure Old Pedant's suggestion will spit out errors.

Maybe use this demo as a guide.

First though, it looks like you have the wins and losses properties in $image declared as public. Although many developers do the same thing, imo it is bad coding practice and using accessor methods is better (a legacy from my Java days).

Also I'm not sure if $top_ratings is an indexed or associative array. If it's indexed, then you should be able to use $key+1 as your row number instead of $rowNum.



<table>
<?php
$rowNum = 1;
foreach ($top_ratings as $image) {
echo '<tr>';
echo '<td>' . $rowNum++ . '</td>';
echo '<td>' . number_format($image->getWins() / ($image->getWins() + $image->getLosses()) * 100, 2, '.', '') . '</td>';
//add the rest of your <td>s for this row
echo '</tr>';
}
?>
</table>
or maybe something like this



<table>
<?php
foreach ($top_ratings as $key => $image) {
echo '<tr>';
echo '<td>' . ($key+1) . '</td>';
echo '<td>' . number_format($image->getWins() / ($image->getWins() + $image->getLosses()) * 100, 2, '.', '') . '</td>';
//add the rest of your <td>s for this row
echo '</tr>';
}
?>
</table>

jcx1028
05-26-2011, 01:43 PM
A quick update... I got the Rank column to work by using this...

</tr>
<? $i = 1;
foreach($top_ratings as $key => $image) : ?>
<tr style="font-size:14px;">
<td><?php echo $i; ?></td>
<td><a href="http://www.howtomeetbroads.com/<?=$image->first?>-<?=$image->last?>"><?=$image->first?> <?=$image->last?></a></td>
<td><?=$image->wins?></td>
<td><?=$image->losses?></td>
<!-- <td>PHP code for win pct.?</td> -->
</tr>
<? $i++; endforeach ?>

...In place of this...

</tr>
<? foreach($top_ratings as $key => $image) : ?>
<tr style="font-size:14px;">
<!-- <td>PHP code for row numbers?</td> -->
<td><a href="http://www.howtomeetbroads.com/<?=$image->first?>-<?=$image->last?>"><?=$image->first?> <?=$image->last?></a></td>
<td><?=$image->wins?></td>
<td><?=$image->losses?></td>
<!-- <td>PHP code for win pct.?</td> -->
</tr>
<? endforeach ?>

Bullant, I believe I copied and pasted your suggestions correctly, but I got errors.

bullant
05-26-2011, 02:17 PM
Bullant, I believe I copied and pasted your suggestions correctly....

Not quite. I don't normally like stopping and starting php tags as often as you have in your code.

Without seeing your error messages or the code for your $image class I have no idea what the problem is.

As a start, after you run the script view the source html in your browser. This will be the html that your php code generates and see if there is anything obviously wrong in the outputed html.

Also. post the error messages.

jcx1028
05-26-2011, 06:44 PM
Bullant, the version I currently have here (http://www.howtomeetbroads.com/battle/stats/) is one that I stumbled into from another source. At the moment, it looks like this...



<table border="1" cellpadding="6" width="85%">
<tr>
<th width="10%">Rank</th>
<th width="40%">Name</th>
<th width="25%">Wins</th>
<th width="25%">Losses</th>
<!-- <th width="XX%">Win Pct.</th> -->

</tr>
<? $i = 1;
foreach($top_ratings as $key => $image) : ?>
<tr style="font-size:14px;">
<td><?php echo $i; ?></td>
<td><a href="http://www.howtomeetbroads.com/<?=$image->first?>-<?=$image->last?>"><?=$image->first?> <?=$image->last?></a></td>
<td><?=$image->wins?></td>
<td><?=$image->losses?></td>
<!-- <td>Code for Win Pct?</td> -->
</tr>
<? $i++; endforeach ?>

</table>


The $top_ratings thing is referenced in the code at the top of the page, and I simply left it there from the code for the main page (http://www.howtomeetbroads.com/battle/). It looks something like this...\


$result = mysql_query("SELECT *, ROUND(score/(1+(losses/wins))) AS performance FROM images ORDER BY ROUND(score/(1+(losses/wins))) DESC LIMIT 0,500");
while($row = mysql_fetch_object($result)) $top_ratings[] = (object) $row;

When attempting to utilize your suggestions (which I genuinely appreciate, by the way), I get the following errors...

Option 1...
Fatal error: Call to undefined method stdClass::getWins() in /home/content/j/c/l/jclayton23/html/battle/statsNEW.php on line 74

Option 2...
Fatal error: Call to undefined method stdClass::getWins() in /home/content/j/c/l/jclayton23/html/battle/statsNEW.php on line 73

I'm open to any suggestions regarding cleaning up this code or whatever because, as mentioned, I don't really know what I'm doing with PHP code. For instance, when you say my "properties in $image declared as public" or that my "$top_ratings is an indexed or associative array," I have no idea what you're talking about. But, you know, I'm slowly learning... or at least trying to...

bullant
05-27-2011, 12:13 AM
When attempting to utilize your suggestions (which I genuinely appreciate, by the way), I get the following errors...

Option 1...
Fatal error: Call to undefined method stdClass::getWins() in /home/content/j/c/l/jclayton23/html/battle/statsNEW.php on line 74

Option 2...
Fatal error: Call to undefined method stdClass::getWins() in /home/content/j/c/l/jclayton23/html/battle/statsNEW.php on line 73

I'm open to any suggestions regarding cleaning up this code or whatever because, as mentioned, I don't really know what I'm doing with PHP code. For instance, when you say my "properties in $image declared as public" or that my "$top_ratings is an indexed or associative array," I have no idea what you're talking about. But, you know, I'm slowly learning... or at least trying to...

I was only suggesting you use accessor methods to access class properties and the demo code I posted assumes you would write your own getWins() method for your $mage class. If the methods don't exist in your class then you will get the errors you are getting.

So in this case just continue to access the properties directly with $image->wins to get your code working.

From your above quote it appears you may be trying to run, or at least trot, before you can walk. If interested, in this thread (http://www.codingforums.com/showthread.php?p=1093571&highlight=drive#post1093571) (post 5) I have a working demo on how a basic php class works. It might help you understand what I was suggesting regarding not declaring properties public and accessor methods.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum