I have a database that has poems in it. Question number one is how do i keep the returns like where a new line starts. In the database it keeps it, but when i echo the contents of the table in php it loses the formatting.
second question is: the poems are long and I just want to display a portion like 200 characters of the poem on one page and have a link to another page where you can view the rest of the poem. how would i do that?
Thanks,
Adam
http://www.adamwhiles.com
mysql and php question
Moderator: General Moderators
-
adamwhiles
- Forum Newbie
- Posts: 4
- Joined: Mon Nov 08, 2004 6:46 pm
- Contact:
This code will fix your line break issue (carriage returns/new lines) and I used a function I found on google a while ago, it detects if your string is longer then the max characters, if so it cuts it off at the 100th character (if 100 is the max) and it adds a .... to the end, if the string is < 100 it returns the string untouched.
Code: Select all
<?php
function Truncate ($str, $length=100, $trailing='...')
{
// take off chars for the trailing
$length-=strlen($trailing);
if (strlen($str) > $length)
{
// string exceeded length, truncate and add trailing dots
return substr($str,0,$length).$trailing;
}
else
{
// string was already short enough, return the string
$res = $str;
}
return $res;
}
$peomtext=str_replace("\n", "<BR>", $poemtext);
$poemtext=truncate($poemtext);
echo $poemtext;
?>-
adamwhiles
- Forum Newbie
- Posts: 4
- Joined: Mon Nov 08, 2004 6:46 pm
- Contact:
So with the code below work you think?
Code: Select all
<?PHP
function Truncate ($str, $length=100, $trailing='...')
{
// take off chars for the trailing
$length-=strlen($trailing);
if (strlen($str) > $length)
{
// string exceeded length, truncate and add trailing dots
return substr($str,0,$length).$trailing;
}
else
{
// string was already short enough, return the string
$res = $str;
}
return $res;
}
$getpoem1 = "SELECT * FROM poems ORDER BY id DESC LIMIT 1";
$getpoem2 = mysql_query($getpoem1);
while ($getpoem = mysql_fetch_array($getpoem2)) {
?>
$poemtext=$getpoemї'poem'];
$poemtext=str_replace("\n", "<BR>", $poemtext);
$poemtext=truncate($poemtext);
<?PHP echo $poemtext; ?>
<BR><BR><B>Posted By:</b> <?PHP echo $getpoemї'posted_by']; ?>
<BR><B>Written By:</b> <?PHP echo $getpoemї'written_by']; ?>
<BR><B><A HREF="./poems.php">View All Poems</A></b>
<?PHP
}
?>-
adamwhiles
- Forum Newbie
- Posts: 4
- Joined: Mon Nov 08, 2004 6:46 pm
- Contact:
oh.. ok it does work lol, from first glance it didnt look like it. Glad i could help.
EDIT:
If it works how are you changeing the value of $poemtext after you exited php with a ?> around line 22?
Maybe you had a typo in your script, or are you like outputting that php code to the browser so the user can see it?
EDIT:
Code: Select all
<?php
?>
$poemtext=$getpoem['poem'];
$poemtext=str_replace("\n", "<BR>", $poemtext);
$poemtext=truncate($poemtext);
<?PHP echo $poemtext;
?>Maybe you had a typo in your script, or are you like outputting that php code to the browser so the user can see it?
-
adamwhiles
- Forum Newbie
- Posts: 4
- Joined: Mon Nov 08, 2004 6:46 pm
- Contact: