Jump to content


Photo

text formatting in php / mysql


  • Please log in to reply
3 replies to this topic

#1 adam_gardner

adam_gardner
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 08 April 2006 - 10:27 PM

hi guys,

I have a field set-up to store a users comments - like a message.

how can i get it to output again with the correct formatting they gave it? (i.e. line breaks etc.) because all my text just runs together

is it a php thing or a mysql formatting issue?

ps. my data appears as I typed it when viewed in phpmyadmin, but not when i echo it.

any help would be great!

thanks!

Adam

#2 desithugg

desithugg
  • Members
  • PipPipPip
  • Advanced Member
  • 281 posts
  • LocationScarborough

Posted 08 April 2006 - 10:31 PM

[!--quoteo(post=362876:date=Apr 8 2006, 06:27 PM:name=adam_gardner)--][div class=\'quotetop\']QUOTE(adam_gardner @ Apr 8 2006, 06:27 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
hi guys,

I have a field set-up to store a users comments - like a message.

how can i get it to output again with the correct formatting they gave it? (i.e. line breaks etc.) because all my text just runs together

is it a php thing or a mysql formatting issue?

ps. my data appears as I typed it when viewed in phpmyadmin, but not when i echo it.

any help would be great!

thanks!

Adam
[/quote]

umm not sure what you mean but try this

<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('tpf');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query("SELECT message FROM inbox WHERE messageID = 'message id');
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
?>

and make it so its in a table and even with html variables like <center><br> etcc.. it should display properly


#3 adam_gardner

adam_gardner
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 08 April 2006 - 10:57 PM

[!--quoteo(post=362877:date=Apr 8 2006, 05:31 PM:name=desithugg)--][div class=\'quotetop\']QUOTE(desithugg @ Apr 8 2006, 05:31 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
umm not sure what you mean but try this

<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('tpf');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query("SELECT message FROM inbox WHERE messageID = 'message id');
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
?>

and make it so its in a table and even with html variables like <center><br> etcc.. it should display properly
[/quote]

nope, not sure you got me -- here's my php code

the problem is, like i'm doing in this post, putting line breaks everywhere, my text seems to run together.

here's the barebones of my php - very straight forward really.

<?
$query="SELECT * FROM messages WHERE toID = $CID";

$result=mysql_query($query) or die (mysql_error() . "\nActual query: " . $query);
$num=mysql_numrows($result);

echo "Inbox";

$i=0;
while ($i < $num) {

$Msg=mysql_result($result,$i,"message");

echo "Message :: $Msg";

$i++;
}

?>


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 06:06 AM

Line breaks in HTML are made by inserting the "<br />" tags, not by outputing a newline character. You can either output your text between "<pre>" and "</pre>" tags which preserve the new lines or use the function nl2br() on your output text:
<?php echo 'Message :: ' . nl2br($Msg); ?>
which places the "<br />" tag before each newline character.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users