forcing a var to be something else

Not sure if this belongs here or in the mysql section…

I have a table,
id
name
class_1
hide

and i am listing all names with their associated class

Quote
Class 1
names…

Class 2
names… ect

the problem i am having is i need to force a users class to “None” if thier hide field is “1”
this is what i have done and its not working … i have commented the code that is not working, as for the rest it is working great.
Data:
[table]
[tr]
[td]name[/td]
[td]class_1[/td]
[td]hide[/td]
[/tr]
[tr]
[td]john[/td]
[td]Option A[/td]
[td]0[/td]
[/tr]
[tr]
[td]bob[/td]
[td]Option B[/td]
[td]0[/td]
[/tr]
[tr]
[td]chris[/td]
[td]Option C[/td]
[td]0[/td]
[/tr]
[tr]
[td]tom[/td]
[td]None[/td]
[td]0[/td]
[/tr]
[tr]
[td]sam[/td]
[td]Option A[/td]
[td]1[/td]
[/tr]
[/table]

Output Now:

[code]Option A
john
sam

Option B
bob

Option C
chris

None
Tom[/code]

Output i want:

[code]Option A
john

Option B
bob

Option C
chris

None
Tom
sam[/code]

[php]<?php
include("./sql-conn.php");
function outputClassList($class1, $users)
{
if (!$class1) {
return false;
}
$userCount = count($users);
$output = “
\n{$class1} ({$userCount})”;
$output .= “
\n”;
$output .= implode("
\n", $users);
$output .= “
\n”;
return $output;
}
$db = mysql_connect($hostname, $username, $password) or die('Failed to connect to database: ’ . mysql_error());
mysql_select_db($database);
$query = “SELECT class_1, name, hide FROM class ORDER BY class_1, name ASC” or die('Failed to Query: ’ . mysql_error());
$result = mysql_query($query) or die('Failed Getting Results: ’ . mysql_error());
$class1 = false;
while ($row = mysql_fetch_assoc($result)) {
if ($class1 != $row[“class_1”]) {
echo outputClassList($class1, $users);

            //this isn't working
            if ($row['hide'] == "1") {
                    $class1 = "None";
            }
            //end
            $class1 = $row['class_1'];
            $users  = array();
    }
    $users[] = ucfirst($row['name']);

}
//print_r ($users);
echo outputClassList($class1, $users);
mysql_free_result($result);
mysql_close($db);
?>[/php]

$class1 = $row[‘class_1’];

This needs to be before your if statement for ‘hide’. Otherwise you just overwrite the $class1 var again with the original value.

[php]
$class1 = $row[‘class_1’];
if ($row[‘hide’] == “1”) {
$class1 = “None”;
}
[/php]

This could also be done with ternary

[php]
$class1 = ($row[‘hide’] == “1” ? “None” : $row[‘class_1’]);
[/php]

One more method would be if/else.

[php]
if ($row[‘hide’] == “1”) {
$class1 = “None”;
}
else {
$class1 = $row[‘class_1’];
}
[/php]

The if/else is unnecessary and (I think) looks bad, but it functions the same as the other two. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service