Jump to content

Problem outputting Multi-Dimensional Array


doubledee

Recommended Posts

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?!

 

:shrug:

 

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.