Forum Moderators: coopster

Message Too Old, No Replies

Trouble with elliStr()

Error when trying to echo 2 rows from MySQL.

         

scraptoft

1:40 pm on Nov 30, 2006 (gmt 0)

10+ Year Member



Hi,

I am trying to echo 2 results from a MySQL database and strip them to a maximum number characters and then words.

It works fine when I use it with one result but when I try to loop through two I get the following error:

Fatal error: Cannot redeclare ellistr() (previously declared in.

Here is my code:
$article = $row['text'];

if (strlen($article) > 30) {
$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; }
}
}
echo "<div class='headlinersub'>";
echo "<a href='search.php?t=$content_url'><h2>$title</h2> </a>";
echo (elliStr("$article", 50));
echo "</div>";
}[/code]

The error message says to me that it as previously been used or so on, I have tried numerous ways to correct it but failed - Could anyone lend me a hand here?

Cheers

Psychopsia

3:39 pm on Nov 30, 2006 (gmt 0)

10+ Year Member



Hi scraptoft

Just move the function outside the loop.

scraptoft

5:35 pm on Nov 30, 2006 (gmt 0)

10+ Year Member



Could you explain a bit more Psychopsia? Perhaps a demo? I can't quite figure it out.
Thanks for your reply.

Psychopsia

5:42 pm on Nov 30, 2006 (gmt 0)

10+ Year Member



A easy way to fix it is moving the function to the end of the file, before PHP close tag.

function elliStr($s,$n) {
...
}

?>

Or, if you have a file only for functions, move it there.

scraptoft

3:06 am on Dec 2, 2006 (gmt 0)

10+ Year Member



Excellent, thankyou. I've got it working - how simple was the cause too!