1

I just try to get my own user management tool, so first of all I just want to display all users by using a table. Then I want to allow to change the roles of the users by using <select> tags. My problem is that I want to get the current role of every user pre-selected and I want to have the possibility to change by getting an array of the possible roles by searching database. But how is this possible? Here you can see my code:

<?php

header('Content-Type: text/html; charset=UTF-8');
include("db.php"); //just the database connection
session_start();
?>

<html>
    <head>
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    </head>
    <body>
</html>
<?php
    $abfrage= mysql_query("SELECT * FROM user ORDER BY Name desc");
    //$ergebnis = mysql_query($abfrage) or die( mysql_error() ); 
    echo "<table>";
    echo"<caption>Mitglieder<br></caption>";
    echo"<table border=\"1\" style=\"width:300px\">";
    echo "<th>Name</th>
          <th>Prename</th>
          <th>Role</th>";

    while($row = mysql_fetch_object($abfrage)) {

        echo "<tr>";
        echo "<td align=center>",$row->Name,"</td>";
        echo "<td align=center>",$row->Prename,"</td>";
        echo "<td align=center><select><option value=",$row->Role,">",$row->Role,"</option></select></td>";
        //Here is the Problem

        echo "</tr>";
    }
    echo "</table>";
?>

It displays the table with the users and their role from database correctly. Bur I need to get the other roles selectable. I hope someone can help me.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Headjordan
  • 19
  • 6
  • 2
    You're only outputting their role as an option because it's the only one you have access to. You need to have an array of selectable roles, and assign `selected="true"` to their actual role when you're displaying the rest. – jhmckimm Jan 26 '16 at 01:20
  • Thank you for the fast answer. How can I loop the Array to get every role? – Headjordan Jan 26 '16 at 01:22
  • 1
    Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Jan 26 '16 at 01:32
  • Where are you keeping these Roles on your database. The user will I assume have ONE Role, but if you want to get ALL the Roles from the database they need to be stored somewhere and then SELECTed seperatly to the Users – RiggsFolly Jan 26 '16 at 01:35
  • Every user has one role: author, admin, reader. I just want to get the current role of every User preselected in the – Headjordan Jan 26 '16 at 01:38

2 Answers2

3

EDIT - ADD SQL QUERY:

$available_roles = [];
$available_roles_query = mysql_query("SELECT * FROM roles");

while($role = mysql_fetch_object($available_roles_query)){

    $available_roles[] = $role->Name;
}

ORIGINAL POST:

while($row = mysql_fetch_object($abfrage)): ?>
   <tr>
       <td align="center"><?php echo $row->Name; ?></td>
       <td align="center"><?php echo $row->Prename; ?></td>
       <td align="center">
           <select>
               <option value="none">Please Select:</option>
               <?php foreach($available_roles as $role): ?>
                   <?php if($row->Role == $role): ?>
                      <option value="<?php echo $role; ?>" selected="true"><?php echo $role; ?></option>
                   <?php else: ?>
                       <option value="<?php echo $role; ?>"><?php echo $role; ?></option>
                   <?php endif; ?>
               <?php endforeach; ?>
           </select>
    </tr>
<?php endwhile;

Anything you don't understand, let me know. I'm too tired to write out explanations for the most probable issues you might have.

P.S. This won't allow you to update values. You'll need to add that functionality yourself, and when you do I'd recommend using different keys for each role than its name.

jhmckimm
  • 870
  • 6
  • 15
  • You can instead use `= "Hi" ?>` to echo things, and it'll save you tracking down missing semicolons on ``-like statements, but not a lot of people seem to know about it so I'm going to keep the sanity in check and only post a comment instead of *actually* using it. – jhmckimm Jan 26 '16 at 01:35
  • He wants to get the ROLES from the database? Not a hard coded array! – RiggsFolly Jan 26 '16 at 01:36
  • @RiggsFolly From his original question it's clear he knows how to query a database. Why would I tell him something he knows? Was it you that downvoted? – jhmckimm Jan 26 '16 at 01:38
  • He quite obviously does not know what he is doing and giving him a solution he has already said he does not want, is a waste of disc space, see [Thank you, but is it possible to get the Array dynamically from the database?](http://stackoverflow.com/questions/35005504/how-to-get-option-value-as-an-array-out-of-database/35005632?noredirect=1#comment57739113_35005590) – RiggsFolly Jan 26 '16 at 01:40
  • @RiggsFolly I've edited to include the SQL query. Please remove your downvote. – jhmckimm Jan 26 '16 at 01:45
  • @RiggsFolly But, of course, it won't be accurate because I don't know the name nor structure of his roles table. – jhmckimm Jan 26 '16 at 01:46
  • And that is why we have comments. We ask questions to clarify whatever is missing from the original question, and then when we know what is actually required, we give an answer – RiggsFolly Jan 26 '16 at 01:51
  • Now thats quite a nice answer. DV Removed and a UV Added, well done – RiggsFolly Jan 26 '16 at 01:53
0

1.you need get all roles as array

$roles = ['admin', 'user', .....]; // or you can get roles from database

2.loop the roles

$options = "<option value=".$row->Role.">".$row->Role."</option>";
foreach( $roles as $role ){
    $options .= "<option value={$role}>{$role}</option>";
}
echo "<td align=center><select>{$options}</select></td>";
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
suibber
  • 267
  • 1
  • 6
  • Thank you, but is it possible to get the Array dynamically from the database?, because I want the admin to dynamically Name the roles, store them into database and get them out in a tag – Headjordan Jan 26 '16 at 01:31
  • $re= mysql_query("select role_name from role_table where 1"); – suibber Jan 26 '16 at 01:38
  • $roles = [];while( $data = mysql_fetch_assoc($re) ){$roles[]=$data['role_name'];} – suibber Jan 26 '16 at 01:40