Hi i'm fairly new to PHP and I am slowly trying to contruct a guestbook. My end aim is to have a simple guestbook that runs off MySQL, while also on the front page having some sort of small "Flash Box" that shows the first 3 or so entries truncated down to 100 characters or so.

Currently im working off a tutorial that had me create a jokes database. I can get the jokes within the database to show up fine. What i want to do now is is to truncate them down. I currently have..


<?php

//Checks to see If a connection can be made to the MySQL server and displays the appropriate message

$connect = @mysql_connect('localhost', 'root', '*******');

if (!$connect) {
   exit ('Wont Connect to MySQL Server');
} else {
   echo 'Connected to my SQL Server<br><br><hr><br>';
}



//Checks to see if a connection can be made to the database IJDB and displays appropriate message

mysql_select_db('ijdb', $connect);
      if (!@mysql_select_db('ijdb')) {
         exit ('Wont Connect to Joke Database');
} else {
         echo 'Connected to my Joke Database<br><br><hr><br>';
}
?>
<?php

//Retrieves Jokes and loads them into array

$result = @mysql_query('SELECT joketext, jokedate FROM joke');

//Displays the loaded jokes

while ($row = mysql_fetch_array($result)) {
   echo $row['joketext'].'<br>';   
echo $row['jokedate']. '<br><br>'; } ?>

This works fine and shows the jokes as expected. I've been working with the following function, and while i don't understand exactly how it works, I understand what it does.

<?php

function truncate($string, $del) {
  $len = strlen($string);
  if ($len > $del) {
    $new = substr($string,0,$del)."...";
    return $new;
  }
  else return $string;
}
?>

<?php

$arg = "Hello World. How are you?";

echo $arg."<br>";
echo truncate($arg, 6)."<br>";
echo truncate($arg, 7)."<br>";

?>

I can't figure out how to integrate it into

echo $row['joketext'].'<br>';

I've tried various things, but I think im having problems as that is based on a single variable and I want to use it against ALL jokes that come out of the joketext field.

Any help would be appreciated.

Thanks

Richard

    All you need to do is echo the output of the function rather than outputting the $row variable itself. The code you posted even has examples at the bottom.

    So... what have you tried?

      Write a Reply...