self submited form problem, sorting, cleaning and array...

Discussion in 'Programming' started by Peuplarchie, Nov 9, 2008.

  1. #1
    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
    
    //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>";
    
    
    
    }
    ?>
    
    
    
    PHP:

    Thanks for your time !
     
    Peuplarchie, Nov 9, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    Question is not clear enough and too muh code has been posted actually, that might be the reason thread remained unreplied :). Please tell me exactly what you are trying to do, and what is the problem.

    Besides where filename is created, you can use one call to 'date' function rather than 6

    fopen(  date("Y-m-d-G-i-s",time()) . ".txt", "w");
    Code (markup):
    regards
     
    Vooler, Nov 10, 2008 IP
  3. Peuplarchie

    Peuplarchie Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    this code will be used as a user comments.

    That is why I need a self-submit form, one page.
    I had one but spam just got trough it, and I want to make a better one with of mine in it.
     
    Peuplarchie, Nov 10, 2008 IP