doubledee Posted April 18, 2012 Share Posted April 18, 2012 I have a Q&A Form/script where registered Users can share there thoughts on 10 Questions. On the backend I have these tables: member, answer, question Thanks to some help last night, I have been able to do a LEFT join and store the above data in a multi-dimensional array which always stoes the 10 Questions, and also any Answers that the User may have responded to. (Questions are optional.) I am having a hell of a time DISPLAYING this multi-dimensional array. The key is that I want my PHP and array to stay at the top of my script and use minimal PHP in my HTML section. Here is how I query the data and 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>'; // Close prepared statement. mysqli_stmt_close($stmt7); And in my HTML Form, here is how I am attempting to dynamically build the Q&A section... <form id="changeAnswers" action="" method="post"> <fieldset> <legend>Change Profile Answers</legend> <?php echo '<p>TEST: ' . $thoughtsArray[4]['answerText'] . '</p>'; 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 and display the Questions from my multi-dimensional array as seen above, however, for the life of me, I cannot get the Input Fields (with any Answers) to work properly. I either get compile errors, or the code runs but my Input Fields are blank even when an Answer should be displayed?! I am hoping this is just a syntax and/or reference error. I'm still new to arrays, so properly retrieving an Answer from a multi-dimensional array is really tricky for me!! So close and yet so far away?! Can someone please help me out?? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/ Share on other sites More sharing options...
batwimp Posted April 18, 2012 Share Posted April 18, 2012 If you display the page in your browser, then do a View Source, what does the HTML look like? Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/#findComment-1338518 Share on other sites More sharing options...
doubledee Posted April 18, 2012 Author Share Posted April 18, 2012 If you display the page in your browser, then do a View Source, what does the HTML look like? I changed the second echo and it is broken now, so I can't do that. As far as I can tell, I just need to get the right syntax with all those crazy single and double quotes, AND I need to use the right syntax to grab the right thing out of my multi-dimensional array. That is what I need help with... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/#findComment-1338519 Share on other sites More sharing options...
batwimp Posted April 18, 2012 Share Posted April 18, 2012 So you're not seeing your form at all? Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/#findComment-1338520 Share on other sites More sharing options...
doubledee Posted April 18, 2012 Author Share Posted April 18, 2012 So you're not seeing your form at all? As I said, I see the Form and Questions, but get a parse error if I don't comment out the second echo which tries to display my Answers. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/#findComment-1338522 Share on other sites More sharing options...
batwimp Posted April 18, 2012 Share Posted April 18, 2012 Are you trying to display text from the array in the text area, or just take it as an input to later throw into a database? Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/#findComment-1338523 Share on other sites More sharing options...
doubledee Posted April 18, 2012 Author Share Posted April 18, 2012 Are you trying to display text from the array in the text area, or just take it as an input to later throw into a database? This block of code is just supposed to dynamically DISPLAY the Questions and any Answers. (I have a whole set of other code that takes care for Form Input.) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/#findComment-1338525 Share on other sites More sharing options...
batwimp Posted April 18, 2012 Share Posted April 18, 2012 echo <<<EOT <textarea id="question{$questionNo} name="{$thoughtsArray[$questionNo]['answerText']}" cols="60" rows="2"> EOT; echo isset($questionNo) ? htmlentities($thoughtsArray[$questionNo]['answerText'], ENT_QUOTES) : ''; echo "</textarea>"; Quote Link to comment https://forums.phpfreaks.com/topic/261183-problem-outputting-multi-dimensional-array/#findComment-1338528 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.