Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2007
    Posts
    31

    Exclamation Unanswered: Read line by line.

    I want to do a demo page to simulate the read of the temperature from a room. I want to do that read from a file.txt. In this file i write data manualy:
    18 celsius or just 18
    19
    20
    19
    21
    ...
    I want to show this data in my page. php row by row in 30 seconds. First 18, after 30 seconds 19, after 30 sec 20...
    I have this script, but I have an error on line 3, sintax error: Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\licenta\mysql1.php on line 3.


    <?php
    session_start();
    if( (!isset($_SESSION['cnt']) $_SESSION['cnt'] = 0) );
    $file = file_get_contents('fisier.txt');
    $file = explode("\n",$file);
    if( $_SESSION['cnt'] >= count($file) ){
    $val = 'Last value was read';
    } else {
    $val = trim($file[$_SESSION['cnt']);
    $_SESSION['cnt']++;
    }
    echo '$val';


    Thanks

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    The hint was in the error message : line 3

    if( (!isset($_SESSION['cnt']) $_SESSION['cnt'] = 0) );

    doesn't look right to me. What should it be?

  3. #3
    Join Date
    Apr 2007
    Posts
    31
    Thanks..i have modified.. but I stil have an error here:Fatal error: Function name must be a string
    $val = trim($file($_SESSION['cnt']));

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Errrr.... $file isn't a function...

  5. #5
    Join Date
    Apr 2007
    Posts
    31
    Ok.I have the script..it is working.., but just the script. When I put the php code in a html page I get this error:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\licenta\zzz.php:10) in C:\xampp\htdocs\licenta\zzz.php on line 11
    Temperatura: 20

    "Temperatura: 20" is the first line in my file.txt.
    You know what is the problem..?
    Thanks

  6. #6
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Look out for whitespace outside of your <??> tags and don't do header redirects after you have echo'd something. Please post both your files for a better debug.

  7. #7
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Oh, and you can't start a session after you have output (echo'd) something...

  8. #8
    Join Date
    Apr 2007
    Posts
    31
    First file-I get the error:

    <html>

    <head>

    <meta http-equiv="refresh" content="10" />
    </head>

    <body>



    <?php
    session_start();
    if (!isset ( $_SESSION ['cnt'])) $_SESSION ['cnt'] = 0;
    $file = file_get_contents ('fisier.txt');
    $file = explode ("\n",$file);
    if( $_SESSION ['cnt'] >= count ($file) )
    {
    $val = 'Readed';
    }
    else
    {
    $val = (trim ($file[$_SESSION['cnt']]));
    $_SESSION ['cnt']++;

    }
    ?>
    Temperatura: <?php echo $val; ?>

    </body>

    </html>

  9. #9
    Join Date
    Apr 2007
    Posts
    31
    Second file..just the script, is working:

    <?php
    session_start();
    if (!isset ( $_SESSION ['cnt'])) $_SESSION ['cnt'] = 0;
    $file = file_get_contents ('fisier.txt');
    $file = explode ("\n",$file);
    if( $_SESSION ['cnt'] >= count ($file) )
    {
    $val = Last value has read;
    }
    else
    {
    $val = (trim ($file[$_SESSION['cnt']]));
    $_SESSION ['cnt']++;

    }
    echo $val;

  10. #10
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    session_start needs to come at the top...
    it should be the first thing you do in a script (if you're going to use sessions)

  11. #11
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    As soon as you start outputting (by writing <html> blah blah blah...) you've already sent the headers to the client. THEN, you're trying to call sesson_start which itself has to alter the headers, but wait, i've already sent the headers... DOH! Error...

    In summary : put <?session_start();?> at the top of your script.

  12. #12
    Join Date
    Apr 2007
    Posts
    31
    Thanks, resolved.

Posting Permissions

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