Function causing Buffer Error

I am currently working on an online Survey, and having issues with a Buffer Error.

In my HTML section, I send the QuestionID - from a recordset which I retrieved earlier in my PHP section - to a function which retrieves the Question-Stem and generates the Answer Options based on the Question-Type (e.g. Yes/No, True/False, Multiple-Choice, etc.).

Here is that code…


	<form id="rateThisArticle" action="" method="post">
		<fieldset>
			<legend>Rate This Article</legend>
			<ul>
				<?php
					// Set Question Number.
					$questionNo = 1;


					// **************************
					// Generate Survey Items.		*
					// **************************
					while (mysqli_stmt_fetch($stmt3)){
						generateSurveyItem($dbc, $questionNo, $articleSurveyQuestionID);

						// Increment Question Number.
						$questionNo = $questionNo + 1;
					}//End of GENERATE SURVEY ITEMS
				?>

				<!-- Submit Form -->
				<li id="buttons">
					<input type='submit' name='submit' class='button' value='Rate This Article' />
					<input type='reset' class='button' value='Cancel' />
				</li>
			</ul>
		</fieldset>
	</form>

The problem is that in the Function - which is in a separate script - I try to display an Error-Message if the Query which retrieves the Question and Answers fails.

Here is a snippet…


	//****************************************************************************
	function generateSurveyItem($dbc, $questionNo, $articleSurveyQuestionID){
		// Initialize Variables.
		$sessMemberID = (isset($_SESSION['sessMemberID']) ? $_SESSION['sessMemberID'] : '');

		// ********************************
		// Attempt to Retrieve Item Info.	*
		// ********************************

		// Build query.
		$q1 = "SELECT stem, question_type, label_lower, label_upper
						FROM article_survey_question
						WHERE id=?";

		// Prepare statement.
		$stmt1 = mysqli_prepare($dbc, $q1);

		// Bind variable to query.
		mysqli_stmt_bind_param($stmt1, 'i', $articleSurveyQuestionID);

		// Execute query.
		mysqli_stmt_execute($stmt1);

		// Store results.
		mysqli_stmt_store_result($stmt1);

		// Check # of Records Returned.
		if (mysqli_stmt_num_rows($stmt1)==2){    //TRY TO CREATE ERROR (should be ==1)
			// Item Found.

			// Bind result-set to variables.
			mysqli_stmt_bind_result($stmt1, $questionStem, $questionType, $labelLower, $labelUpper);

			// Fetch record.
			mysqli_stmt_fetch($stmt1);

		}else{
			// Item Not Found.
			$_SESSION['resultsCode'] = 'RATE_ARTICLE_QUESTION_NOT_FOUND_2385';
//			$resultsCode = 'RATE_ARTICLE_QUESTION_NOT_FOUND_2385';

			// Set Error Source.
			$errorPage = $_SERVER['SCRIPT_NAME'];

			// Redirect to Display Outcome.
			header("Location: " . BASE_URL . "/account/results.php");
			// Log Function Error.
//			logError($dbc, $resultsCode, $errorPage, $sessMemberID);

			// End script.
			exit();

		}//End of ATTEMPT TO RETRIEVE ITEM INFO

		// Rest of Function below...

As you can see from the commented-out lines above in my Function, I was originally just logging the error, but then I realized that my website cannot return Questions and Answers then I need to not only log things, but display an Error-Message like “Sorry, a System Error occurred and the Survey cannot be displayed.”

I typically do a good job of putting the logic up in my PHP section, but I am not seeing how to do it here, since I need to call the Function as I loop through the QuestionID’s I found earlier, and that is breaking my Error-Message if my Function fails?!

Can someone help me figure this out??

Thanks,

Debbie

Change how your function works and move it the loop above your HTML output.

So instead of using echo or print inside your function to write the HTML as it is called, return the HTML output as a string, and return it. Your loop would concatenate it into an $output variable which is then echo’d to the screen later on.

This way when an error occurs, you can redirect to an error page, or potentially numerous other options.

But as I showed, I am doing that now.

However, because I have already printed the Page Heading and so on I will still get an error even with what you are saying.

As far as I know, I have two options…

1.) You Output Buffering or whatever that is called. (But I hear that is hokey…)

2.) I am wondering if I can build my Question/Answer HTML and store it in an Array in my PHP section. Then, if there is an error, I can redirect to my Error Page, and if there isn’t, I could then loop through the “valid” HTML in my array in my HTML section.

What do you think about that?

Debbie

I did a combination of what you said and what I said…

First I took my query results and sent each record to my Function to get the HTML code - which is now assigned to a variable - and then I stored it in an Array…


	// *************************************************************
	// HANDLE FORM.										 *
	// *************************************************************
	if ($_SERVER['REQUEST_METHOD']=='POST'){
		// Form was Submitted (Post).


	}else{
		// Form was not Submitted (Get).

		// ******************
		// Populate Form.	*
		// ******************

			// **************************
			// Generate Survey Items.		*
			// **************************
			while (mysqli_stmt_fetch($stmt3)){
				$surveyItemArray[$articleSurveyQuestionID] = generateSurveyItem($dbc, $questionNo, $articleSurveyQuestionID);

				// Increment Question Number.
				$questionNo = $questionNo + 1;
			}//End of GENERATE SURVEY ITEMS


Next, in my HTML section I looped through the Array - which contains HTML created from already validated Questions - and echoed it out.


	<ul>
		<?php
			foreach($surveyItemArray as $articleSurveyQuestionID => $surveyItem){
				echo $surveyItem;
			}
		?>

		<!-- Submit Form -->
		<li id="buttons">
		<input type='submit' name='submit' class='button' value='Rate This Article' />
		<input type='reset' class='button' value='Cancel' />
		</li>
	</ul>

That seems to have solved everything.

Thanks,

Debbie