Jump to content

Truncating the value of an array


Rithotyn

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/150472-truncating-the-value-of-an-array/
Share on other sites

Got the answer off another forum.

 

SHould have been...

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

 

combined with the previous function.

 

Thanks for your help anyway

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.