PDA

View Full Version : Form entry question


-fedexer-
05-15-2008, 10:59 AM
Ok, i know how to make forms etc.

But what i am wondering is how on earth do you get php/sql to store the returns and line breaks and spaces into SQL? Currently i have to get people to type /n and then i convert that into a <br/> tag, but seeing as actual forums have forms that don't require you to do this and it still keeps the text looking like you typed it , how is it done?

I'm guessing it might be a hex code or something?

Also i have wondered what the $variable->$anothervariable

symbol means in php, i have searched it but have found nothing explaining its purpose, but i see it is used alot.

All help is appreciated.
-fedexer-

davidj
05-15-2008, 11:28 AM
use the nl2br() function in php

*** NewLine 2 Break ***

In this example you would use $newData in your database INSERT
$newData = nl2br($_POST['textfield']);

This will convert \n to <br>

davidj
05-15-2008, 11:41 AM
Also i have wondered what the $variable->$anothervariable

symbol means in PHP, i have searched it but have found nothing explaining its purpose, but i see it is used allot.

All help is appreciated.
-fedexer-

You are looking at PHP but using OOP methods normally associated with a class (group of functions in one place)

when you write PHP in a procedural styley you may have many database calls throughout your application. You have to write HTML tables to hold the results and then go on to format it. Now imagine just having 1 db call wrapped in a function which you pass in the SQL as an argument. Not only does the function return the results but could also builds a table to put them in and includes paging and navigation controls and also formats with CSS

Once you have written a class to do this you would just need to call it by...

echo database->querydb("SELECT * FROM TABLE WHERE ID='5'");

-fedexer-
05-15-2008, 02:06 PM
So when we take a new line in a form, the browser automatically adds a \n ? or does that have to manually be typed? as i am unsure how you would make php add the <br/> tags when a user doesnt type \n, or am i looking at this wrongly?

Thanks so far david , your amazing 8-)

davidj
05-15-2008, 02:11 PM
in text areas when you hit return to move to the next line the \n is added automatically although is hidden.

-fedexer-
05-15-2008, 02:30 PM
Ahh that makes sense :D And what is the \r , i have heard of it but do not recall why it appears.

davidj
05-15-2008, 03:45 PM
A carriage return is \r

A tab is \t

-fedexer-
05-15-2008, 03:57 PM
Right thanks alot David ;)

That clears alot up for me.

pete
05-15-2008, 07:20 PM
I'm not a huge fan of nl2br() because if you have a several paragraphs then you end up with

copy<br /><br />
more copy ... <br /><br />

when what you actually want is:

<p>copy</p>
<p>more copy ..</p>

so I made a better mousetrap


function nl2html($text) {
$pattern = array('#\r\n?#', '#\n\n+#', '#\n#');
$replace = array("\n", '</p><p>', '<br />');
return '<p>'.preg_replace($pattern, $replace, $text).'</p>';
}


so I used nl2html($text) which creates the proper <p> tag structure and a <br /> for single line breaks.

Here is a similar one for creating lists :


function nl2li($text) {
$pattern = array('#\r\n?+#');
$replace = array("</li>\n<li>");
$str = "<ul><li>".preg_replace($pattern, $replace, $text)."</li></ul>";
return preg_replace("(<li>\n?</li>)","",$str);
}

pete
05-15-2008, 07:24 PM
Just looked at the user comments on http://www.php.net/nl2br many people have came up with different variations of what I have done. Ideally PHP would have this function built in.

-fedexer-
05-15-2008, 07:36 PM
Nice pete, hey would you mind if i used your function in my current project?

pete
05-15-2008, 08:39 PM
Nice pete, hey would you mind if i used your function in my current project?

Help yourself:grin:

davidj
05-15-2008, 09:28 PM
think we need a toolbox function to store all these functions and givaways

something where they can be kept and searched on

Mmmm

-fedexer-
05-15-2008, 11:03 PM
Thanks Pete ;) and that would be an idea David 8-)