How to extract the user entered form data stored in a .txt using php to a .php page

Discussion in 'PHP' started by archik123, Oct 14, 2010.

  1. #1
    I am trying to extract the user entered form data which is stored in a .txt file to a .php page using php ...

    how do i get the tabular display of that data ?
     
    archik123, Oct 14, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    do you have a template of your text file?? kindly post some sample output of the text file
     
    bartolay13, Oct 14, 2010 IP
  3. archik123

    archik123 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the response ........


    Saving to the .txt file the user entered form data using php:

    <?php

    $fp = fopen("formda.txt" ,"a");

    $savestring = $Fname . "||" .$Mname . "||" .$Lname . "||" .$addr . "||" .$cit. "||" .$stat . "||" .$zip. "||" .$home. "||" .$cell. "||" .$em."||".$gender."||".$dob_month."||".$dob_day."||".$dob_year."||".$mcond."||".$Experience. "||" .$category ."\n" ;
    fwrite($fp,$savestring);
    fclose($fp);



    ?>

    Extracting out ..using php:

    <?php
    foreach (glob("formda.txt") as $filename) {
    $file = $filename;
    $contents = file($file);

    $string = implode($contents);

    $str1=explode("||",$string);


    foreach ($str1 as $str2) {

    echo nl2br(" $str2 &nbsp;&nbsp; ");


    }
    ?>

    output displayed is :

    lkmk lmkl mkl mk mlm klm lkm lkm lmlk mkl female January 9 2004 ljnbjknjlnln Expert Teen


    Now how do i get the above display in a tabular format with the headings as Fname ,Mname etc ..(total of 14 headings) ..

    what changes do i need to make in my code for the tabular display...Do let me know..
     
    archik123, Oct 14, 2010 IP
  4. ZachF

    ZachF Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    A CSS Div or an HTML Table would do the trick. Type HTML Table generator in on google to make a bunch of columns.
     
    ZachF, Oct 14, 2010 IP
  5. learnerabn

    learnerabn Peon

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Simple create a table before starting the php code.
    After that give for loop and echo the values in the corresponding cells..
    got it?...
     
    learnerabn, Oct 15, 2010 IP
  6. Gediminas

    Gediminas Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    <?php
        $cont=file_get_contents("formda.txt");
        $data=explode("||",$cont);
    ?>
    <table>
    <tr><td>First name:</td><td><?php echo $data[0]; ?></td></tr>
    <tr><td>Middle name:</td><td><?php echo $data[1]; ?></td></tr>
    <?php //and so on... ?>
    </table>
    Code (markup):
    I think everything is very clear here... Enjoy
     
    Gediminas, Oct 15, 2010 IP
  7. archik123

    archik123 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for the response ..

    I have tried in the above mentioned way and it was working ..

    I have a total of 17 cols to display ..
    But in the display of the last column data .. ( ie. My last column is Category : Teen ( data of the Category column) ..

    In the display it is showing up as : Category :Teen lucy ..where Lucy is the data of the first column of the next row ..

    So now what should i do to have my proper display as : Category :Teen ...


    2) As we do not have count on the number of users entering the data in the form .. So can we use echo $data instead of explicitly giving echo $data[0] and so on .. ?

    i.e. can we embed echo $data in a for loop and what should the condition be ? (as we would not know in advance the number of records) ..


    do let me know ........
     
    archik123, Oct 15, 2010 IP
  8. archik123

    archik123 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The code metioned above is working for 1 record display ..But how to work on if i have 'n'number of records ..

    i.e. if i give echo $data[0] .....( it extracts the first column data of the first record ) .But how do i access the data of the second record and so on..

    should a for loop be used ? If so where to be used ?
     
    archik123, Oct 15, 2010 IP
  9. Gediminas

    Gediminas Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I suggest you to display one entry in one row, not in one column. Just less complicated code.
    Firstly, using loop read file line-by-line, you can use fgets to do it. And then exploded the line you just read.
    Yeah I could write all the code, but it will be more useful for you to write by yourself. Also, I highly recommend you to learn MySQL. It makes storing data a lot easier.
     
    Gediminas, Oct 16, 2010 IP
  10. archik123

    archik123 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yeah ..i tried it in the other way ..and i am able to get the display ...Thanks ..for the response ..
     
    archik123, Oct 16, 2010 IP