473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with displaying formatted text.

290 100+
I use a form to write an article and then save it into the mysql table.

I use this to save it:

$Db_profile = safe_sql($_POST['x_profile']);

and the function safe_sql is:

Expand|Select|Wrap|Line Numbers
  1. // Make variable SQL safe
  2. function safe_sql( $value )
  3. {
  4.      $value = strip_tags(trim($value));
  5.  
  6.     // Stripslashes
  7.     if (get_magic_quotes_gpc()) {
  8.         $value = stripslashes($value);
  9.     }
  10.     // Quote if not integer
  11.     if (!is_numeric($value)) {
  12.         $value = mysql_real_escape_string($value);
  13.     }
  14.     return $value;
  15. } // End of Function
  16.  
I save it with a normal insert:
Expand|Select|Wrap|Line Numbers
  1. $sql = "UPDATE clients SET 
  2.          profile   = '$Db_profile',
  3.          last_date = '$today'
  4.  
  5.          WHERE user_id = \"{$_SESSION['expert']}\" ";
  6.  
  7.          mysql_query($sql) or die("could not execute PROFILE update query". mysql_error());
  8.  
When I come to display the article all the sentences just get pushed together,the line breaks have al gone.

I use this to display it:

Expand|Select|Wrap|Line Numbers
  1. <div class="hot04">
  2.  <p><?php echo "$profile"; ?></p>
  3. </div> <!-- End div:hot04 --> 
  4.  

The strange thing is, when I look at the table data it still contains the line breaks so it must be my display method that is causing the problem.

What am I doing wrong ?
Jan 14 '09 #1
10 1802
Markus
6,050 Expert 4TB
HTML won't take a newline character (\n) and create a new line with it - it will in the source code, but not the live code. You need to convert these into a html tag (<br />). Luckily, there is a standard PHP function that does this for you (nl2br()). Just simply use this function on your output.

Expand|Select|Wrap|Line Numbers
  1.  
  2. $text = "These\n will\n appear\n on\n new\n lines";
  3.  
  4. echo nl2br($text);
  5.  
  6.  
Jan 14 '09 #2
jeddiki
290 100+
Hi Marcus,

Thanks,

So I can just do:

<p><?php echo "nl2br($profile)"; ?></p>

or maybe a 2 step would be better

$Sh_profile = nl2br($profile);

later
<p><?php echo "$Sh_profile"; ?></p>

Do I need the double quotes ?
( I am never quite sure )
Jan 15 '09 #3
Dormilich
8,658 Expert Mod 8TB
the first code won't work because of the quotation marks (PHP doesn't look for functions in a string)
in the third code you can omit the quotation marks, because all you want to print is the variable.

which of the two ways you implement depends on your personal liking.

@jeddiki
If you're not sure about something, refer to the PHP manual, it usually gives good explanation and plenty of examples.
PHP: echo - Manual
Jan 15 '09 #4
Markus
6,050 Expert 4TB
Markus (with a K) ;]

Double quotes: functions will not be parsed inside them, variables, however, will be parsed.

So, the second option you show will work.
Jan 15 '09 #5
Dormilich
8,658 Expert Mod 8TB
note:
Expand|Select|Wrap|Line Numbers
  1. echo myFunction();
only works, if the function has a (printable) return value.
Jan 15 '09 #6
jeddiki
290 100+
Thanks for your help,

So it seems that the textarea and the mysql dtatbase both like
the line break /n/r style and will record them invisibly,
but the HTML page needs to have these output converted <br> or <br />

One more this that is confusing me.
When using the texarea to write an article how can I add bold
and italics so that they will save in the database and then
display correctly when I output them to an HTML page ?
Jan 15 '09 #7
Dormilich
8,658 Expert Mod 8TB
@jeddiki
probably the best is to add some tags, that will be converted to the appropriate HTML tags (be it <span>, <b> or <i>). make sure you sanitize your input before putting it to DB or the HTML page.
Jan 15 '09 #8
Markus
6,050 Expert 4TB
@jeddiki
Check out BBCode.
Jan 15 '09 #9
Dormilich
8,658 Expert Mod 8TB
@Markus
note: PHP provides an extension to parse BBCode.
Jan 15 '09 #10
jeddiki
290 100+
Thanks, I am checking out how I can get the bbCode extension for my server.

Looks like what I need :)
Jan 15 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: The Roys | last post by:
Hi I have a textbox which displays a formatted numeric. eg: 100,000 When I retreive this value for calculations, it is valued at 100 eg: calc = val(txtbox) * 2 calc will equal 200, not...
1
by: George | last post by:
I've got an interesting problem that I just have not been able to figure out. I'm trying to use Mail_Mime to send multi-part/mixed emails from a Linux server and I am having trouble getting...
1
by: sck10 | last post by:
Hello, I am pulling data from a SQL Server table. One field that is (varchar 4000) is used to show notes. I am using a FormView for showing and editing the data. When the form is in Item...
2
by: Griff | last post by:
I want to display a repeating set of formatted infomation that appears within a "box" so that the individual boxes appear to the right of the previous one until the line is filled and then the next...
2
by: biganthony via AccessMonster.com | last post by:
Hi, I decided to install Office 2003 Service Pack 3 on my home computer to test (in full knowledge that there may be some issues with it). After installation, I have noticed that with a small...
0
by: suprodeep | last post by:
have a page for admin(admin.aspx). This would enter product name, image, category name and and long description in a formatted manner. The prod name, image, category name can be inputted to...
0
by: Kumba | last post by:
Hi all, Working on a pretty fancy access report that prints out a schedule of events in a two-column format, I've stumbled across the need for displaying formatted text into a field. Since I'm...
8
by: Andy B | last post by:
I have the object property StockContract.Dictionary which is a dictionary collection of <string, stringkey/value pairs. I need to be able to retreive the keys and their values and display them on a...
1
by: Bobby | last post by:
Hi, I'm using Office 2007. I have a text field called txtDate which is formatted to medium date. This gives me the date picker when the control has the focus. I have several other text controls...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.