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 5 of 5
  1. #1
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts

    for loop integer variable always reads the same

    all,

    I have this small loop that explodes a delimited text file:
    PHP Code:
    $log fread($handlefilesize($logfile));
    fclose ($handle);

    $log explode(PHP_EOL PHP_EOLtrim($log)); 
    $numitems count($log);
    $cutoff floor($numitems 5);
    $itemcounter 0;

    //FIRST TABLE HERE
    echo '<div style="width:250px; position:absolute; left:0px"><table border="0" cols="1">';

    for (
    $i 0$i $cutoff 1$i++) {
        echo 
    '<tr>';
        echo 
    '<td align="center">' $log[$i] . '</td>';
        echo 
    '</tr>';

            if (
    $itemcounter == $cutoff 1) {
                break; }
                
        
    $itemcounter $itemcounter 1;

    }

    echo 
    '</table></div>'
    i is always reading as cutoff * 1. it doesn't start at 0 like it should. i'm sure the syntax is wrong, and i am not astute at PHP, so if someone could point out what i need to change here to get $i to start at 0 that would be great! thanks!

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    PHP Code:
    for ($i 0$i $cutoff 1$i++) { 
    should be
    PHP Code:
    for ($i 0$i == $cutoff 1$i++) 
    or
    PHP Code:
    for ($i 0$i <= $cutoff 1$i++) 
    You used assignment "=" instead of comparison "==" or "===".

  • #3
    Regular Coder
    Join Date
    Nov 2009
    Location
    Scotland / Glasgow
    Posts
    184
    Thanks
    1
    Thanked 19 Times in 19 Posts
    You loop should be something like

    for ($i = 0; $i < $cutoff * 1; $i++) {

    The second param of a for loop is an if statement of sorts so you can't have =.

    In the loop you should always be checking for less than< or greater than >

    Hope it helps
    .:To me AS is like LEGO, Only for the big Kids :.
    - Site - Blog - Glasgow Flasher

  • Users who have thanked [Paul Ferrie ] for this post:

    ajetrumpet (03-03-2010)

  • #4
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    thanks for the help you too. got it fixed!

  • #5
    Regular Coder
    Join Date
    Nov 2009
    Location
    Scotland / Glasgow
    Posts
    184
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Nice one
    .:To me AS is like LEGO, Only for the big Kids :.
    - Site - Blog - Glasgow Flasher


  •  

    Posting Permissions

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