tsteuwer Posted March 19, 2010 Share Posted March 19, 2010 Hello all. I've been dabbling in PHP for a few years but never got that deep. Until now, I've never really built a system that interacts PHP with MySQL. I've been working on a new website and I am having issues generating output that looks exactly like how it was inputed. Here's how the data looks when I input it: "Some random text. More random text, followed by more random text. Also, we'll add more random text." When I insert this data into my MySQL database, it looks exactly like the above. However, when I get the data from MySQL and echo it via PHP it looks like the following: "Some random text. More random text, followed by more random text. Also, we'll add more random text." It basically removes all formatting. I find this really odd because when I log into the MySQL Admin, and actually open up the data, it looks like the first example. What am I doing wrong when outputting? You can see an example of the output. Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/ Share on other sites More sharing options...
Zane Posted March 19, 2010 Share Posted March 19, 2010 look at http://php.net/nl2br Also, how are you outputting it right now? I assume with just a simple echo, but what's your code. Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028382 Share on other sites More sharing options...
tsteuwer Posted March 19, 2010 Author Share Posted March 19, 2010 When a user submits data via a form, I clean it and then insert into the database with: $description = cleanData($_POST['description']); function cleanData($originalvariable) { $newvariable = trim(strip_tags(addslashes(htmlspecialchars($description)))); return $newvariable; } Then, when I'm displaying it via echo, I use: echo decodeData($description); function decodeData($originalvariable) { $newvariable = htmlspecialchars_decode(stripslashes($originalvariable)); return $newvariable; } Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028385 Share on other sites More sharing options...
tsteuwer Posted March 19, 2010 Author Share Posted March 19, 2010 By the way, I tried using echo nl2br($description); And it looks the exact same way as if I didn't use it. Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028387 Share on other sites More sharing options...
Zane Posted March 19, 2010 Share Posted March 19, 2010 instead of using your custom CleanData function.. try the built-in cleaning function http://php.net/mysql_real_escape_string Then do a normal echo and see how that does Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028389 Share on other sites More sharing options...
tsteuwer Posted March 19, 2010 Author Share Posted March 19, 2010 Hey buddy, I tried using mysql_real_escape_string and the formatting comes out the exact same. Any suggestions on how to clean data from a form to make sure I don't get attached by sql and to keep the formatting of the user in tact? And then, how would I echo it to look the same way and keep the formatting as well? Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028550 Share on other sites More sharing options...
Jax2 Posted March 19, 2010 Share Posted March 19, 2010 Can't you do a string replace from /n to <br> or something along that line? Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028584 Share on other sites More sharing options...
Zane Posted March 19, 2010 Share Posted March 19, 2010 are you even echoing this text within a pre-formatted element? For instance, the P tag. Or are you just echoing it flat out Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028586 Share on other sites More sharing options...
tsteuwer Posted March 19, 2010 Author Share Posted March 19, 2010 I'm echoing it straight out. When I look in mysql, the format is in there. I don't use any html tags because the regular user to my site won't know any html tags. Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028588 Share on other sites More sharing options...
Zane Posted March 19, 2010 Share Posted March 19, 2010 well echo it out inside a P tag and see what happens. If the formatting is there within phpmyadmin .... or mysql as most call it, then it's not impossible to say YOU can do it too. Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028590 Share on other sites More sharing options...
tsteuwer Posted March 19, 2010 Author Share Posted March 19, 2010 Hey zabus, when I do that it comes out as one paragraph. In mysql it shows three paragraphs. But I don't know how to have php output it with the three paragraphs intact. I know a regular user won't know how to type br br Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028594 Share on other sites More sharing options...
Zane Posted March 19, 2010 Share Posted March 19, 2010 Obviously, there is something stripping your output of its newlines. Which is exactly what the nl2br function is for. It take new lines and turns them into 's. I know you can't assume the user knows HTML, you've already mentioned this. Repetition isn't the answer and The answer isn't repetition. If you can't get the newlines to show, then how do you expect the user to. I'm not expecting you or telling you how to run your website or even how to program it. But before you can even begin to program a site FOR someone you need to know WHY it isn't showing the newlines. Is there any other code you're not showing us that could possibly effect the output. Since obviously, the input seems to be PERFECT. Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028596 Share on other sites More sharing options...
tsteuwer Posted March 19, 2010 Author Share Posted March 19, 2010 Zanus, I'm sorry for my redundant posts. I always feel like I'm not explaining myself correctly. Anyway, you were completely right about it nl2br(). I have no clue what I was doing prior, but I added it to the portion of where it was being echoed and it came out correct. Thank you so much for your help. I've got a lot to learn. Check it out now. Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028642 Share on other sites More sharing options...
Zane Posted March 19, 2010 Share Posted March 19, 2010 Glad you got it sorted. Remember nl2br() = New Line -> Quote Link to comment https://forums.phpfreaks.com/topic/195760-formating-form-strings-for-output/#findComment-1028663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.