配列内のアイテムに対して 2 番目のクエリを実行しようとしています。まず、ユーザー情報の配列を作成しました。次に、ユーザーごとに、回答テーブルにある回答を照会する必要があります。
アイデアは、配列が最初のクエリからのデータを持ち、回答からのデータを関連するユーザー要素に単純に追加することです。以下のコードは、最初のシステムのみをリストしています。
ここに質問を投稿するのはとても気が進まなかったのですが、私の 1 日はこれに費やされてしまい、どうしようもありません。私の明らかに欠陥のあるアプローチについて事前にお詫び申し上げます。
Array
(
[0] => Array
(
[fname] => asdf
[lname] => asdf
[minitial] => a
[rank] => MAJ
[uniq] => !s5$qn
[sysName] = System 1 Name
[choice] = The choice for This named system
[priority] = The priority
[termcom] = The termcom
[sysName] = System 2 Name
[choice] = The choice for This named system
[priority] = The priority
[termcom] = The termcom
[sysName] = System 3 Name
[choice] = The choice for This named system
[priority] = The priority
[termcom] = The termcom
[1] => Array
(
[fname] => asdf
[lname] => lkjlkj
[minitial] => i
[rank] => oiuoi
[uniq] => @z26dr
[sysName] = System 1 Name
[choice] = The choice for This named system
[priority] = The priority
[termcom] = The termcom
[sysName] = System 2 Name
[choice] = The choice for This named system
[priority] = The priority
[termcom] = The termcom
[sysName] = System 3 Name
[choice] = The choice for This named system
[priority] = The priority
[termcom] = The termcom
)
//コード
$sql = "SELECT fname, lname, minitial, rank, uniq FROM `user` join answers on answers.uniqid = user.uniq";
$data = mysqli_query($con, $sql) or die("MySQL ERROR: ". mysqli_error($con));
$users = array();
$i = 0;
while ($row = mysqli_fetch_array($data, MYSQL_ASSOC))
{
$users['answers'][$i] = array (
"fname" => $row['fname'],
"lname" => $row['lname'],
"minitial" => $row['minitial'],
"rank" => $row['rank'],
"uniq" => $row['uniq']
);
$query2 = "SELECT a.sysid, s.sysName, uniqid, choice, priority, termcom FROM answers a LEFT JOIN systems s ON s.sysID = a.sysid WHERE a.uniqid = '" . $row['uniq'] . "'";
$data2 = mysqli_query($con, $query2);
while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM))
{
$users['answers'][$i]['sysName'] = $row2[1];
$users['answers'][$i]['choice'] = $row2[3];
}
$i++;
}
共有できる洞察を事前にありがとうございます。
編集:これは戻ってくるアレイであり、各ユーザーについて最初のシステムのみがリストされています。
[2] => Array
(
[fname] => asdf
[lname] => lkjlkj
[minitial] => i
[rank] => oiuoi
[uniq] => @z26dr
[sysName] => Super Terminate System
)
[3] => Array
(
[fname] => Juuu
[lname] => kjuuu
[minitial] => k
[rank] => LTC
[uniq] => gthdz%
[sysName] => Super Terminate System
)