/    Sign up×
Community /Pin to ProfileBookmark

Wrap a mysql text field in php

Ok ive been looking at web examples for days and nothing has helped me so far, so I thought I may as well post what im doing and get the answer this way.

Its basic as, I have a text field in mysql that I want to display by echo’ing it. However I dont want it to push the page out, instead it needs to wrap.

Its basically just

[code=php]
echo $comments;
[/code]

Ive tried wrap_text, wrap and other functions just to do it, as well as that nbrl thingy. There must be something simple to do this, I saw somewhere here:

[code=php]
echo wordwrap($comments[$i], 56, ” “, 1);
[/code]

But obviously something must have been set earlier as for me its just showing the first letter.

Thanks

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 27.2005 — If you want to keep any newlines that exist within the text, use the function [url=http://www.php.net/nl2br]nl2br()[/url] to convert those newlines to <br> tags in the output. If you just want it to be treated as one paragraph that breaks as needed to fit the space, wrap it in <p>...</p> tags within the body section of your HTML document:
[code=php]
echo "<p>{$comments[$i]}</p>n";
[/code]
Copy linkTweet thisAlerts:
@SheldonOct 27.2005 — can you not use CSS to style it?
[code=php]
echo '<div style="width: 100;"> .$comments. </div>';
[/code]
Copy linkTweet thisAlerts:
@avaOct 27.2005 — I'd say, either try:

[code=php]$newcomments = wordwrap($comments, 56 );
echo "$newcommentsn"[/code]


Or just make a table with border 0 and the width you like and put the text in there.
Copy linkTweet thisAlerts:
@on_lookerauthorOct 27.2005 — Ok I tried echo "<p>{$comments[$i]}</p>n"; but again that just outputs the first letter. Do I need to do anything previously with [$i]??

Heres my query, just a normal one:
[code=php]
//display all guestbook entries
$sql = ('SELECT * FROM Guestbook ORDER BY GuestbookDate DESC');
$result = @mysql_query($sql);
//show error message if no result
if ($result === false) { die(mysql_error()); }
//display all guestbook entries
while ($row = mysql_fetch_array($result))
{
$guestbookid = $row['GuestbookID'];
$name = $row['GuestbookName'];
$date = $row['GuestbookDate'];
$comments = $row['GuestbookComments'];
$country = $row['GuestbookCountry'];
[/code]


Then my table code as follows:
[code=html] <tr>
<td class="textgrey12" valign="top">Comments:</td>
<td colspan="2" class="textnavy12">
<?php echo "<p>{$comments[$i]}</p>n"; ?></td>
</tr>
[/code]
Copy linkTweet thisAlerts:
@on_lookerauthorOct 27.2005 — I tried ava's newcomments sugguestion but the text is still going out past the page edge. I was hestiant to specify a width because i want it to stretch with the page but just the comments to look normal.

Edit: I specified a width but it still went past it.

P.S When I view forums now they look all weird and archived rather then the normal layout, is this normal or am i in search view or something?
Copy linkTweet thisAlerts:
@NogDogOct 27.2005 — Not sure if I see enough code there to know how you're implementing this. However, I suspect you want to do something along the lines of:
[code=php]
<table>
<?php
while ($row = mysql_fetch_array($result))
{
$guestbookid = $row['GuestbookID'];
$name = $row['GuestbookName'];
$date = $row['GuestbookDate'];
$comments = $row['GuestbookComments'];
$country = $row['GuestbookCountry'];
# presumably other rows precede the following for the other fields?
echo "<tr><td class='textgrey12' valign='top'>Comments:</td>n";
echo "<td colspan='2' class='textnavy12'>$comments</td></tr>n";
}
?>
</table>
[/code]
Copy linkTweet thisAlerts:
@on_lookerauthorOct 27.2005 — This is my table after the while loop, HTML with php echo's.

[code=html]
<td width="66%">&nbsp;</td>
<td width="16%" class="textgrey12"><?php echo $date; ?></td>
</tr>
<tr>
<td class="textgrey12">Name:</td>
<td colspan="2" class="textnavy12"><?php echo $name; ?></td>
</tr>
<tr>
<td class="textgrey12" valign="top">Comments:</td>
<td colspan="2" class="textnavy12">
<?php echo $comments; ?></td>
</tr>
<tr>
<td class="textgrey12">Country:</td>
<td colspan="2" class="textnavy12"><?php echo $country; ?></td>
</tr>
<tr>
<td colspan="3"><hr></td>
</tr>
<tr>
<td>
[/code]
Copy linkTweet thisAlerts:
@on_lookerauthorOct 30.2005 — Found this in anothe rtopic and it seems to work fine ?

$wrappedText = wordwrap($text, 70, "<br>");
×

Success!

Help @on_looker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...