如何修复“数组到字符串的转换”以将 table 中的内容显示为复选框描述
How to fix “Array to string conversion” to display content from table as a checkbox description
我正在创建一个 table,其内容来自 MySQL 数据库中的 table。 table 上的字段之一是一个复选框表单,用于向用户添加角色。我让它工作,但描述是硬编码的。由于可能会添加新角色,因此我想将其显示为新的复选框选项。
澄清:值 1 将角色设置为管理员,2 设置为订阅者...
问题是消息: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>
您的问题源于此:
while ($course_roles = mysqli_fetch_assoc($records_roles)){
$roles []= $course_roles;
$count++;
}
$course_roles
是一个列值数组,即使您只检索一列,所以 $roles 确实看起来像
[
['role_name' => 'first_role_name'],
['role_name' => 'second_role_name']
]
您需要做的是只获取该循环中的一列
while ($course_roles = mysqli_fetch_assoc($records_roles)){
$roles []= $course_roles['role_name'];
$count++;
}
我正在创建一个 table,其内容来自 MySQL 数据库中的 table。 table 上的字段之一是一个复选框表单,用于向用户添加角色。我让它工作,但描述是硬编码的。由于可能会添加新角色,因此我想将其显示为新的复选框选项。
澄清:值 1 将角色设置为管理员,2 设置为订阅者...
问题是消息: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>
您的问题源于此:
while ($course_roles = mysqli_fetch_assoc($records_roles)){
$roles []= $course_roles;
$count++;
}
$course_roles
是一个列值数组,即使您只检索一列,所以 $roles 确实看起来像
[
['role_name' => 'first_role_name'],
['role_name' => 'second_role_name']
]
您需要做的是只获取该循环中的一列
while ($course_roles = mysqli_fetch_assoc($records_roles)){
$roles []= $course_roles['role_name'];
$count++;
}