homepage Welcome to WebmasterWorld Guest from 54.211.50.5
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
limit number of words in a description
hanyaz




msg:3787253
 12:44 pm on Nov 16, 2008 (gmt 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

 

Pico_Train




msg:3787262
 1:08 pm on Nov 16, 2008 (gmt 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]

hanyaz




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

thank you !

asas111




msg:3814753
 2:03 am on Dec 27, 2008 (gmt 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,

Pico_Train




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

Try this. Put that function above your query.

Then

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

That "should" work - :-)

asas111




msg:3814926
 6:44 pm on Dec 27, 2008 (gmt 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 :)

Pico_Train




msg:3815110
 3:10 pm on Dec 28, 2008 (gmt 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]

asas111




msg:3815224
 8:39 pm on Dec 28, 2008 (gmt 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

Pico_Train




msg:3815433
 7:57 am on Dec 29, 2008 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved