SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say someone put in the textarea a list, and was separated by pushing the Enter key, or by commas, how do I separate those lists when processing through PHP? When I process it, it just goes in as one big chunk.

    Also, when I pull it out again for editing, the format is lost. So even if I made a nice list with each item on new line, when I pull it back out of the MySQL database, it displays as one big blob.

    When I basically wanna do is when someone enters an item in a new line, it gets reconize each line as a separate entry, and when it comes to editing, it pulls those entry back out and put them back in a text area, when each lines retained, not one big blob.

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you look at the HTML code of the output page, you should see that the formatting was indeed preserved -- it just requires newlines to be replaced with <BR> tags to look right in a Web browser!

    To do this, call the nl2br() function before outputting the variable:

    $var = nl2br($var); // Convert line breaks to <BR> tags
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nop...don't see any linebreaks, <br> tags in the source...but just tried it, and the format was still there, the source simply put each line on a new line, and the browser displayed it correctly...doesn't work all the time though.

    And how can I get it to know that each new line is a new entry, and belongs in a diferent row of the db?

  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For a list separated by newlines, you can break the string into an array with:

    $arrayName = explode("\n",$stringName);

    And for comma-delimited lists:

    $arrayName = explode(",",$stringName);
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  5. #5
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But this is the strange part...I push enter for new lines, and it shows up fine, but when I view the source, there isn't anything that causes it to be in different lines. The source simply has each line on a new line, no /n, no <br>, yet it shows up fine in the text area.

    Strange...and still have to figue out a way to solve it.

  6. #6
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, the text between <TEXTAREA> and </TEXTAREA> is formatted as typed (whitespace is not collapsed).

    I've lost track... what problem are you trying to solve?
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  7. #7
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Find a way to tell the script that each line is a new entry. Say someone typed:

    Item 1
    Item 2
    Item 3

    I want the script to put those three things into three different rows. Since there isn't any \n or <br> in between, I can't use explode().

    Hope I made that clear...

  8. #8
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There should indeed be a \n between each entry in the string variable that is produced upon submission of the form.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  9. #9
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try the following example:

    Code:
    <HTML>
    <BODY>
    <? if ($submit == ""): ?>
    <FORM ACTION="<?=$PHP_SELF?>" METHOD=POST>
    <TEXTAREA NAME="textarea"></TEXTAREA>
    <INPUT TYPE=SUBMIT NAME=submit VALUE="Submit">
    </FORM>
    <? else: ?>
    Submitted value:<BR>
    <PRE><?=$textarea?></PRE>
    <?
    $textarray = explode("\n",$textarea);
    
    for ($i=0;$i<count($textarray);$i++) {
    	echo("Item $i: $textarray[$i]<br>");
    }
    ?>
    <? endif; ?>
    </BODY>
    </HTML>
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  10. #10
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks, it does work! Sorry for the long bother.


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
  •