Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL returning unformatted text 1

Status
Not open for further replies.

philstockham

Technical User
Feb 21, 2005
20
GB
I am returning text from Mysql and it is displayed in a non-formatted list. How do i get PHP to change the text so it breaks the text into lines etc as i inputted into my form. it is going in to the form and db ok just returning corretcly.

Any help will be much appreciated
 
HTML is white-space agnostic, meaning it will treat any number of spaces and line breaks as a single space character. If you do a view source you will see that text is formatted as you did. HTML has a tag called <pre> which preserves formatting. You could just encompass your variable in it:
Code:
echo '<pre>';
echo $var;
echo '</pre>';
Other than that, you could use the nl2br function to change regular new lines to html accepted new line characters (<br />).
 
not sure how to incorporate that into my code. below is the code i am using:

<?php
include 'config.php';
include 'opendb.php';

// if no id is specified, list the available articles
if(!isset($_GET['id']))
{
$self = $_SERVER['PHP_SELF'];

$query = "SELECT id, title FROM report ORDER BY id";
$result = mysql_query($query) or die('Error : ' . mysql_error());

// create the article list
$content = '<ol>';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $title) = $row;
$content .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
}

$content .= '</ol>';

$title = 'Reports';
} else {
// get the article info from database
$query = "SELECT title, description, pricing, covershot, mainbody, contents, keywords FROM report WHERE id=".$_GET['id'];
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);


$description = htmlspecialchars($description);
$pricing = htmlspecialchars($pricing);
$mainbody = htmlspecialchars($mainbody);
$contents = htmlspecialchars($contents);

$title = $row['title'];
$description = $row['description'];
$pricing = $row['pricing'];
$covershot = $row['covershot'];
$mainbody = $row['mainbody'];
$contents = $row['contents'];
$keywords = $row['keywords'];



}

include 'closedb.php';
?>
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<style type="text/css">
<!--
h1 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
}

h2 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}

.main {
padding: 10px;
border: 1px solid #006699;
position: relative;
width: 580px;
margin-top: 20px;
margin-bottom: 20px;
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<?php include 'reportheader.htm'; ?>

<table border=0 cellspacing=0 cellpadding=0 width=934>
<!--DWLayoutTable-->
<tr valign=top>
<td width="23" height="25"></td>
<td width="535">&nbsp;</td>
<td width="24"></td>
<td width="24">&nbsp;</td>
<td width="1" rowspan=8 valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
<td width="28">&nbsp;</td>
<td width="159">&nbsp;</td>
<td width="140">&nbsp;</td>
</tr>
<tr valign=top>
<td height="15"></td>
<td rowspan=2 valign="top"><h1 align="left"><?php echo $title; ?></h1></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr valign=top>
<td height="12"></td>
<td></td>
<td></td>
<td></td>
<td rowspan=3 valign="top"> <p><span class="bodybold">&nbsp; <img src="<?php echo $covershot; ?>" border="0"></span></p>
<p><span class="body"></span></p></td>
<td></td>
</tr>
<tr valign=top>
<td height="18"></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr valign=top>
<td height="127"></td>
<td colspan=2 rowspan=2 valign="top"><p><?php echo $mainbody; ?><br>
</p>
<p>&nbsp; </p></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign=top>
<td height="195">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td rowspan=2 valign="top"> <h2 align="left"><?php echo $pricing; ?></h1></td>
<td>&nbsp;</td>
</tr>
<tr valign=top>
<td height="368">&nbsp;</td>
<td colspan=2 valign="top"> <p><span class="body"><strong><font face="Geneva, Arial, Helvetica, sans-serif">Table</font></strong><font face="Geneva, Arial, Helvetica, sans-serif">&nbsp;<strong>of
contents:</strong></font></span></p>
<p><strong></strong><span class="body"><?php echo $contents; ?></span></p>
<p><span class="body">&nbsp;</span></p></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign=top>
<td height="40">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr valign=top>
<td height="45">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</div>


</body>
</html>


thanks
 
bit difficult to know which field you're worried about (as it's best to use the <pre> tags - you would just incorporate these into the html itself.

if you wanted the php method in the code block that looks like
Code:
  $title   = $row['title'];
    $description = $row['description'];
    $pricing = $row['pricing'];
    $covershot = $row['covershot'];
    $mainbody = $row['mainbody'];
    $contents = $row['contents'];
    $keywords = $row['keywords'];

just change the syntax to be:
Code:
  $title   = nl2br($row['title']); //etc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top