0

I can't figure this out and isn't my strong side of codeing ether.

As of right now it'll only print the first person and timestamp, not anything more.

<table cellpadding="0" cellspacing="0" width="100%">
<tr><td></td></tr>

<?php
include '../connection.php';  



$sql = "SELECT * 
FROM messagebox
INNER JOIN person
ON messagebox.sid = person.sid
ORDER BY messagebox.id DESC
LIMIT 20
";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
if ($switch=='1')
{
echo "<tr bgcolor=\"#FFFFFF\">";
$switch='0';
}
else
{
echo "<tr bgcolor=\"#F9F9F9\">";
$switch='1';
}

$elfstring = utf8_encode($row['shout']);

function smiley($elfstring) { 
      $elfstring = ereg_replace(":)","<img src=!.png alt=\"!\" >", $elfstring); 
      $elfstring = ereg_replace(":(","<img src=laugh.gif alt=\":D\" >", $elfstring); 
      $elfstring = ereg_replace(":p","<img src=tongue.gif alt=\":p\" >", $elfstring); 
      return $elfstrings; 

}
$messages = smiley($elfstring);

echo "";
   
echo "<td width=\"100\" valign=\"top\"><strong>" . $row['name'] . "</strong></td>";
 
echo "<td width=\"100\" valign=\"top\">" . "(" . $row['place'] .")</td>";
echo "<td width=\"70\" valign=\"top\">" . "" . date('H:i:s',strtotime ($row['timestamp'])) ."</td>";
echo "<td valign=\"top\">" . smiley($elfstrings) . "</td>";
echo "</tr>";

} 
?>

<tr>
<td>

</td>
</tr>
</table>

I know some parts of this code is deprecated, but the server using this is old and isn't up to date.

Thanks in advance for help.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • 1
    The ereg_ functions have been deprecated ages ago. If you need to use regular expressions, use the preg_ functions instead. But here you have no reason to use regex, because you only want to replace static search strings - so str_replace will do. – CBroe Sep 22 '17 at 12:27
  • You sure there is more then one result using that exact query? – Peter M Sep 22 '17 at 12:28
  • It's supposed to print 20 lines from the messagebox, right now it's printing 1 and the message isn't echoed. – Petrus Alli Sep 22 '17 at 12:31
  • Not sure I follow you... – Petrus Alli Sep 22 '17 at 12:37
  • Got it! Now all lines gets echoed but not the message rows, the once that the function is supposed to handle. – Petrus Alli Sep 22 '17 at 12:44
  • `echo "" . smiley($elfstrings) . "";` – Petrus Alli Sep 22 '17 at 12:48
  • Yeah only that part gets echoed, not the calue from the row shout after beeing handled by the function. – Petrus Alli Sep 22 '17 at 12:51
  • Changed it to str_replace, but how do I make the () escape? – Petrus Alli Sep 22 '17 at 12:57
  • 1
    your variable inside the smiley function is called 'elfstring'. But you are returning 'elfstrings' (notice the S at the end). The elfstrings variable doesn't exist so your function does not return anything – Wessel van der Linden Sep 22 '17 at 13:41
  • @WesselvanderLinden Aha, good eyes. I figured it was something like that but didn't look through that closely. Another thing error reporting would have identified. – chris85 Sep 22 '17 at 13:50

1 Answers1

0
  1. You can't declare a function multiple times. Move the function smiley(){ outside of your while loop.
  2. You should enable error reporting and monitor your error logs.
  3. You should indent each control block so you can easily tell where blocks end/start.
  4. If you don't need a regex/ aren't using one don't use a regex function. The ( and ) are special regex characters and will cause errors. Use str_replace because you are doing static replacements anyway.
  5. You can enclose strings in " or ', this can simplify string construction because you won't need to escape.

So make the ending of your script:

function smiley($elfstring) { 
      return str_replace(array(':)', ':(', ':p'),  
                         array('<img src="!.png" alt="!" >', '<img src="laugh.gif" alt=":D" >', '<img src="tongue.gif" alt=":p" >'), 
                          $elfstring); 
}
chris85
  • 23,846
  • 7
  • 34
  • 51
  • That works! But how do I echo it correctly? `echo "" . smiley($elfstrings) . "";` dosen't echo anything. – Petrus Alli Sep 22 '17 at 13:02
  • Is `$elfstrings` actually populated? Try `var_dump($row['shout']); var_dump($elfstrings);`, what do you get? – chris85 Sep 22 '17 at 13:04
  • Gets NULL value on both, but they're populated in database.... Got it, I managed to delete the definition of elfstring. THX! – Petrus Alli Sep 22 '17 at 13:07