Welcome to WebmasterWorld Guest from 50.17.114.227

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

limit number of words in a description

     
12:44 pm on Nov 16, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 25, 2007
posts: 94
votes: 0


hello,
i want to display a small intro of with a limited number of words without cutting the last word if it is too long...
I know there is a php function substr() but it does not display the intro like "this is my starting text..."
it makes it like "this is my starting t..."
thanks for any suggestion
hanyaz
1:08 pm on Nov 16, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


This will cut your description to 175 characters, you can change the number of characters...;-), enjoy!
$variable['abbr'] = $this->cutText($variable['description'], 175);

function cutText($string, $length) {

if($length<strlen($string)){

while ($string{$length} != " ") {

$length--;
}

return substr($string, 0, $length);

}else return $string;
}

[edited by: Pico_Train at 1:09 pm (utc) on Nov. 16, 2008]

2:03 pm on Nov 16, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 25, 2007
posts: 94
votes: 0


thank you !
2:03 am on Dec 27, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 11, 2005
posts:127
votes: 0


Where do I insert that code?

I have the following code, and I am trying to display first 100 characters or so, up to the last character in the word.

<?php

// Create the connection and select the DB
$link = mysql_connect("URL","db","password");

if ($link) {
mysql_selectdb("db",$link);

// Select records from the DB
$query = "SELECT content,thema,id FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
$result = mysql_query($query);

// Display records from the table
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

echo "<div style=\"font-weight:bold\">" . substr($row[1],0,50) . "</div><div>" . substr($row[0],0,75) . "...</div>" .
"<div><a href=\"http://www.example.net/faq/index.php?action=artikel&artlang=en&id=".$row[2]."\">Click to read more...</a></div>";

}
} else {
echo "Can't connect to the database!";
}

?>

thanks a lot,

5:18 pm on Dec 27, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


Try this. Put that function above your query.

Then

$query['abbr_content'] = cutText($query['content'],100);

That "should" work - :-)

6:44 pm on Dec 27, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 11, 2005
posts:127
votes: 0


I appreciate your help Pico, but I am still a little lost.
I am now working with three parts:

<Original Code>

<?php

// Create the connection and select the DB
$link = mysql_connect("URL","db","password");

if ($link) {
mysql_selectdb("db",$link);

// Select records from the DB
$query = "SELECT content,thema,id FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
$result = mysql_query($query);

// Display records from the table
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

echo "<div style=\"font-weight:bold\">" . substr($row[1],0,50) . "</div><div>" . substr($row[0],0,75) . "...</div>" .
"<div><a href=\"http://www.example.net/faq/index.php?action=artikel&artlang=en&id=".$row[2]."\">Click to read more...</a></div>";

}
} else {
echo "Can't connect to the database!";
}

?>

------------------------------------
<your first code>

$variable['abbr'] = $this->cutText($variable['description'], 175);
function cutText($string, $length) {

if($length<strlen($string)){

while ($string{$length} != " ") {

$length--;
}

return substr($string, 0, $length);

}else return $string;
}

------------------------------
<your second code>

$query['abbr_content'] = cutText($query['content'],100);

#*$!#*$!#*$!#*$!#*$!#*$!#*$!#*$!#*$!#*$!#*$!#*$!#*$!XX

Based on all that you told me, I did it but it is not working. I am not sure if I am putting all three together properly.

Appreciate any further help, this is bothering me and I need to get it done :)

3:10 pm on Dec 28, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


Try this

<?php

// Create the connection and select the DB
$link = mysql_connect("URL","db","password");

if ($link) {
mysql_selectdb("db",$link);

// Select records from the DB
$query = "SELECT content,thema,id FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
$result = mysql_query($query);

$result['abbr'] = $this->cutText($result['content'], 175);
function cutText($string, $length) {

if($length<strlen($string)){

while ($string{$length} != " ") {

$length--;
}

return substr($string, 0, $length);

}else return $string;
}

// Display records from the table
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

echo "<div style=\"font-weight:bold\">" . substr($row[1],0,50) . "</div><div>" . substr($row[0],0,75) . "...</div>" .
"<div><a href=\"http://www.example.net/faq/index.php?action=artikel&artlang=en&id=".$row[2]."\">Click to read more...</a></div>";

}
} else {
echo "Can't connect to the database!";
}

?>

You need to try and figure this out yourself if you get an error. The error should be easy to figure out. It is always best to try and figure things out before simply going for the copy and paste option. It's the best way to learn.

If you get an error. why don't you print_r($result); so you can see if $result['abbr'] gets added. Let me know how it goes.

[edited by: Pico_Train at 3:19 pm (utc) on Dec. 28, 2008]

[edited by: eelixduppy at 6:32 pm (utc) on Dec. 28, 2008]
[edit reason] formatting [/edit]

8:39 pm on Dec 28, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 11, 2005
posts:127
votes: 0


Thanks Pico. I am doing my best to learn on my own, and things are coming pretty good so far.

I did what you told me, tested it and I got the following error:

Fatal error: Call to a member function on a non-object in /home/content/........ (on line 485)

Line 485 in my code corrosponds to:
$result['abbr'] = $this->cutText($result['content'], 175);

I am trying to figure out why that is the issue.

Thanks a lot

7:57 am on Dec 29, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


ok that means it can't find the funtion cutText();

Try this:

<?php

// Create the connection and select the DB
$link = mysql_connect("URL","db","password");

if ($link) {
mysql_selectdb("db",$link);

function cutText($string, $length) {

if($length<strlen($string)){

while ($string{$length} != " ") {

$length--;
}

return substr($string, 0, $length);

}else return $string;
}

// Select records from the DB
$query = "SELECT content,thema,id FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
$result = mysql_query($query);

$result['abbr'] = $this->cutText($result['content'], 175);

// Display records from the table
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

echo "<div style=\"font-weight:bold\">" . substr($row[1],0,50) . "</div><div>" . substr($row[0],0,75) . "...</div>" .
"<div><a href=\"http://www.example.net/faq/index.php?action=artikel&artlang=en&id=".$row[2]."\">Click to read more...</a></div>";

}
} else {
echo "Can't connect to the database!";
}

?>

if that doesn't work, try removing the $this-> from $result['abbr'] = $this->cutText($result['content'], 175); and see if that works. Just keep moving things around and trying, almost there.