Jump to content

Problem with an IF in a WHILE


slipperyfish

Recommended Posts

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.

[code]<?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>";
}

?>[/code]


Anyone any ideas?

-Thankyou
Link to comment
https://forums.phpfreaks.com/topic/7177-problem-with-an-if-in-a-while/
Share on other sites

[!--quoteo(post=363925:date=Apr 12 2006, 03:19 AM:name=slipperyfish)--][div class=\'quotetop\']QUOTE(slipperyfish @ Apr 12 2006, 03:19 AM) [snapback]363925[/snapback][/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.

[code]<?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>";
}

?>[/code]
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.
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.