Forum Moderators: coopster

Message Too Old, No Replies

I want to display only a certain number of words from a database entry

php : truncated display with a more button

         

jonny827

10:56 am on Nov 16, 2005 (gmt 0)

10+ Year Member



I want to be able to take a description from a database, row called {car_description} and cut it to a certain number of words and display only that for the main gallery of cars before you click to get the full description ... like many other sites do with the short description and the "more" button next to it.. I can do the mroe button but how do I right the rest of the tag..

Assume I already have a db connection...so from inserting
into html template engine which gets crunched by php what would I do?
<?php #*$!x.... ?>

coopster

12:33 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, jonny827.

How about if you return the primary key of that row in the database along with a substring of the description. Lay down the description and then make the "More ..." a link to your next processing script with the primary key as the value to be used to retrieve the article again on your next display page.

jonny827

1:06 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



I only want the first part of the description and I am not normally a full on programmer I can edit and make adjustments to code my techs write but this project is due tommorrow... so I am trying to get it done soon...

char_count or something but if you could get write the simple script it should take or at least give me a better idea... the row its comming from is "car_description"

care to help?

and thanks for the welcome...

neo_brown

1:15 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



I wanted to do the exact same thing the other day. I never did figure it out and I will be interested to see how to do this.
What I did, (time also being an issue) was created a new field allowing 50 characters and just transfered the description data into the new field.
I know this must be the worst way to do this, luckilly it was a very small table.

Nutter

1:45 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



How 'bout this? Take the description and explode() it into an array split by spaces. Then, take array elements 1 through 50 (or however many words) and implode() it back, again using spaces as the delimiter.

Anyango

1:55 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jonny,

Yes, everyone here cares to help. Have you already written some code? or your techs have? i mean if it's only about taking description from the database field and then showing it in a short form, thats pretty easy, but i dunno whats the status of code with you. if you could post related portion of your existing work, this would be pretty easy for anyone to assist and get that to work as expected.

regards
Kami

Hester

2:48 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than loop through the spaces, what I would do is cut the text after a specific number of characters, then move backwards through the string until you reach a space or full stop. Then just add three periods. Like this:

String = "Our new project is beginning to take shape with an emphasis on tomorrow's economic climate as predicted by Professor Smith in yesterday's news."

Cut after so many characters, not words:

String = "Our new project is beginning to take shape with an emphasis on tomorrow's eco"

Now move back to the first space, in this case after the word "tomorrow's". Add ending to suit, like so:

String = "Our new project is beginning to take shape with an emphasis on tomorrow's... (More)"

That ought to do it.

taylanpince

4:01 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



i think what you need here is a simple substr function that would cut it to as many characters as you want. for instance:

$string = substr($string, 0, 50);

would cut it down to 50 characters. and then, if you don't want a word to be cut right in the middle, you can create a simple while loop that loops until you reach the first space. that would be like:

$char = strlen($string);

while ($char > 0)
{
if ($string{$char} == " ") break;

$char = $char - 1;
}

$string = substr($string, 0, $char);

and finally, add three dots to the end of the string.

echo $string."...";

hope it helps.

cheers,
taylan

mooger35

6:26 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



This is a condensed version of what I use...

<?php
$text = $row_recap['text'];
if (strlen($text) > 300) {
$ext = "... <a href='readmore.php'>read more</a>";
} else {
$ext = "";
}
function elliStr($s,$n) {
for ( $x = 0; $x < strlen($s); $x++ ) {
$o = ($n+$x >= strlen($s)? $s : ($s{$n+$x} == " "?
substr($s,0,$n+$x) . "..." : ""));
if ( $o!= "" ) { return $o; }
}
}
?>

<?php echo (elliStr("$text", 300)) . $ext;?>