I have an Associative Array called $sqldata. This array looks like:
我有一个名为$ sqldata的关联数组。这个数组看起来像:
{"assignment":"1","grade":"11.00000"}
My Code:
我的代码:
function displayfetchAssignGrades($sqldata) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "moodle";
$sqlr = array();
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
foreach ($sqldata as $x => $sqld) {
$sqlqueryinfo = $sqld;
$sql = "SELECT name FROM mdl_assign WHERE id='$sqlqueryinfo'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$row["grade"] = $sqldata["grade"][$x];
$sqlr = $row;
}
} else {
echo "";
}
}
echo json_encode($sqlr);
mysqli_close($conn);
}
What I want is as the foreach loop iterates I want the name of the assignment received from the SQL Query and the grade (from the original $sqldata array) in another associative. The order of the grades values corresponds to the Assignment Name. So the final array should look like in JSON:
我想要的是当foreach循环迭代我希望从SQL查询接收的赋值的名称和另一个关联中的等级(来自原始的$ sqldata数组)。成绩值的顺序对应于作业名称。所以最终的数组看起来应该像JSON:
{"assignment name":"Task 1","grade":"11.00000"}
1 个解决方案
#1
0
Try this:
尝试这个:
$newArr = [];
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$newArr[] = array(
'assignment name' => 'Task ' . $row["assignment"],
'grade' => $row["grade"]
);
}
}
$json = json_encode($newArr, true);
#1
0
Try this:
尝试这个:
$newArr = [];
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$newArr[] = array(
'assignment name' => 'Task ' . $row["assignment"],
'grade' => $row["grade"]
);
}
}
$json = json_encode($newArr, true);