SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    formatting text retrieved from <textarea>

    I have a form that writes information to a .txt file. Then I have a webpage that retrieves the data from the.txt file and displays it in tables.
    With the <textarea> of the form, is a user presses enter then all info after the enter will appear in the next table. This is really stuffing up the format of my page.

    So, What I need to do is have each word in the text area display on a seperate line in a table cell no matter how the user enters it into the textarea box. Is there a way to do this?

    any help is greatly appreciated
    coiL
    "cradled in the learning curve"

  2. #2
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    coiL,

    Here is one solution using php's explode() function:

    PHP Code:
    <?php
     
    if ( $action == "submit" ){
       
    $wordArray explode(" ",$textfield);
       for ( 
    $n 0$n sizeof($wordArray); $n++){
         print 
    $wordArray[$n]."<br>";
       } 
     } else {
    ?>
       <form action="test.php" method="get">
       <textarea name="textfield"></textarea>
       <input type="hidden" name="action" value="submit">
       <input type="submit" value="submit">
       </form>
    <?   
     
    }
    ?>
    Hope this helps,

    Chris Carlson
    totaldeveloper.com

  3. #3
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply , but I am very new to php (started yesterday) and don't really understand where I put that code. Also do I replce test.php with the php script that writes the data to the text file?
    coiL
    "cradled in the learning curve"

  4. #4
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, that test.php was the name of the script that i made to test my theory. (I am new to php, as well, and I think that it is a good idea for me to test my ideas before giving them to people to make sure they work. )

    the main part of my code you should look at is this:

    PHP Code:
       $wordArray explode(" ",$textfield);
       for ( 
    $n 0$n sizeof($wordArray); $n++){
         print 
    $wordArray[$n]."<br>";
       } 
    Let me explain what this is doing.

    The explode function takes a string (in this case I told it to take the text entered in the box named $textfield) and break it up into an array by chunks seperated by the given seperator (in this case the " " space character).

    So once we execute this line:

    PHP Code:
    $wordArray explode(" ",$textfield); 
    $wordArray is an array where each member of that array is a different word.

    Then, I use that for loop to step through the array printing each word out on to the web page the user is viewing.

    Can you post your code online? I can insert what you need into it. What you will end up doing is adding a '\n' to each portion of the array and then writing that line to the file.

    Wow, after writing this I realized how confusing I really am. lol

    Chris

  5. #5
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that does help explain it a bit

    I wasn't sure which code you wanted to see, so I am posting both.

    html form:
    PHP Code:
    <form action="writeteams.php" method="post">
    <
    table border="0">
    <
    tr>
     <
    td>Team Name</td>
     <
    td align="left"><input type="text" name="name" size="30" maxlength="30"></td>
    </
    tr>
    <
    tr>
     <
    td>leader</td>
     <
    td align="left"><input type="text" name="leader" size="30" maxlength="30"></td>
    </
    tr>
    <
    tr>
     <
    td>email</td>
     <
    td align="left"><input type="text" name="email" size="30" maxlength="30"></td>
    </
    tr>
    <
    tr>
     <
    td>members</td>
     <
    td align="left"><textarea name="members" cols="30" rows="6"></textarea></td>
    </
    tr>
    </
    table>
    </
    form
    Writeteams.php
    PHP Code:
    <?php

        
    $fp fopen ("teams.txt""a"1);  //open the teams.txt file ready for writing

        
    flock ($fp2); //lock file for writing (only one user can write at a time)

        
    if (!$fp)  //If the file can't be opened, display error message
        
    {
        echo 
    "Your team could not be added at this time. Please Try again later.";
        exit;
        }


        
    $outputstring $name":" $leader ":" $email ":" .  $members "\n"
        
    fwrite($fp$outputstring);  
        
    flock ($fp3); //release file lock
        
    fclose($fp);

    ?>
    and I have to say, I'm not sure that I did those parts right either - though it does work apart from the format problem.
    coiL
    "cradled in the learning curve"

  6. #6
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that I take a closer look at the problem, I realize that it is a little more complicated than I originally thought.

    Most sites instruct a user to "seperate teams with a comma in the box below" instead of allowing for the user to enter the data in the box however they choose.

    A much more complicated function is required to process both space seperated entries and entries seperated by new line characters.

    Or so I think.

  7. #7
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, I was actually just thinking that the comma way might be a good idea before I read your post
    I think I might end up doing that and hope that the users are good at following instructions

    it seems that no one knows how to help me.

    thanks for your replies totaldeveloper
    coiL
    "cradled in the learning curve"

  8. #8
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show me your HTML Code that displays all the entries from your textfile. You see, IMHO youre making yourself problems which shouldn't be there in the first place.
    Im sure there is a way so solve this in the HTML Code itself, at least we can try it

  9. #9
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sure

    PHP Code:
    echo ("<table border=1 width=60% bordercolor=000000 cellpadding=5 cellspacing=0>
    <tr valign=top> <td class=colone>Team Name</td>" 
    "<td class=coltwo>" $team "</td></tr>" .
    "<tr valign=top> <td class=colone>Team Leader</td>" 
    "<td class=coltwo>" "<a href=mailto:"$email ">" $leader "</td></tr>" .
    "<tr valign=top> <td class=colone>Members</td>" 
    "<td class=coltwo>" $members "</td></tr>" ."</table><br />"); 
    Last edited by coiL; Aug 1, 2002 at 09:45.
    coiL
    "cradled in the learning curve"

  10. #10
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the code you have posted there, you can request that people who enter in the teams hit enter after each name.

    Then you can simply use the nl2br() function call around the $members variable.

  11. #11
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have actually tried using nl2br() but it didn't seem to help. Maybe I was putting it in the wrong place. Where would you suggest I put it?
    coiL
    "cradled in the learning curve"

  12. #12
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe all you have to do earlier in the script is put:

    PHP Code:
    $members nl2br($members); 
    But, I have been wrong before. LOL

    Chris

  13. #13
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, nl2br() will only work if the people hit enter after each word they enter in the textarea box.

    I guess what you could do is this
    PHP Code:
    <table border=1 width=60% bordercolor=000000 cellpadding=5 cellspacing=0>
    <tr valign=top> <td class=colone>Team Name</td>
    <td class=coltwo><?=$team?></td></tr>
    <tr valign=top><td class=colone>Team Leader</td>
    <td class=coltwo><a href=mailto:"<?=$email?>">"<?$leader?></td></tr>
    <tr valign=top> <td class=colone>Members</td>
    <td class=coltwo>

    <?php
    $wordArray 
    explode(" "$members);
       for ( 
    $n 0$n sizeof($wordArray); $n++){
         print 
    $wordArray[$n]."<br>";
       }
    ?> 
    </td></tr></table><br />
    Ok, i haven't tested this, but it should work, and one can assume that people will logically divide member names using a space. It may not be the most efficient solution, but i guess it should do the job for your needs.
    Let me know if this worked out or not.

  14. #14
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, this is the method I was talking about in my initial responses. Either you have to mandate that your users enter spaces inbetween names or hit return. If you want more options for your users, then I believe you will need a much more complicated function. With the space route...you do run into problems if team names are composed of more than one word. Just a thought.

    Chris

  15. #15
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do I put all of that in the echo() part?
    coiL
    "cradled in the learning curve"

  16. #16
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for you help guys (or girls). I thik I will just leave it so that users must seperate names by commas.

    I will tackle the more complicated stuff when I have learnt a bit more php

    thanks again for your help.
    coiL
    "cradled in the learning curve"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •