I have a multi-dimensional array, and am having a world of trouble getting the values I need to display in my form.
In the top half of my script is PHP, and here is how I build my array…
// ********************
// Build Q&A Query. *
// ********************
// Build query.
$q7 = "SELECT q.id, q.question_no, q.question_text, a.answer_text
FROM bio_question AS q
LEFT JOIN bio_answer AS a
ON q.id=a.question_id
AND a.member_id =?
ORDER BY q.question_no";
// Prepare statement.
$stmt7 = mysqli_prepare($dbc, $q7);
// Bind variables to query.
mysqli_stmt_bind_param($stmt7, 'i', $memberID);
// Execute query.
mysqli_stmt_execute($stmt7);
// Store results.
mysqli_stmt_store_result($stmt7);
// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt7) > 0){
// Questions Found.
// Bind result-set to variable.
mysqli_stmt_bind_result($stmt7, $questionID, $questionNo, $questionText, $answerText);
// Populate PHP array with Questions & Answers.
$x=1;
while (mysqli_stmt_fetch($stmt7)){
$thoughtsArray[$x] = array('questionID' => $questionID,
'questionText' => $questionText,
'answerText' => $answerText);
$x=$x+1;
}
echo '<pre>';
print_r($thoughtsArray);
echo '</pre>';
When I run that the array looks like this…
Array
(
[1] => Array
(
[questionID] => 2
[questionText] => Why did you decide to start your own business?
[answerText] =>
)
[2] => Array
(
[questionID] => 4
[questionText] => What advice would you share with others on what NOT to do?
[answerText] =>
)
[3] => Array
(
[questionID] => 3
[questionText] => What advice would you share with others on what TO do?
[answerText] =>
)
[4] => Array
(
[questionID] => 7
[questionText] => How do you compete against large corporations?
[answerText] => By knowing my Customers!
)
[5] => Array
(
[questionID] => 5
[questionText] => What Business Structure do you have?
[answerText] =>
)
)
You’ll notice in the Question #4, there is an Answer that I ultimately need displayed in my Form…
Down in my HTML, I am trying to loop through the array and display the Question and then the Answer in an HTML Input Field.
Here is that code…
<form id="changeAnswers" action="" method="post">
<fieldset>
<legend>Change Profile Answers</legend>
<?php
foreach($thoughtsArray as $questionNo => $qaArray) {
echo '<label for="question' . $questionNo . '">' . $questionNo . '.) ' . $qaArray['questionText'] . '</label>';
echo '<textarea id="question' . $questionNo . '" name="thoughtsArray[' . $questionNo . '][\\'answerText\\']" cols="60" rows="2">' .
(isset($questionNo]) ? "htmlentities($thoughtsArray[$questionNo]['answerText'], ENT_QUOTES" : '') .
'</textarea>';
}
?>
I am able to retrieve each Question from my array, and display it.
But when I try to retrieve each Answer, I either cannot get the syntax right and I get a compile error, OR I get it to run, but no Answer will appear even though - as shown above - I do have an Answer for Question #4.
One last thing…
If I add this line of code it will display Answer #4…
echo '<p>TEST: ' . $thoughtsArray[4]['answerText'] . '</p>';
But if I add this line of code nothing appears…
<textarea id="question4" name="thoughtsArray[4]['answerText']" cols="60" rows="2"></textarea>
So the problem clearly exists in my second echo statement in my ForEach loop…
Any ideas what I am doing wrong?!
Thanks,
Debbie