Help With Simple IF statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Help With Simple IF statement

Post 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!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

JCart,

Thanks for helping. But,,, It doesnt seem to work, but I do not get a parse error.
Im stumped..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

and what does

Code: Select all

var_dump($row['ip']);
return?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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{}.">";
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

NULL
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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' : '') .">";
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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);
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

love you..

thank you both for all your help!!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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' : '') .">";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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'
Post Reply