Jump to content

[SOLVED] welcome message for front of site help


thewooleymammoth

Recommended Posts

I wanted to have the site owner (probably me) create a text file named 1.txt or 2.txt or whatever and have the page show the contents of the file in order of smallest to largest (as decided by the name of the txt file) but nothing shows up at all

<?php
while($lastart!=0){
$lastart=1;
$filename="$lastart".".txt";
$openfile=fopen($filename, "r");
$content=fread($openfile, filesize($filename));
fclose($openfile);
if(!fread($openfile, 'r')){
	$lastart=0;
	}else{
	echo "<table>
  <tr>
    <td>"."date(F d Y H:i:s., filemtime($filename)"."</td>
  </tr>
  <tr>
    <td>"."$content"."</td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>";
$lastart=$lastart+1;
	}
	}
?>

 

nothing shows up which im assuming means the while statement is satisfied? if that is the case i would like to know how i would go about looping it so that when there is no file with the number its looking for it ends the loop. if that is not the case i just dont know what is and help would be appreciated. thanks

 

Eric

First, your loop never gets to the table echo

 

<?php
...
fclose($openfile);
if(!fread($openfile, 'r')){
$lastart=0;
}else{
...
?>

 

Since you terminate de file resource, fread will always return false, wich validates the first condition resseting $lastart and rewinding the while loop, ad nausea.

 

Second, I don't believe you can do this (do enlighten me if I'm wrong  ;D).

<?php
"...
<td>"."date(F d Y H:i:s., filemtime($filename)"."</td>
..."
?>

 

Try this for the echo:

 

<?php
echo "<table>
  <tr>
    <td>".date(F d Y H:i:s., filemtime($filename)."</td>
  </tr>
  <tr>
    <td>".$content."</td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>";
?>

 

As for looping through all the numbered files, I belive you only need to increment $lastart right after the echo (inside the if) by one ( $lastart++;)

Remember to move $lastart=1 out of the while loop though, else you'd return to file 1.txt eternally.

 

Hope it helps, cheers.

alright so i tried it like this

<?php  
$lastart=1;

while($lastart!=0){

$filename="$lastart".".txt";
$openfile=fopen($filename, "r") or die('error1');
$content=fread($openfile, filesize($filename)) or die ('error2');

echo "$lastart";
if(!fread($openfile, filesize($filename))){
	$lastart=0;

	}else{
echo "
<table>
  <tr>
    <td>".date(F d Y H:i:s., filemtime($filename)."</td>
  </tr>
  <tr>
    <td>".$content."</td>
  </tr>
  <tr>
    <td>written by eric wooley</td>
  </tr>
</table>";
$lastart=$lastart+1;
	}
	}
?>

but i got the same result :(

I figured it out, this is working (although a bit ugly, can be optimized I guess).

 

<?php
$lastart = 1;

while ($lastart != 0){
    $filename = $lastart.'.txt';
    if (is_readable($filename)){
        $openfile = fopen($filename, 'r') or die('error1');
        $content = fread($openfile, filesize($filename)) or die ('error2');
        $date = date('F d Y H:i:s.', filemtime($filename));
    }
    else{
        $content =null;
    }
    
    if (!$content){
        $lastart = 0;
    }
    else{
        echo '<table>
              <tr>
                <td>'.$date.'</td>
              </tr>
              <tr>
                <td>'.$content.'</td>
              </tr>
              <tr>
                <td>written by eric wooley</td>
              </tr>
            </table>';
            $lastart++;
    }
}
?>

 

Note that I'm not a double quote fan  ::)

Cheers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.