Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Multi-line values.  (Read 4019 times)

0 Members and 1 Guest are viewing this topic.

CBMatt

    Topic Starter
  • Mod & Malware Specialist


  • Prodigy

  • Sad and lonely...and loving every minute of it.
  • Thanked: 167
    • Yes
  • Experience: Experienced
  • OS: Windows 7
Multi-line values.
« on: April 23, 2008, 05:35:38 AM »
I'm really starting to get the hang of PHP, but I would still only consider myself intermediate at best.  My fiancé and I are looking at different houses right now and I thought I would make a simple little web site to help us keep track of the different properties we're considering.  Basically, I just want a page that will display the different houses (and their related info) in tables.  I could easily do this with HTML, but instead of having to code a bunch of tables, I thought it would be easier to simply create a form and use some PHP.  For the most part, this is a lot easier, but I've encountered one small problem...if I enter more than one line of data into my Notes textarea, it causes the info to be broken up into multiple tables, and any data entered afterwards is displayed incorrectly.  Take a look here for an example.

At the moment, I have three pages...
1. addhouse.html contains a form that allows me to enter whatever info I want.
2. addhouse.php stores the form info into a textfile (houselist.txt).  More functionality will be added later.
3. showhouses.php retrieves the info from houselist.txt and displays it in tables.

If I only enter one line in the textarea, everything works perfectly.  I can make the info display on multliple lines if I manually insert line breaks when entering the data, but I would like to avoid that.  When I press the Enter key, is there any way to make that translate into a /r/n line break?

To avoid major side-scrolling, I attached all of my files in a zip.  And if it helps at all, I also attached what is currently in my houselist.txt file.  It holds the information that is on the example page I linked to earlier.  It should all be one line, not two...

I know it would probably be better to enter everything into a MySQL database instead (and it might be easier to work with), but I would rather use this method because it's a lot easier to manually change data if necessary.

[recovering space - attachment deleted by admin]
« Last Edit: April 23, 2008, 05:47:33 AM by CBMatt »
Quote
An undefined problem has an infinite number of solutions.
—Robert A. Humphrey

michaewlewis



    Intermediate
  • Thanked: 26
    • Yes
    • Yes
  • Experience: Expert
  • OS: Unknown
Re: Multi-line values.
« Reply #1 on: April 23, 2008, 02:41:57 PM »
I think all you need to do is convert your \n\l to

http://us2.php.net/manual/en/function.nl2br.php

CBMatt

    Topic Starter
  • Mod & Malware Specialist


  • Prodigy

  • Sad and lonely...and loving every minute of it.
  • Thanked: 167
    • Yes
  • Experience: Experienced
  • OS: Windows 7
Re: Multi-line values.
« Reply #2 on: April 23, 2008, 05:26:20 PM »
Thanks for the link.  Some of these functions sound like they do what I need them to do, but so far, none of them are doing it quite right.  I'll have to toy around with some of these for a little bit.
Quote
An undefined problem has an infinite number of solutions.
—Robert A. Humphrey

Astoria



    Intermediate

    Re: Multi-line values.
    « Reply #3 on: April 24, 2008, 02:16:03 PM »
    you could also try:

    Code: [Select]
    $notes=str_replace("\n\r", "<br />", $notes);

    I know you don't want to use a MySQL dB because you said it was easier to modify data manually, I disagree.

    If you use XAMPP or WAMPP for you localhost server, I can help you do this with MySQL.

    « Last Edit: April 24, 2008, 02:26:17 PM by Astoria »



    michaewlewis



      Intermediate
    • Thanked: 26
      • Yes
      • Yes
    • Experience: Expert
    • OS: Unknown
    Re: Multi-line values.
    « Reply #4 on: April 24, 2008, 03:35:26 PM »
    I know you don't want to use a MySQL dB because you said it was easier to modify data manually, I disagree.

    If you use XAMPP or WAMPP for you localhost server, I can help you do this with MySQL.

    But what if you don't know how to code sql with php? :P

    you could also try:

    Code: [Select]
    $notes=str_replace("\n\r", "<br />", $notes);
    Correct me if I'm wrong, but doesn't that do the exact same thing as nl2br?

    Astoria



      Intermediate

      Re: Multi-line values.
      « Reply #5 on: April 24, 2008, 03:48:03 PM »
      But what if you don't know how to code sql with php? :P

      And that's why I offered to help him with that  :)
      In my experience the PHP/MySQL combo is so much easier than messing with text files.


      Correct me if I'm wrong, but doesn't that do the exact same thing as nl2br?

      Yes my bad. I should've checked it. I didn't know they upgraded it from
       to
      .
      However, what nl2br does is, replace "\n" with "
      ", it doesn't replace "\n\r" with "
      ".
      But nl2br should work fine for him.



      CBMatt

        Topic Starter
      • Mod & Malware Specialist


      • Prodigy

      • Sad and lonely...and loving every minute of it.
      • Thanked: 167
        • Yes
      • Experience: Experienced
      • OS: Windows 7
      Re: Multi-line values.
      « Reply #6 on: April 25, 2008, 05:59:14 AM »
      For me personally, the text file is easier.  Maybe it's just me, but if I want to make a change to a couple of properties (i.e. change the address of one and the price of another), it would be quicker and easier to simply open up the text file and make the change super quick.  Coding the SQL commands or accessing the database through PuTTY takes too long for it to be worth the effort, especially when I don't plan on making too many changes.

      Now, if I had a large table with hundreds of arrays, then I agree that the suggested methods would be more efficient.  Just in case it ever comes down to that, I've set up a primary key, but I don't expect this list to ever surpass 100 entries/arrays.  Another thing I should note is that my site is currently residing in my section of the school server.  Once the semester is over, I probably won't have access to it anymore, so the text files will make it easier for me if/when I transfer the site to a different server.  I'm sure there's probably a way to do this via SQL, but something like that is beyond anything I've learned so far.

      Oh, and Astoria, your suggestion does exactly what I want.  Well, I had to change the "\n\r" to "\r\n", but other than that, it works perfectly.  It seems so simple that I'm surprised I didn't figure it out for myself.  I was just looking at it differently, I suppose.  Anyway, thanks a bunch, you just made my form a lot friendlier.


      Also...
      However, what nl2br does is, replace "\n" with "
      ", it doesn't replace "\n\r" with "
      ".
      But nl2br should work fine for him.
      You are correct that there is a slight difference.  This difference is what makes nl2br not work for me.  I believe it may be because I'm working with a server running on a Windows OS rather than a Linux OS (don't quote me on this).  All I know is I need to use "\r\n" for a new line.  So, when I use nl2br, instead of getting this result:

      1
      2
      3

      I get this result:

      1

      2

      3

      It converts the \n to a
      , but leaves the \r as is.  At least, that's how it appears to me.  But I've got a solution now, so that's all that really matters to me.  Heh.
      « Last Edit: April 25, 2008, 06:21:33 AM by CBMatt »
      Quote
      An undefined problem has an infinite number of solutions.
      —Robert A. Humphrey

      michaewlewis



        Intermediate
      • Thanked: 26
        • Yes
        • Yes
      • Experience: Expert
      • OS: Unknown
      Re: Multi-line values.
      « Reply #7 on: April 25, 2008, 09:18:44 AM »
      Quote
      because I'm working with a server running on a Windows OS rather than a Linux OS

      that could very well be the reason nl2br didn't work. linux does have a different return code than windows.

      CBMatt

        Topic Starter
      • Mod & Malware Specialist


      • Prodigy

      • Sad and lonely...and loving every minute of it.
      • Thanked: 167
        • Yes
      • Experience: Experienced
      • OS: Windows 7
      Re: Multi-line values.
      « Reply #8 on: April 26, 2008, 04:55:20 AM »
      Yeah, that's what I'm assuming the problem is.  One of the first things we learned in my class is that Windows, Linux, and Mac all have different return escapes...I just can't remember which belongs to what OS.  Heh.
      Quote
      An undefined problem has an infinite number of solutions.
      —Robert A. Humphrey