PHP form to Flat File

Discussion in 'PHP' started by le007, Jun 5, 2008.

  1. #1
    Hi all,

    I'm working with a simple php flat file here - can't use mySql at work....

    Anyway, I am half asleep here, but have toyed around with it for the past few minutes. Can someone please show me how to write/append and read a table of info into a text file?

    EG

    table
    row 1 Data1 Data2
    row 2 Data2 Data2
    row 3 Data3 Data3

    I need the data to be represented in a viewing php page in <td> tags.....

    As I say I'm extremely tired so I hope I made sense..

    I'm trying to allow a user to update a table when necessary using a form with various <input> fields..

    Thanks all - anyone need CSS help, I'm your man! :cool:
     
    le007, Jun 5, 2008 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    just write the results from the form like this

    $fileContent = $_POST['field1'] . "%#%" . $_POST['field2'] . "%#%" . $_POST['field3'];

    and so on till you have 1 string with all your data seperated by a unique seperator

    then write the file by saying

    file_put_contents("filename.txt",$fileContent);

    you can make a unique name for each file with, for example, their first name?

    then, when loading the files with php, just use the explode function, and explode at each %#% or whatever you seperated your data with, and your form data will be in an array.

    tada!
     
    crath, Jun 5, 2008 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Heh crath,

    thanks for the reply buddy, appreciate that. I have tried this and its working nicely..... however I have another quick question. How can I have it that if status = BOOKED that the font for status ONLY is in red? How can I put an if statement for that in here somehow?

    <?php
      $saving = $_REQUEST['saving'];
      if ($saving == 1) { 
        $data = '<table width=300 bgcolor=silver><tr><td>' . $_POST['datefrom1'] .'</td><td>'. $_POST['dateto1'] .'</td><td>'. $_POST['status1'] .'</td><td>'. 
    
    $_POST['price1'] . '</td></tr>'
     . '<tr><td>' . $_POST['datefrom2'] .'</td><td>'. $_POST['dateto2'] .'</td><td>'. $_POST['status2'] .'</td><td>'. $_POST['price2'] . '</td></tr>'
     . '<tr><td>' . $_POST['datefrom3'] .'</td><td>'. $_POST['dateto3'] .'</td><td>'. $_POST['status3'] .'</td><td>'. $_POST['price3'] . '</td></tr>'
     . '<tr><td>' . $_POST['datefrom4'] .'</td><td>'. $_POST['dateto4'] .'</td><td>'. $_POST['status4'] .'</td><td>'. $_POST['price4'] . '</td></tr>'
     . '<tr><td>' . $_POST['datefrom5'] .'</td><td>'. $_POST['dateto5'] .'</td><td>'. $_POST['status5'] .'</td><td>'. $_POST['price5'] . '</td></tr></table>';
    	$file = "test.txt"; 
     
        $fp = fopen($file, "w") or die("Couldn't open $file for writing!"); 
        fwrite($fp, $data) or die("Couldn't write values to file!"); 
     
        fclose($fp); 
        echo "Saved to $file successfully!";
      }
    ?>
    
    PHP:
     
    le007, Jun 6, 2008 IP
  4. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #4
    Add the code below under "if ($saving == 1) {"
    for($i=0;$i<5;$i++){
    	if($_POST['status'.$i]=="booked"){
    		$booked[$i] = "style='color:red';
    	} else {
    		$booked[$i] = "";
    	}
    }
    PHP:
    Then just replace each
    '<tr><td>' . $_POST['datefrom#']
    PHP:
    with
    "<tr><td $booked[#]>" . $_POST['datefrom#']
    PHP:
    and in that part, besure to replace each # with that number (eg 1-5, whichever row your on)
     
    crath, Jun 6, 2008 IP
  5. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Excellent, thanks again.
     
    le007, Jun 6, 2008 IP
  6. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    Actually...... I tried this:

    
    <?php 
    
    $saving = $_REQUEST['saving']; 
    if ($saving == 1) {     
    
    for($i=0;$i<5;$i++){
        if($_POST['status'.$i]=="booked"){
            $booked[$i] = "style='color:red';
        } else {
            $booked[$i] = "";
        }
    } 
    $data = "<table width=300 bgcolor=silver><tr><td $booked[1]>" . $_POST['datefrom1'] .'</td><td>'. $_POST['dateto1'] .'</td><td>'. color($_POST['status1']) 
    
    .'</td><td>'. $_POST['price1'] . '</td></tr>' . '<tr><td>' . $_POST['datefrom2'] .'</td><td>'. $_POST['dateto2'] .'</td><td>'. $_POST['status2'] 
    
    .'</td><td>'. $_POST['price2'] . '</td></tr>' . '<tr><td>' . $_POST['datefrom3'] .'</td><td>'. $_POST['dateto3'] .'</td><td>'. $_POST['status3'] 
    
    .'</td><td>'. $_POST['price3'] . '</td></tr>' . '<tr><td>' . $_POST['datefrom4'] .'</td><td>'. $_POST['dateto4'] .'</td><td>'. $_POST['status4'] 
    
    .'</td><td>'. $_POST['price4'] . '</td></tr>' . '<tr><td>' . $_POST['datefrom5'] .'</td><td>'. $_POST['dateto5'] .'</td><td>'. $_POST['status5'] 
    
    .'</td><td>'. $_POST['price5'] . '</td></tr></table>';   
     $file = "test.txt";     
     $fp = fopen($file, "w") or die("Couldn't open $file for writing!");   
     fwrite($fp, $data) or die("Couldn't write values to file!");     
     fclose($fp);     echo "Saved to $file successfully!"; 
    }
    
    ?>
    PHP:
    but it didn't work...... sorry buddy I need to brush up my php, haven't used it in so long.....
     
    le007, Jun 6, 2008 IP
  7. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Sorry I need the booked to be RED when its shown on the page I open the text file from?
     
    le007, Jun 6, 2008 IP