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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    null line problem

    PHP Code:
    <?php
    $handle 
    = @fopen("sql.txt""r");
    if (
    $handle) {
        
    $i=1;
        while (!
    feof($handle)) {
            
    $buffer fgetss($handle4096);
            echo 
    $i." - ".$buffer."<br/>";
            
    $i++;
        }
        
    fclose($handle);
    }
    ?>

    it read file like following
    why it assing number as it is null (no line) ?
    Code:
    1 - SHOW AUTHORS
    2 -
    3 - SHOW BINARY LOGS
    4 -
    5 - SHOW BINLOG EVENTS
    6 -
    7 - SHOW CHARACTER SET
    8 -
    9 - SHOW COLLATION
    10 -
    11 - SHOW COLUMNS
    12 - SHOW CONTRIBUTORS
    13 - SHOW CREATE DATABASE
    14 - SHOW CREATE EVENT
    15 - SHOW CREATE FUNCTION
    16 - SHOW CREATE PROCEDURE
    17 - SHOW CREATE TABLE
    18 - SHOW CREATE TRIGGER
    19 - SHOW CREATE VIEW
    20 - SHOW DATABASES
    21 - SHOW ENGINE
    22 - SHOW ENGINES
    23 - SHOW ERRORS
    24 - SHOW EVENTS

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Are those lines blank in sql.txt? You're echoing the line number, hyphen, and data with no check if the line is empty.

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    Are those lines blank in sql.txt? You're echoing the line number, hyphen, and data with no check if the line is empty.
    yes they are blank in sql.txt. how i can block to show in page ?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by zodehala View Post
    yes they are blank in sql.txt. how i can block to show in page ?
    PHP Code:
    function notempty($a){
       return (
    $a !== '');
    }

    $content file("sql.txt");
    $newcontent array_filter($content,"notempty"); 
    because that lines could be just \n or could contain spaces you must change notempty function to do what you need.

    best regards

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $handle fopen'./sql.txt''r' );

    if ( 
    FALSE !== $handle )
    {
        
    $i 1;
        while ( 
    FALSE !== ( $buffer fgetss$handle ) ) )
        {
            
    // only display non-empty lines
            
    if ( '' !== trim$buffer ) )
            {
                echo 
    $i++, ' - '$buffer"<br />\n";
            }
        }
        
    fclose$handle );

    Or suck them all in using file() and automatically avoid the crap. These flags are only available since PHP 5, which you should be using by now anyhow.
    PHP Code:
    $lines file'./sql.txt'FILE_SKIP_EMPTY_LINES FILE_IGNORE_NEW_LINES );

    foreach ( 
    $lines as $i => $line )
    {
        echo 
    $i 1' - '$line"<br />\n";


  • The Following 2 Users Say Thank You to kbluhm For This Useful Post:

    oesxyl (10-28-2009), zodehala (10-28-2009)

  • #6
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    PHP Code:
    $handle fopen'./sql.txt''r' );

    if ( 
    FALSE !== $handle )
    {
        
    $i 1;
        while ( 
    FALSE !== ( $buffer fgetss$handle ) ) )
        {
            
    // only display non-empty lines
            
    if ( '' !== trim$buffer ) )
            {
                echo 
    $i++, ' - '$buffer"<br />\n";
            }
        }
        
    fclose$handle );

    first code is ok


  •  

    Posting Permissions

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