Page 1 of 2

Help With Simple IF statement

Posted: Thu Oct 12, 2006 9:02 pm
by nickman013
Hello,

I have a comment system, but people are spamming it, with false names. Some people are using my name. To prevent this I would like to change the table row background color to a different color when I post a comment so people know it is by me.

My code is this so far, apparently it doesnt work, but I tried to write it how I thought it would work.

Code: Select all

$result4 = mysql_query($sql4) or die(mysql_error()); 
while($row4 = mysql_fetch_array($result4)) { 
echo "<tr".if $row['ip']==68.195.158.89{echo 'bgcolor=red'}else{};.">"; 
echo "<td align=left width=100 valign=top>".$row4['name']."</td>"; 
echo "<td align=left width=400>".$row4['comment']."</td>";
echo "</tr>";
echo "<tr>"; 
echo "<td>";
echo " ";
echo "</td>";
echo "</tr>";
} mysql_close($connection2); 
?>
</table>
</center>
I know it can be better, but it works.

The error I get is
Parse error: parse error, unexpected T_IF in /home/muot/public_html/muotreport2.php on line 494
If any one can help me solve this, I would greatly appreciate it.

Thanks So Much!

Posted: Thu Oct 12, 2006 9:21 pm
by neophyte

Code: Select all

echo "<tr".if ($row['ip']==68.195.158.89){echo 'bgcolor=red'}else{};.">";
I think that'll fix it. But don't let the IP police around this board check out your code.

Posted: Thu Oct 12, 2006 9:29 pm
by nickman013
Thanks for responding.

I got this error. I tried to look it over and find a error, but I couldnt.


Parse error: parse error, unexpected T_IF in /home/muot/public_html/muotreport2.php on line 494



What do you mean by IP police?

Posted: Thu Oct 12, 2006 9:29 pm
by John Cartwright

Code: Select all

echo "<tr". ($row['ip'] == '68.195.158.89' ? 'bgcolor=red' : '') .">";
Firstly you cannot escape an echo with an if statement but you can use a ternary (see above). Also you need to quote the ip address, as the stored format is most likely a string.

Posted: Thu Oct 12, 2006 9:31 pm
by neophyte
Sometimes with this type of error its best to comment chunks of code till you isolate it. You may also want to use an app like PHPEclipse. Good IDE's generally pick up on this sort of syntax error. You have an if statement with missing { or ) characters somewhere.

Posted: Thu Oct 12, 2006 9:35 pm
by nickman013
JCart,

Thanks for helping. But,,, It doesnt seem to work, but I do not get a parse error.
Im stumped..

Posted: Thu Oct 12, 2006 9:37 pm
by John Cartwright
and what does

Code: Select all

var_dump($row['ip']);
return?

Posted: Thu Oct 12, 2006 9:39 pm
by neophyte
I removed the semicolon. Try this.
Jcart doesn't echo the bgcolor. Check your html source.

Code: Select all

echo "<tr".if ($row['ip']==68.195.158.89){echo 'bgcolor=red'}else{}.">";

Posted: Thu Oct 12, 2006 9:43 pm
by nickman013
NULL

Posted: Thu Oct 12, 2006 9:44 pm
by John Cartwright
neophyte wrote:I removed the semicolon. Try this.
Jcart doesn't echo the bgcolor. Check your html source.

Code: Select all

echo "<tr".if ($row['ip']==68.195.158.89){echo 'bgcolor=red'}else{}.">";
I'm sorry, you are mistaken. I am echo'ing out the bgcolor -- I take it you are unfamiliar with the ternary operator. Infact, as I previously mentioned it is not possible to escape an echo() with an if() statement.

I actually made a boo-boo with my original code (I forgot to add a space). Try

Code: Select all

echo "<tr". ($row['ip'] == '68.195.158.89' ? ' bgcolor=red' : '') .">";

Posted: Thu Oct 12, 2006 9:44 pm
by nickman013
My code right now.

Code: Select all

$sql4 = "SELECT * FROM `comments` WHERE who =" . $row['Number'] . " ORDER BY `id` DESC"; 
$result4 = mysql_query($sql4) or die(mysql_error()); 
while($row4 = mysql_fetch_array($result4)) { 
echo "<tr". ($row4['ip'] == '68.195.158.89' ? 'bgcolor=red' : '') .">";
echo "<td align=left width=100 valign=top>".$row4['name']."</td>"; 
echo "<td align=left width=400>".$row4['comment']."</td>";
echo "</tr>";
echo "<tr>"; 
echo "<td>";
echo " ";
echo "</td>";
echo "</tr>";
} 

mysql_close($connection2);

Posted: Thu Oct 12, 2006 9:46 pm
by nickman013
love you..

thank you both for all your help!!

Posted: Thu Oct 12, 2006 9:46 pm
by neophyte

Code: Select all

echo "<tr".if (strcmp($row['ip'], '68.195.158.89') ==0 ){echo 'bgcolor=red'}else{}.">";
Your getting the else{}. Try comparing the ip addresses as strings.

Posted: Thu Oct 12, 2006 9:48 pm
by John Cartwright
nickman013 wrote:NULL
Your database does not have a value for $row['ip'] then. I take it you are recording $_SERVER['REMOTE_ADDR'] during insertion to the database, but this variable is not guaranteed to exist.

I would check to make sure $row['ip'] exists before checking it's value.

Code: Select all

echo "<tr". (!empty($row['ip']) && $row['ip'] == '68.195.158.89' ? ' bgcolor=red' : '') .">";

Posted: Thu Oct 12, 2006 9:49 pm
by John Cartwright
neophyte wrote:

Code: Select all

echo "<tr".if (strcmp($row['ip'], '68.195.158.89') ==0 ){echo 'bgcolor=red'}else{}.">";
Your getting the else{}. Try comparing the ip addresses as strings.
Please try running that code. I have mentioned twice now that is invalid syntax.
nickman013 wrote:My code right now.

Code: Select all

$sql4 = "SELECT * FROM `comments` WHERE who =" . $row['Number'] . " ORDER BY `id` DESC"; 
$result4 = mysql_query($sql4) or die(mysql_error()); 
while($row4 = mysql_fetch_array($result4)) { 
echo "<tr". ($row4['ip'] == '68.195.158.89' ? 'bgcolor=red' : '') .">";
echo "<td align=left width=100 valign=top>".$row4['name']."</td>"; 
echo "<td align=left width=400>".$row4['comment']."</td>";
echo "</tr>";
echo "<tr>"; 
echo "<td>";
echo " ";
echo "</td>";
echo "</tr>";
} 

mysql_close($connection2);
A couple posts back I mentioned you also needed a space. Change 'bgcolor=red' to ' bgcolor=red'