SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    escape after 3 turns during turning query

    The code below produces the result below
    Code:
    code
    
    $test=mysql_query(" 
    select name
    from myTable
    limit 5
    ") ;
    
    echo "start";
    while( $row = mysql_fetch_array($test)  )
    {
    echo $row['name']."<br>";
    }
    echo "end";
    
    result
    
    start
    Tom
    Jane
    John
    Kate
    Tim
    end
    I like to make it to escape after 3 turns.

    The would-be code below doesn't work correctly, but I hope it shows what I want.

    Code:
    would-be code
    
    
    $test=mysql_query(" 
    select name
    from myTable
    limit 5
    ") ;
    
    $i=1;
    echo "start";
    while( $row = mysql_fetch_array($test)  )
    {
    echo $row['name']."<br>";
    if ($i==3)
    { escape() }
    
    $i=$i+1;
    
    }
    echo "end";
    
    target result
    
    start
    Tom
    Jane
    John
    end

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First of all, why not just change the limit?

    But, regardless, here's my suggestion:
    PHP Code:
    $NameQuery MySQL_Query('
    SELECT
        name
    FROM
        myTable
    LIMIT 5
    '
    );

    echo 
    'start';
    echo 
    '<ul>';
    for(
        
    $i 0$row MySQL_Fetch_Assoc($NameQuery);
        
    $i && $row !== false;
        
    $i++, $row MySQL_Fetch_Assoc($NameQuery)
    ){
        echo 
    '<li>' $row['name'] , '</li>';
    }
    echo 
    '</ul>';
    echo 
    'end'
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to leave the loop after n-times, you use a counter variable and php's break; statement, however I agree with Jake - why not just change the limit?


Bookmarks

Posting Permissions

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