Jump to content


Photo

Problem with an IF in a WHILE


  • Please log in to reply
2 replies to this topic

#1 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 12 April 2006 - 08:19 AM

Hey!

This is part of my shoutbox script to display the shouts.

I want to run an IF within the WHILE to determine wether the email field in the db contains "no" - which means no email obviouslly. However I tried the following script, and it doesn't work?

I have a few sample shouts in the db; with and without emails - and the outcome is that the email link is always there, which, altho only a small problem, I want it to function properly.

Im guessing it's something to do with the IF being within the WHILE.

<?php

include('../include/connect.php');

$sql = "SELECT * FROM ns_shouts ORDER BY time DESC";
    $qr = mysql_query($sql) or die("Error communicating with database.");

If ($qr) {

    while($qa=mysql_fetch_array($qr)) {
 
     $name = $qa[username];
     $shout = $qa[message];
     $time = $qa[time];
     $email = $qa[email];

    If ($email=="no") {
        print '<font style="font-family: Tahoma; font-size: 10px; color: #f1f1f1;"><b>' .$name. '</b>: </font>';
    } else {
        print '<font style="font-family: Tahoma; font-size: 10px; color: #f1f1f1;"><a href="mailto:' .$email. '"><b>' .$name. '</b></a>: </font>';
    }

    print '<font style="font-family: Tahoma; font-size: 10px; color: #f1f1f1;">' .$shout. '<br /></font>';
    print '<font style="font-family: Tahoma; font-size: 8px; color: #999999;">' .$time. '</font>';
    print '<hr color="#999999">';

    }

} else {
    print "<font class=\"normal\">No Shouts.</font>";
}

?>


Anyone any ideas?

-Thankyou

#2 bonaparte

bonaparte
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts
  • Location#172, I Floor, 'Hamsashree', Defence Colony, Sahakaranagar, Bangalore, India

Posted 12 April 2006 - 09:15 AM

[!--quoteo(post=363925:date=Apr 12 2006, 03:19 AM:name=slipperyfish)--][div class=\'quotetop\']QUOTE(slipperyfish @ Apr 12 2006, 03:19 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hey!

This is part of my shoutbox script to display the shouts.

I want to run an IF within the WHILE to determine wether the email field in the db contains "no" - which means no email obviouslly. However I tried the following script, and it doesn't work?

I have a few sample shouts in the db; with and without emails - and the outcome is that the email link is always there, which, altho only a small problem, I want it to function properly.

Im guessing it's something to do with the IF being within the WHILE.

<?php

include('../include/connect.php');

$sql = "SELECT * FROM ns_shouts ORDER BY time DESC";
    $qr = mysql_query($sql) or die("Error communicating with database.");

If ($qr) {

    while($qa=mysql_fetch_array($qr)) {
 
     $name = $qa[username];
     $shout = $qa[message];
     $time = $qa[time];
     $email = $qa[email];

    If ($email=="no") {
        print '<font style="font-family: Tahoma; font-size: 10px; color: #f1f1f1;"><b>' .$name. '</b>: </font>';
    } else {
        print '<font style="font-family: Tahoma; font-size: 10px; color: #f1f1f1;"><a href="mailto:' .$email. '"><b>' .$name. '</b></a>: </font>';
    }

    print '<font style="font-family: Tahoma; font-size: 10px; color: #f1f1f1;">' .$shout. '<br /></font>';
    print '<font style="font-family: Tahoma; font-size: 8px; color: #999999;">' .$time. '</font>';
    print '<hr color="#999999">';

    }

} else {
    print "<font class=\"normal\">No Shouts.</font>";
}

?>
Anyone any ideas?

-Thankyou
[/quote]

The expression $email=="no" returns true if and only if the $email string is "no". In all other cases it returns false. If you want to search for the string "no" within a string use eregi()

if eregi("no", $email) {
// $email contains "no"
// Process
}

eregi() is case insensitive.


#3 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 12 April 2006 - 09:32 AM

sorry, i dont think i explained properly. When they submit a shout, if they didnt enter an email, the default value "no" is put in the database. In the database, if the value of the email field is equal to "no", then the email link shouldn't be visible for that row.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users