...

View Full Version : Truncating the value of an array in PHP5



Rithotyn
03-21-2009, 06:03 PM
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

PappaJohn
03-21-2009, 06:12 PM
while ($row = mysql_fetch_array($result)) {
echo truncate($row['joketext'], 100) .'<br>';
echo $row['jokedate']. '<br><br>';
}

timgolding
03-21-2009, 06:13 PM
<?php


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


//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>';
}

//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 truncate($row['joketext'],6).'<br>';
echo $row['jokedate']. '<br><br>';
}
?>


Something like this?

steelaz
03-21-2009, 06:13 PM
Did you try



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


This should work if you have your truncate() function in the same file or included from another file.

Rithotyn
03-21-2009, 06:17 PM
Worked perfectly. Many thanks for your help.

:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum