Results 1 to 3 of 3

Thread: Why is this not counting correctly?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Why is this not counting correctly?

    Hi guys, I am trying to make it count for each indiviual item and display the number next to it.

    Code:
     
    <?$query = mysql_query ( "SELECT * FROM `Stations` LIMIT 6" ); while ( $fea = mysql_fetch_object ( $query ) ){
    
            $num = 1;
    ?>
     <li>
    
    <table border="0" style="background-color:#FFFFFF" width="100%" cellpadding="3" cellspacing="3">
    	<tr>
    		<td>
     <?echo $num;?> </td>
    	
    </table>
                               
                            </li><?$num++; } ?>

    how ever it just echos 1 6 times, rather than 1 - 6

    any ideas?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Why is this not counting correctly?

    because this is inside your loop
    $num = 1;

    so on each iteration, it's setting $num to 1 ...

    Try initializing it to 1 outside of the while loop.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Why is this not counting correctly?

    What a mess...

    PHP Code:
    <?php
        $query 
    mysql_query "SELECT * FROM `Stations` LIMIT 6" );
        
    $num 1;

        while (
    $fea mysql_fetch_object($query)):
    ?>
        <li><?php echo $num++ ?></td></li>
    <?php endwhile; ?>
    Formatting your code isn't just something you do when you're bored; it's essential for spotting mistakes like the one you just made.

Posting Permissions

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



Click Here to Expand Forum to Full Width