TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   self submited form problem, sorting, cleaning and array... (http://www.talkphp.com/advanced-php-programming/3583-self-submited-form-problem-sorting-cleaning-array.html)

Peuplarchie 11-10-2008 01:41 AM

self submited form problem, sorting, cleaning and array...
 
Good day to you all,

Here I am again with another question regarding a listing and creating file.
This time I would like to 4 things;

- Clean my code, I clean the list and display only the txt file, but I do it 2 time same for where I display it, I'm sure there is a better way..?

- Sort last added message on top.

- When I add text, the page does not refresh and show what I just enter, I have to add another text to see it, on and on...

- Could I get the result of the listing into an array ?


Here is my code:

PHP Code:


<?php

//Receive post 
if(isset($_POST['Submit'])){

// list only txt file 
$extensions = array('txt');
$thelist "";
 if (
$handle opendir('.')) {
   while (
false !== ($file readdir($handle)))
      {
          
$ext strtolower(end(explode('.'$file)));
       
          if (
in_array($ext$extensions) AND $file != "." AND $file != "..")
      {
              
$thelist .= '<table border=\"1\" align=\"center\"><tr><td width=\"500\"><a href="'.$file.'">'.$file.'</a></td></tr>';
              
$contents file($file);
              
$string implode($contents);
              
$thelist .= '<tr><td>'.$string.'</td></tr></table><br/>';
            
          }
       }
  
closedir($handle);
  }


//Show the form
echo "<form action=\"\" method=\"post\">";
echo 
"<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
echo 
"</textarea>";
echo 
"<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>"
;

//Show the list
echo "<P>List of files:</p>";
echo 
"<P>".$thelist."</p>";





// Declare Variables
$text $_POST['update'];
$your_data $text;



// Open the file and erase the contents if any
$fp fopen(date('Y')."-".date('m')."-".date('d')."-".date('G')."-".date('i')."-".date('s').".txt""w");

// Write the data to the file
fwrite($fp$your_data);

// Close the file
fclose($fp);

// Confirm saved !
echo "File updated.<br />"




//else, if you are not receiving a post...
}else{


// list only txt file 
$extensions = array('txt''ou');
$thelist "";
 if (
$handle opendir('.')) {
   while (
false !== ($file readdir($handle)))
      {
          
$ext strtolower(end(explode('.'$file)));
       
          if (
in_array($ext$extensions) AND $file != "." AND $file != "..")
      {
              
$thelist .= '<table border=\"1\" align=\"center\"><tr><td width=\"500\"><a href="'.$file.'">'.$file.'</a></td></tr>';
              
$contents file($file);
              
$string implode($contents);
              
$thelist .= '<tr><td>'.$string.'</td></tr></table><br/>';
            
          }
       }
  
closedir($handle);
  }


//Show the form
echo "<form action=\"\" method=\"post\">";
echo 
"<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
echo 
"</textarea>";
echo 
"<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>"
;

//Show the list
echo "<P>List of files:</p>";
echo 
"<P>".$thelist."</p>";



}
?>


Thanks for your time !

Runar 11-10-2008 12:13 PM

Hello,

Quote:

Originally Posted by Peuplarchie (Post 19482)
- Clean my code, I clean the list and display only the txt file, but I do it 2 time same for where I display it, I'm sure there is a better way..?

I removed the else part and moved the code to open the folder and display all files till after the fwrite() part. The code to display the files is not repeated anymore.


Quote:

Originally Posted by Peuplarchie (Post 19482)
- Sort last added message on top.

Done, using krsort() and storing the files in an array.


Quote:

Originally Posted by Peuplarchie (Post 19482)
- When I add text, the page does not refresh and show what I just enter, I have to add another text to see it, on and on...

Done, by having the code to write to the file before we open the folder and display the files.


Quote:

Originally Posted by Peuplarchie (Post 19482)
- Could I get the result of the listing into an array ?

Done.

If I were you, I would consider using some CSS to clean up the code further. I am using inline CSS, but you should move it all to an external CSS file. Let me know if you are having any trouble with this code!

PHP Code:

<?php

// Turn on error reporting
ini_set'display_errors''yes' );

// Is submit button pressed?
if( isset( $_POST['submit'] ) )
{
    
// Store new content in a variable
    
$new_content $_POST['update'];

    
// Array containing all files
    
$file_list = array();

    
// Filename on file we are writing to
    
$filename date'Y' ) . '-' date'm' ) . '-' date'd' ) . '-' date'G' ) . '-' date'i' ) . '-' date's' ) . '.txt';

    
// Open the file and erase the contents if any
    
$content_file fopen$filename'w' );

    
// Write the data to the file
    
fwrite$content_file$new_content );

    
// Close the file
    
fclose$content_file );

    
// Confirm the file was updated
    
echo '<h2>File successfully updated!</h2>';
}
    
// Set path to our directory (for easier use)
// Remember the last slash (/)!
$directory_path './';

// Return .txt files found in the directory
foreach( glob$directory_path '*.txt' ) as $file )
{
    
// Put some file data in variables
    
$name    basename$file );
    
$size    filesize$file );
    
$content implodefile$file ) );
    
    
// Generate file stats
    
$stats   stat$file );
    
    
// Last time the file was modificated
    
$mod_time $stats['mtime'];

    
// Do your thing, for example add the info to an array
    // or output it directly                    
    
$file_list[$mod_time]['filename'] = $name;
    
$file_list[$mod_time]['filesize'] = $size;
    
$file_list[$mod_time]['content']  = $content;
    
$file_list[$mod_time]['mod_time'] = $mod_time;
    
    
// Sort array by modification time (key) in reverse order
    
krsort$file_list );
}

// Display the form
?>
    <form action="/YOUR FILE.php" method="post">
    <label for="update">File content:</label>
    <textarea name="update" id="update" cols="50" rows="10"></textarea>
    
    <button type="submit" name="submit">Update</button>
    </form>

    <h2>List of files:</h2>
    
    <table style="border: 1px solid #555; border-collapse: separate; font-size: 12px; width: 800px">
    <tr>
        <th>Filename:</th>
        <th>Filesize:</th>
        <th>Content:</th>
        <th>Last modified:</th>
        </tr>
<?php

// Return all files from array
// Since the array contains multiple arrays, we must
// use i.e. $files['name'] to access the data
foreach( $file_list as $files )
{
    echo 
'<tr>
            <td style="border: 1px solid #555; padding: 4px; text-align: center; width: 200px;"><a href="' 
$files['filename'] . '">' $files['filename'] . '</a></td>
            <td style="border: 1px solid #555; padding: 4px; text-align: center; width: 60px;">' 
$files['filesize'] . ' kb</td>
            <td style="border: 1px solid #555; padding: 4px; ">' 
nl2br$files['content'] ) . '</td>
            <td style="border: 1px solid #555; padding: 4px; text-align: center; width: 120px">' 
date'Y/m/d - H:i:s'$files['mod_time'] ) . '</td>
        </tr>'
;
}

echo 
'</table>';

?>

I have tested the code myself, and it seems to work perfectly.


Yours, Runar

Peuplarchie 11-11-2008 03:37 AM

I tried out your code, wow it look very nice, thanks, but it's not what I'm looking for. You fixed my code, ho yah, it is now fast and do not repeate within it self. Now, when the page reload after submit, it load another page, it should just refresh cotent and add content just added. The key here is a user comment code.

Runar 11-11-2008 12:08 PM

Well, that is not possible with PHP. You will have to use AJAX or something if you want the page to refresh the content without reloading itself.


All times are GMT. The time now is 04:13 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0