need help with an IF

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

Post Reply
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

need help with an IF

Post 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";
?>
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

if ($mem_type == 'Family')
Strings need quoting ;)
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You also need to enclose the entire block in curly brackets
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

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

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post by me! »

Got it!

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