Skip to content
Advertisement

How to fix “Array to string conversion” to display content from table as a checkbox description

I am creating a table with content fed from a table in a MySQL Database. One of the fields on the table is a checkbox form that adds roles to the user. I got it to work, but with the description being hardcoded. Since a new role might get added, I want to show it as a new checkbox option.

Clarification: the value of 1 sets the role as Admin, 2 as Subscriber…

The problem is the message: Notice: Array to string conversion in /var/www/html/wordpress/multi_users/user_table.php on line 110

<?php
$records_roles = mysqli_query($connect, "SELECT role_name FROM tbl_roles");
$roles = array();
$count = 0;
while ($course_roles = mysqli_fetch_assoc($records_roles)){
    $roles []= $course_roles;
    $count++;
} 
?>

<!DOCTYPE html>
<html lang="en">
<html>
<head>
    <title>Modify users</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="jquery-tabledit-1.2.3/jquery.tabledit.min.js"></script>
</head>

<body>
<div class="container">
    <br />
    <br />
    <br />
    <div class="table-responsive">
        <h3 align="center">Modifying Users</h3><br />
        <table id="editable_table" class="table table-bordered table-striped">
            <thead>
            <tr>
                <th>ID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Approval</th>
                <th>Roles</th>
            </tr>
            </thead>
            <tbody>
            <?php
            while($row = mysqli_fetch_array($result))
            {
                echo '
              <tr>
               <td>'.$row["user_id"].'</td>
               <td>'.$row["first_name"].'</td>
               <td>'.$row["last_name"].'</td>
               <td>'.$row["email"].'</td>
               <td>
                    <form method="post">
                        <input type="hidden" name="user_id" value='.$row["user_id"].'>

                        <select name="admin_approved">
                            <option disabled selected value>--Select an option--</option>
                            <option value="1">Approved</option>
                            <option value="0">Dissaproved</option>
                        </select>
                        <button type="submit" name="selectSubmit" >Submit</button>
                    </form>
               </td>
               
               <td>
                    <form method="post">
                        Select user roles<br/>
                        <input type="hidden" name="user_id" value='.$row["user_id"].'>
               ';


                for ($a = 1; $a <= $count ; $a++){
                    echo '<input type="checkbox" name="techno[]" value="$a" />' .$roles[($a-1)]. "<br>";
                }// line 110

                echo '
                    </form>
                    </td>
                    </tr>
                ';
            }
            ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

Advertisement

Answer

Your issue stems from here:

while ($course_roles = mysqli_fetch_assoc($records_roles)){
    $roles []= $course_roles;
    $count++;
} 

$course_roles is an array of column values, even though you’re only retrieving one column, so $roles really looks like

[
    ['role_name' => 'first_role_name'],
    ['role_name' => 'second_role_name']
]

What you need to do is get just the one column in that loop

while ($course_roles = mysqli_fetch_assoc($records_roles)){
    $roles []= $course_roles['role_name'];
    $count++;
} 
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement