When your grabbing a result set from MySQL (or whatever db server) set up the array variable before the start of the while loop, using the code in your post as an example, between the line where the query is run and the first line of the while loop you need to add:
$status=array();
That would set up status as an empty array ready to have the result set added to it.
From the looks of your code you might not actually need an array. If all you need is to look up a member and see what status (user group?) they’re in then you only need to get the status of the user concerned:
$query = "
SELECT
status
FROM
members
WHERE
username = '$user'
";
$res = mysql_query($query);
$users_matched = mysql_num_rows($res);
if ( $users_matched <> 1 ) {
// Handle error state of no match for the username
echo "ERROR: Unable to located $user in the database!";
die;
}
$row = mysql_fetch_assoc($res);
$status = $row['status'];
if ($status === 1) {
echo "<div align='left'>You are a site admin.</div>";
$res = mysql_query("SELECT * FROM pcourse");
$num_rows = mysql_num_rows($res);
if ($num_rows == 1) {
echo "There is $num_rows pending course. <a href='pending.php'>View</a>";
} elseif ($num_rows == 0) {
echo "There are $num_rows courses pending. <a href='pending.php'>View</a>";
} elseif ($num_rows > 1) {
echo "There are $num_rows pending courses. <a href='pending.php'>View</a>";
}
}
Thanks spacePhoenix…That worked and yes the status is the (user group) either 0 or 1. The default status is 0 when a user registers if I change their status to 1 I am allowing that user to edit courses and approve/deny user submitted courses. On the pages where a member must be a admin I am using this if statement
$res = mysql_query("SELECT * FROM members where username = '$user'");
while ($row=mysql_fetch_array($res)) {
$status = $row['status'];};
if ($status == 1) {// code and form that needs approved }
else {
echo "You can not make changes to the course. You are being redirected back.";
header("Refresh: 8; url='\\condition\\index.php'");};
Is that a good method to use? Or would it be better to just start another session like user when they log in?
Where is the value for user coming from? If it’s coming from the $_SESSION array then possibly you could store the value of status as a session variable then grab the value of status from the session instead, remembering to update the value of status in the session if the user is promoted to an admin or is an admin who is demoted to a member.