unknown gamer Posted October 6, 2008 Share Posted October 6, 2008 Ok, I have this file that echos delete if the status isn't Administrator or Moderator. it's not exactly working. here it is: <?php$result = mysql_query("SELECT * FROM users");echo "<table border='1'><tr><th>id</th><th>username</th><th>status</th><th>Delete</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . "<a href=\"view.php?name={$row['username']}\">{$row['username']}</a>" . "</td>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" .$status="";if ($row['status'] == "Administrator") {$status = 1; // if an admin, set $status to one/true}if ($status== 1) {echo 'Admin';}if ($row['status'] == "Moderator") {$status = 2; // if an admin, set $status to one/true}if ($status== 2) {echo 'Mod';}if ($row['status'] != "Moderator") {$status = 3; // if an admin, set $status to one/true}if ($status== 3) {echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";}"</td>"; echo "</tr>"; }echo "</table>";?> That works, but it echos delete for admins, so i add the same code but for admins: <?php$result = mysql_query("SELECT * FROM users");echo "<table border='1'><tr><th>id</th><th>username</th><th>status</th><th>Delete</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . "<a href=\"view.php?name={$row['username']}\">{$row['username']}</a>" . "</td>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" .$status="";if ($row['status'] == "Administrator") {$status = 1; // if an admin, set $status to one/true}if ($status== 1) {echo 'Admin';}if ($row['status'] == "Moderator") {$status = 2; // if an admin, set $status to one/true}if ($status== 2) {echo 'Mod';}if ($row['status'] != "Moderator") {$status = 3; // if an admin, set $status to one/true}if ($status== 3) {echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";}if ($row['status'] != "Administrator") {$status = 4; // if an admin, set $status to one/true}if ($status== 4) {echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";}"</td>"; echo "</tr>"; }echo "</table>";?> then that echos Delete for all the users including admins and mods. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 6, 2008 Share Posted October 6, 2008 if ($row['status'] != "Moderator") {$status = 3; // if an admin, set $status to one/true}if ($status== 3) {echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";} It's working exactly like you're telling it to. If they are not a moderator, echo the delete link. That's exactly what that code is doing. You're not testing whether or not they are an admin, only a moderator. Link to comment Share on other sites More sharing options...
unknown gamer Posted October 6, 2008 Author Share Posted October 6, 2008 I don't understand. Why is there a link to the delete thing on all users?and to check i the user is not an admin I have the same code but changed Moderator to Administrator. it makes no sense to me. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 7, 2008 Share Posted October 7, 2008 Why is there a link to the delete thing on all users?Because that's what the code is telling it to do. You have this code block:if ($row['status'] != "Moderator") {$status = 3; // if an admin, set $status to one/true}if ($status== 3) {echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";} That says to output the delete link if they are not a moderator. Right under that you have this: if ($row['status'] != "Administrator") {$status = 4; // if an admin, set $status to one/true}if ($status== 4) {echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";} That says to output the delete link if they are not an admin. So you are telling it to print a delete link if they are not a moderator, and also if they are not an admin. Since no one is both a moderator and an admin, it will print the link for everyone.Your code is not checking if a previous check passed. You check if they are a mod, but then when you go to check if they are an admin you don't take into account whether or not they're a mod, you're only checking if they're an admin. All of your if statements are independent, they don't check to see if a previous if statement passed. That's what an else statement is for, or you can test status the next time you check to see if status already got set. Link to comment Share on other sites More sharing options...
unknown gamer Posted October 7, 2008 Author Share Posted October 7, 2008 Ok I changed it to:$status="";if ($row['status'] == "Administrator" || "Moderator") {echo 'Cannot delete.';}else{echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";} but now they all say cannot delete, this is soo confusing. Can you help me figure out how to properly write it? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 7, 2008 Share Posted October 7, 2008 if ($row['status'] == "Administrator" || $row['status'] == "Moderator") {echo 'Cannot delete.';}else{echo "<a href=\"delete.php?name={$row['username']}\">Delete</a>";} Link to comment Share on other sites More sharing options...
unknown gamer Posted October 7, 2008 Author Share Posted October 7, 2008 Cool, forgot to add the $row['status'] I didn't know you had to do that. Also 1 more thing, how do you check if a user is in the database? I've looked forever to figure it out. <?php $user = "select username from users where username = '$username'"; $result = mysql_query($user); if(!$result){echo "this user doesn't exist! Here is a list of users.";include 'users.php'; }else {echo "Test";}?>that is the closest I could find yet it still doesn't work. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 7, 2008 Share Posted October 7, 2008 if(!mysql_num_rows($result)){ Link to comment Share on other sites More sharing options...
unknown gamer Posted October 7, 2008 Author Share Posted October 7, 2008 It always says this user does not exist... even if the user does exist Link to comment Share on other sites More sharing options...
Synook Posted October 7, 2008 Share Posted October 7, 2008 Are you assigning a value to $username? Link to comment Share on other sites More sharing options...
unknown gamer Posted October 7, 2008 Author Share Posted October 7, 2008 Wooops, forgot to change it from $username to $_GET['name']. That was a stupid mistake....thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.