...

View Full Version : Dynamic Content Question



steve55
12-12-2003, 09:19 PM
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!

firepages
12-13-2003, 12:02 AM
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 ..



<?
$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

steve55
12-13-2003, 11:49 AM
ok thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum