Forum Moderators: coopster
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]
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,
<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 :)
<?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]
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
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.