Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2008
    Posts
    31
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Adding Row Numbers to PHP Table

    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!

    Code:
    <?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>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    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.

    Code:
    <? $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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    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.

    PHP Code:
    <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()) * 1002'.''') . '</td>';
            
    //add the rest of your <td>s for this row
            
    echo '</tr>';
        }
        
    ?>
    </table>
    or maybe something like this

    PHP Code:
    <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()) * 1002'.''') .  '</td>';
            
    //add the rest of your <td>s for this row
            
    echo '</tr>';
        }
        
    ?>
    </table>
    Last edited by bullant; 05-26-2011 at 04:35 AM.

  • #4
    New Coder
    Join Date
    Sep 2008
    Posts
    31
    Thanks
    4
    Thanked 0 Times in 0 Posts
    A quick update... I got the Rank column to work by using this...
    PHP Code:
    </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...
    PHP Code:
    </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.

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by jcx1028 View Post
    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.
    Last edited by bullant; 05-26-2011 at 02:20 PM.

  • #6
    New Coder
    Join Date
    Sep 2008
    Posts
    31
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Bullant, the version I currently have here is one that I stumbled into from another source. At the moment, it looks like this...

    PHP Code:
    <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. It looks something like this...\
    PHP Code:
    $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...

  • #7
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by jcx1028 View Post
    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 (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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •