Your return statement is in your while loop. This means it only runs once (on the first record) and returns the string with that smiley replaced. Try something like this:
function smile($text)
{
global $connection;
$query = "SELECT * FROM tit_smilies";
$result = mysql_query($query, $connection);
while ($smrow = mysql_fetch_assoc($result))
{
$rpt = $smrow['text'];
$rpi = $smrow['replace'];
$rpd = $smrow['dec'];
$text = str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
}
return $text;
}
For the sake of efficiency, it's worth mentioning that you can pass arrays into str_replace and it will match corresponding indexes. For example:
$start = array('one', 'two', 'three');
$end = array(1, 2, 3);
echo str_replace('three blind mice', $start, $end);
The example above would output "3 blind mice". You could do the same here, looping through your results and building corresponding match/replace array,s then just calling str_replace once with the two arrays as arguments. Not sure how much more efficient that is, but I'm assuming it would help.
T
: another problem i have just came across on this pice of code that i would like some help on. the problem is each time the while function runs it works with the origional (i assum) and once the function is finnished only the last loop is displayed as the output. can someone help me or show me a better way to make a function to process text for emotes
:
: vipercyborg
:
: : Global variables aren't available inside of a function unless declared so with the global keyword.
: :
: :
: : function smile($text)
: : {
: : global $connection;
: : $query = "SELECT * FROM tit_smilies";
: : $result = mysql_query($query, $connection);
: : while ($smrow = mysql_fetch_assoc($result))
: : {
: : $rpt = $smrow['text'];
: : $rpi = $smrow['replace'];
: : $rpd = $smrow['dec'];
: : return str_replace("$rpt",'<img src="images/smiles/'.$rpi.'" border="0" alt="'.$rpd.'" title="'.$rpd.'" width="15" height="15" />', $text);
: : }
: : }
: :
: :
: :
: : T
: :
: :
: