Page 1 of 1

need help with an IF

Posted: Sat Nov 11, 2006 3:10 pm
by me!
Ok I am still new to PHP and am having trouble with this "if"
It should only display the table and info if $mem_type == Family, if $mem_type is anaything else I want it to do nothing. But I am always seeing the table, but the display of SQL results is working as it should. (not seeing when $mem_type does not == Family)
What did i do wrong?

Code: Select all

<?php
// Chech to see if membership type is Family, if ttrue return all family members name and call in HTML table.
// Get a specific result from the table
if ($mem_type == Family)
$result = mysql_query("SELECT pn_name, pn_uname FROM _users WHERE barc_id='$barc_id'")
or die(mysql_error());
 echo "<br /><table width=\"250\"  border=\"0\" cellspacing=\"0\" cellpadding=\"2\">
       <caption><b>All Family Members</b></caption>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table><br />\n";
?>

Posted: Sat Nov 11, 2006 3:20 pm
by John Cartwright

Code: Select all

if ($mem_type == 'Family')
Strings need quoting ;)

Posted: Sat Nov 11, 2006 3:40 pm
by aaronhall
You also need to enclose the entire block in curly brackets

Posted: Sat Nov 11, 2006 4:26 pm
by me!
Ok now it's working - Thanks guys!

It was the curlu brackets that did it.

Now as far as the string needing quotes, do they, or should they? I have 6 others that do not have qqoes and they are working ok. This if statement also works ok with or with out them now? :?:


Code that works is

Code: Select all

// Chech to see if membership type is Family, if ttrue return all family members name and call in HTML table.
// Get a specific result from the table
if ($mem_type == 'Family')
{$result = mysql_query("SELECT pn_name, pn_uname FROM _users WHERE barc_id='$barc_id'") or die(mysql_error());
 echo "<br /><table width=\"250\"  border=\"0\" cellspacing=\"0\" cellpadding=\"2\"><caption><b>All Family Members</b></caption>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table><br />\n";
}
?>

Posted: Sat Nov 11, 2006 4:30 pm
by John Cartwright
add error_reporting(E_ALL); to see the errors being generated by not using quotes..

Code: Select all

$var = FooBar; //constant
$var = 'FooBar'; //string

Posted: Sat Nov 11, 2006 4:30 pm
by aaronhall
It's good practice to enclose string literals in quotes. PHP is smart enough to figure out what you meant, but if you had PHP set to display all errors, you would get a notice saying that you were making a call to an undefined constant.

EDIT: whoops, Jcart got to it first

Posted: Sat Nov 11, 2006 4:34 pm
by me!
Got it!

Thanks guys, and that was the fastest replys I have evere seen in a forum :!: