mysql and php question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adamwhiles
Forum Newbie
Posts: 4
Joined: Mon Nov 08, 2004 6:46 pm
Contact:

mysql and php question

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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;
?>
adamwhiles
Forum Newbie
Posts: 4
Joined: Mon Nov 08, 2004 6:46 pm
Contact:

Post by adamwhiles »

So with the code below work you think?

Code: Select all

<?PHP
function Truncate ($str, $length=100, $trailing='...')
&#123;      
// take off chars for the trailing      
$length-=strlen($trailing);      
if (strlen($str) > $length)      
&#123;         
// string exceeded length, truncate and add trailing dots         
return substr($str,0,$length).$trailing;      
&#125;      
else     
&#123;         
// string was already short enough, return the string         
$res = $str;      
&#125;        
return $res;
&#125;

$getpoem1 = "SELECT * FROM poems ORDER BY id DESC LIMIT 1";
$getpoem2 = mysql_query($getpoem1);
while ($getpoem = mysql_fetch_array($getpoem2)) &#123;
?>
$poemtext=$getpoem&#1111;'poem'];
$poemtext=str_replace("\n", "<BR>", $poemtext);
$poemtext=truncate($poemtext);

<?PHP echo $poemtext; ?>
<BR><BR><B>Posted By:</b> <?PHP echo $getpoem&#1111;'posted_by']; ?>
<BR><B>Written By:</b> <?PHP echo $getpoem&#1111;'written_by']; ?>
<BR><B><A HREF="./poems.php">View All Poems</A></b>
<?PHP
&#125;
?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
adamwhiles
Forum Newbie
Posts: 4
Joined: Mon Nov 08, 2004 6:46 pm
Contact:

Post by adamwhiles »

actually it does work, thanks =]
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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?
adamwhiles
Forum Newbie
Posts: 4
Joined: Mon Nov 08, 2004 6:46 pm
Contact:

Post by adamwhiles »

it was a typo, i fixed that lol....thanks alot man =]
Post Reply