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 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    -
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic Content Question

    I have a php script that updates a text file through a web browser - the text file is then included in a web page and the contents shown.. i.e If the text file is updated then so is the web page (in effect an easy way of updating the content on a web page).

    However my text file has over 50 lines of text in it, but my web page only show the first line. Can anyone help me find out why it won't display all 50?

    This is the code for my 'dynamic web page'

    This is currently placed just after the <body> tag.

    <?php
    if (file_exists("bb.txt")) //does the file exist?
    {
    $FILE = fopen("bb.txt","r"); // open data.txt for reading
    $steve = fgets($FILE,4096); // read the file
    fclose($FILE); // close file
    }
    else
    {
    $steve=1; // no file yes - default to rate of 1
    }

    //write the form ...
    ?>


    Then the following has to be placed on the page where you would like the content to appear <?php echo $steve?>

    ------------------------------------------------------------------------

    The text file (bb.txt) is updated from a seperate page made up of this code:-

    <?php
    if (!isset($HTTP_POST_VARS['newrate'])){ //did they enter a new text?
    if (file_exists("bb.txt")) //does the file exist?
    {
    $FILE = fopen("bb.txt","r"); // open bb.txt for reading
    $steve = fgets($FILE,4096); // read the file
    fclose($FILE); // close file
    }
    else
    {
    $steve=1; // no file yes - default to rate of 1
    }
    }
    else
    {
    $steve=$HTTP_POST_VARS['newrate']; // trying to update the text - keep it in the form
    }


    //write the form next...
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    Password:
    <input type="password" name="password"/>
    <br />
    Rate:
    <textarea name="newrate" cols="60" rows="10"><?php echo $steve?></textarea>
    <input name="submit" type="submit" value="Update"/>
    </form>
    <?php
    if (isset($HTTP_POST_VARS['password'])){ //have they input the password?
    if ($HTTP_POST_VARS['password']!="demo"){ //check the password
    print "<h1>Invalid Password - no update made</h1>";
    }
    else {
    if (isset($HTTP_POST_VARS['newrate'])){
    $FILE = fopen("bb.txt","w"); // password ok - open for writing
    fputs($FILE,$HTTP_POST_VARS['newrate']); //save the text
    fclose($FILE); // close bb.txt
    print "<h1>Update made</h1>";
    }
    }
    }
    ?>

    --------------------------------------------------------------

    In summary there are three files involved the text file (which needs to be be chmod 777) , the page to update the content and the dynamic web page. I don't understand why only the first line is showing - I'm quite new to php so if there is an easy solution to this I apologise in advance!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 96 Times in 94 Posts
    fgets($FILE,4096);

    only gets the 1st 4096 Bytes of the file , http://www.php.net/fgets

    you could stick that if a while loop ..

    PHP Code:
    <?
    $fp 
    fopen'filename.txt' 'r' ) ;
    while ( !
    feof $fp ) ) { 
        
    $str .= fgets$fp 4096 ) ;
    }
    ?>

    or quick and easy is

    <?
    $str 
    implode'' file'filename.txt' ) ) ;
    ?>

    or if you have php >= 4.3.0 

    <?
    $str 
    file_get_contents'filename.txt' ) ;
    ?>
    its ALL in the manual , do yourself a favour and grab the windows CHM searchable manual
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    -
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok thank you.


  •  

    Posting Permissions

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