Rithotyn Posted March 21, 2009 Share Posted March 21, 2009 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 More sharing options...
ashton321 Posted March 21, 2009 Share Posted March 21, 2009 I am fairly new at PHP as well but could you run a MYSQL query that limits to 100 character? Link to comment https://forums.phpfreaks.com/topic/150472-truncating-the-value-of-an-array/#findComment-790310 Share on other sites More sharing options...
Rithotyn Posted March 21, 2009 Author Share Posted March 21, 2009 Well that was my first thought as i knew how to do that, within MySQL, but i don't know how to incorporate that into the PHP. Link to comment https://forums.phpfreaks.com/topic/150472-truncating-the-value-of-an-array/#findComment-790312 Share on other sites More sharing options...
ashton321 Posted March 21, 2009 Share Posted March 21, 2009 well when you do the query <?php $query= "SELECT joketext FROM `database` LEFT (joketext, 50)"; $result = mysql_query($query); ?> Then do what you want with data Link to comment https://forums.phpfreaks.com/topic/150472-truncating-the-value-of-an-array/#findComment-790315 Share on other sites More sharing options...
Rithotyn Posted March 21, 2009 Author Share Posted March 21, 2009 I tried $result = @mysql_query('SELECT joketext FROM joke LEFT(joketext, 50)'); but it just returned an error Link to comment https://forums.phpfreaks.com/topic/150472-truncating-the-value-of-an-array/#findComment-790317 Share on other sites More sharing options...
Rithotyn Posted March 21, 2009 Author Share Posted March 21, 2009 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 Link to comment https://forums.phpfreaks.com/topic/150472-truncating-the-value-of-an-array/#findComment-790333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.