Page 1 of 1
mysql and php question
Posted: Mon Nov 08, 2004 6:52 pm
by adamwhiles
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
Posted: Mon Nov 08, 2004 7:00 pm
by josh
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;
?>
Posted: Mon Nov 08, 2004 7:36 pm
by adamwhiles
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
}
?>
Posted: Mon Nov 08, 2004 7:41 pm
by josh
No that code would not work... Write a script to store the poem to a variable and then echo that variable... once you get that script working post back here and I will tell you how to perform the line break search and truncate function on it.
Posted: Mon Nov 08, 2004 7:46 pm
by adamwhiles
actually it does work, thanks =]
Posted: Mon Nov 08, 2004 7:49 pm
by josh
oh.. ok it does work lol, from first glance it didnt look like it. Glad i could help.
EDIT:
Code: Select all
<?php
?>
$poemtext=$getpoem['poem'];
$poemtext=str_replace("\n", "<BR>", $poemtext);
$poemtext=truncate($poemtext);
<?PHP echo $poemtext;
?>
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?
Posted: Mon Nov 08, 2004 7:52 pm
by adamwhiles
it was a typo, i fixed that lol....thanks alot man =]