PHP While Loop using mysql_fetch_array

Hi Guys,

I’m working on creating an online Survey application, but I’m having some trouble with one of the functions. It’s the function to display the questions and answers, and what I’d like is for each page to display all questions that match that category.

The code is below:

function showcategories($row_survey, $row_question, $row_category, $mode)
{
	global $path;
	global $template;
	$count = 0;
	$cat_count = 1;

	// get the template and tags
	$question_text = gettemplate($row_survey, 'vote');
	include($path."includes/tags.inc.php");

	// get special config for this template
	$tpl = $template;
	if ($tpl=="")
		$tpl = $row_survey["template"];
	include($path.'templates/'.$tpl.'/config.inc.php');

	// replace survey and question tags
	$question_text = surveytags($question_text, $row_survey);
	$question_text = categorytags($question_text, $row_category);
	$question_text = questiontags($question_text, $row_question);
	$answer_name = ($mode == SM_ONEPAGE) ? "answer" . $row_question["id"] : "answer";
	
	while ($cat_count = mysql_fetch_array($res_question))
	{
		$cat_count++;
		
		// get the category template
			$start = strpos($question_text, $tags["category_start"]);
			$end = strpos($question_text, $tags["category_end"]);
			if ($start==FALSE || $end==FALSE)
			{
				if ($mode==SM_QUICK)
				{
					echo 'error in template';
					return;
				}
				else
				{
					error($row_category, "error in template");
				}
			}
			$category_tmpl = substr($question_text, $start, $end-$start+strlen($tags["category_end"]));
			
			// build the category
			$cat_build = substr($category_tmpl, strlen($tags["category_start"]), strlen($answer_tmpl)-strlen($tags["category_start"])-strlen($tags["category_end"]));
			$cat_build = str_replace($tags["category_id"],' ', $answer);
			$cat_build = str_replace($tags["category_title"], $row_category["id"], $category);

			// if this is the last answer then we stop adding the template
			if ($count == mysql_numrows($res_question))
				$category_tmpl = "";

			// new string
			$question_text = substr($question_text, 0, $start) . $cat_build . $category_tmpl . substr($question_text, $end+strlen($tags["category_end"]));
	}
		

	if ($row_question["type"] == QT_FREE)
	{
		$start = strpos($question_text, $tags["answer_start"]);
		$end = strpos($question_text, $tags["answer_end"]);

		// build the answer
		$answer_tmpl = substr($question_text, $start, $end-$start+strlen($tags["answer_end"]));
		$answer = substr($answer_tmpl, strlen($tags["answer_start"]), strlen($answer_tmpl)-strlen($tags["answer_start"])-strlen($tags["answer_end"]));
		$answer = str_replace($tags["answer_radio"], "   ", $answer);
		$answer = str_replace($tags["answer_id"], $row_question["id"], $answer);
		$answer = str_replace($tags["answer_text"], '<input type="text" name="'.$answer_name.'" size=40>', $answer);
		$question_text = substr($question_text, 0, $start) . $answer . substr($question_text, $end+strlen($tags["answer_end"]));
	}
	else
	{
		// type of option
		$type = "radio";
		if ($row_question["type"] == QT_MULTI)
			$type = "checkbox";

		// get the list of answers
		$survey_id = $row_survey["id"];
		$question_id = $row_question["id"];
		$res_answer = mysql_query("select * from spherispoll_answers where survey=$survey_id and question=$question_id order by id");
		if ($res_answer == FALSE || mysql_numrows($res_answer) == 0)
			error($row_survey, "error in database - answers");

		while ($row_answer = mysql_fetch_array($res_answer))
		{
			$count++;

			// get the answer template
			$start = strpos($question_text, $tags["answer_start"]);
			$end = strpos($question_text, $tags["answer_end"]);
			if ($start==FALSE || $end==FALSE)
			{
				if ($mode==SM_QUICK)
				{
					echo 'error in template';
					return;
				}
				else
				{
					error($row_survey, "error in template");
				}
			}
			$answer_tmpl = substr($question_text, $start, $end-$start+strlen($tags["answer_end"]));

			// final answer name
			$real_answer_name = $answer_name;
			if ($row_question["type"] == QT_MULTI)
				$real_answer_name = $real_answer_name."_".$count;

			// build the answer
			$answer = substr($answer_tmpl, strlen($tags["answer_start"]), strlen($answer_tmpl)-strlen($tags["answer_start"])-strlen($tags["answer_end"]));
			$answer = str_replace($tags["answer_radio"],' <input type="'.$type.'" name="'.$real_answer_name.'" value="'.$row_answer["id"].'">', $answer);
			$answer = str_replace($tags["answer_id"], $row_answer["id"], $answer);
			$answer = str_replace($tags["answer_text"], $row_answer["answer"], $answer);

			// if this is the last answer then we stop adding the template
			if ($count == mysql_numrows($res_answer))
				$answer_tmpl = "";

			// new string
			$question_text = substr($question_text, 0, $start) . $answer . $answer_tmpl . substr($question_text, $end+strlen($tags["answer_end"]));
		}
	}

	if ($mode == SM_ONEPAGE)
	{
		// remove last tags
		$question_text = str_replace($tags["question_submit"], "", $question_text);

		// link to results
		if ($mode == SM_QUICK)
			$question_text = str_replace($tags["survey_results"], "", $question_text);
	}
	else
	{
		// put the submit image
		$question_text = str_replace($tags["question_submit"], '<input type="image" src="'.$path.$image_vote.'" value="vote" border="0" name="vote" align="absmiddle">', $question_text);

		// link to results
		if ($mode == SM_QUICK)
			$question_text = str_replace($tags["survey_results"], $path.'quickpoll.php?survey='.$row_survey["id"].'&result=1', $question_text);
	}

	// done
	echo $question_text;
}
?>

I know it’s the first while loop that’s messed up, but lack of sleep is making my brain not function! Any help would be really appreciated! Thanks! :slight_smile:

John

To me it looks like $res_question is not set, did you get an error message?


    while ($cat_count = mysql_fetch_array($res_question))
    {
        $cat_count++;

:eek:

mysql_fetch_array does just that - it fetches an array from the mysql result. So why are you then attempting to increment the array as if it is an integer (number)?

You’ve set $cat_count = 1 at the top of the function. You’re then using it again to get each record from the mysql result using mysql_fetch_array. It won’t work. $cat_count is the DATA you’ve just pulled from the database - record by record. You can’t use that one variable for two completely different things.

No error message, $res_question is set outside of the function.

What I’d like to see is:

Category Header
Question 1
Answer 1
Answer 2

Question 2
Answer 1
Answer 2

but what is actually being output is:

Category Header
Question 1
Answer 1
Answer 2

But have you set it as a global so that is available within the function scope?

Also please look at my comments above seeing as you’ve specifically posted about the while() loop. You’ve basically bodged it and I’ve shown you how and you’re now looking at something else instead.

Thanks for the feedback tangoforce, I completely see what you mean.

I’m relatively new to PHP and programming in general. At the top of the function it was set to be $row_question but that was causing problems when I tried requesting the information from the DB.

I’ve changed $cat_count to $cat for now in the top of the function, but it’s made no different to the output :confused:

The $res_question variable is working fine, that’s not the problem.

It has been bodged like I said in the first post, question is where to go from here! Any ideas?

As I said, the $cat_counter variable in the top of the function has been changed.

You need to show your updated code so that we can take a look with fresh eyes rather than guessing what you’ve changed.

function showcategories($row_survey, $row_question, $row_category, $mode)
{
	global $path;
	global $template;
	$count = 0;
	$cat_count = 1;

	// get the template and tags
	$question_text = gettemplate($row_survey, 'vote');
	include($path."includes/tags.inc.php");

	// get special config for this template
	$tpl = $template;
	if ($tpl=="")
		$tpl = $row_survey["template"];
	include($path.'templates/'.$tpl.'/config.inc.php');

	// replace survey and question tags
	$question_text = surveytags($question_text, $row_survey);
	$question_text = categorytags($question_text, $row_category);
	$question_text = questiontags($question_text, $row_question);
	$answer_name = ($mode == SM_ONEPAGE) ? "answer" . $row_question["id"] : "answer";
	
	while ($cat = mysql_fetch_array($res_question))
	{
		$cat_count++;
		
		// get the category template
			$start = strpos($question_text, $tags["category_start"]);
			$end = strpos($question_text, $tags["category_end"]);
			if ($start==FALSE || $end==FALSE)
			{
				if ($mode==SM_QUICK)
				{
					echo 'error in template';
					return;
				}
				else
				{
					error($row_category, "error in template");
				}
			}
			$category_tmpl = substr($question_text, $start, $end-$start+strlen($tags["category_end"]));
			
			// build the category
			$cat_build = substr($category_tmpl, strlen($tags["category_start"]), strlen($answer_tmpl)-strlen($tags["category_start"])-strlen($tags["category_end"]));
			$cat_build = str_replace($tags["category_id"],' ', $answer);
			$cat_build = str_replace($tags["category_title"], $row_category["id"], $category);

			// if this is the last answer then we stop adding the template
			if ($count == mysql_numrows($res_question))
				$category_tmpl = "";

			// new string
			$question_text = substr($question_text, 0, $start) . $cat_build . $category_tmpl . substr($question_text, $end+strlen($tags["category_end"]));
	}
		

	if ($row_question["type"] == QT_FREE)
	{
		$start = strpos($question_text, $tags["answer_start"]);
		$end = strpos($question_text, $tags["answer_end"]);

		// build the answer
		$answer_tmpl = substr($question_text, $start, $end-$start+strlen($tags["answer_end"]));
		$answer = substr($answer_tmpl, strlen($tags["answer_start"]), strlen($answer_tmpl)-strlen($tags["answer_start"])-strlen($tags["answer_end"]));
		$answer = str_replace($tags["answer_radio"], "&nbsp;&nbsp;&nbsp;", $answer);
		$answer = str_replace($tags["answer_id"], $row_question["id"], $answer);
		$answer = str_replace($tags["answer_text"], '<input type="text" name="'.$answer_name.'" size=40>', $answer);
		$question_text = substr($question_text, 0, $start) . $answer . substr($question_text, $end+strlen($tags["answer_end"]));
	}
	else
	{
		// type of option
		$type = "radio";
		if ($row_question["type"] == QT_MULTI)
			$type = "checkbox";

		// get the list of answers
		$survey_id = $row_survey["id"];
		$question_id = $row_question["id"];
		$res_answer = mysql_query("select * from nabopoll_answers where survey=$survey_id and question=$question_id order by id");
		if ($res_answer == FALSE || mysql_numrows($res_answer) == 0)
			error($row_survey, "error in database - answers");

		while ($row_answer = mysql_fetch_array($res_answer))
		{
			$count++;

			// get the answer template
			$start = strpos($question_text, $tags["answer_start"]);
			$end = strpos($question_text, $tags["answer_end"]);
			if ($start==FALSE || $end==FALSE)
			{
				if ($mode==SM_QUICK)
				{
					echo 'error in template';
					return;
				}
				else
				{
					error($row_survey, "error in template");
				}
			}
			$answer_tmpl = substr($question_text, $start, $end-$start+strlen($tags["answer_end"]));

			// final answer name
			$real_answer_name = $answer_name;
			if ($row_question["type"] == QT_MULTI)
				$real_answer_name = $real_answer_name."_".$count;

			// build the answer
			$answer = substr($answer_tmpl, strlen($tags["answer_start"]), strlen($answer_tmpl)-strlen($tags["answer_start"])-strlen($tags["answer_end"]));
			$answer = str_replace($tags["answer_radio"],' <input type="'.$type.'" name="'.$real_answer_name.'" value="'.$row_answer["id"].'">', $answer);
			$answer = str_replace($tags["answer_id"], $row_answer["id"], $answer);
			$answer = str_replace($tags["answer_text"], $row_answer["answer"], $answer);

			// if this is the last answer then we stop adding the template
			if ($count == mysql_numrows($res_answer))
				$answer_tmpl = "";

			// new string
			$question_text = substr($question_text, 0, $start) . $answer . $answer_tmpl . substr($question_text, $end+strlen($tags["answer_end"]));
		}
	}

	if ($mode == SM_ONEPAGE)
	{
		// remove last tags
		$question_text = str_replace($tags["question_submit"], "", $question_text);

		// link to results
		if ($mode == SM_QUICK)
			$question_text = str_replace($tags["survey_results"], "", $question_text);
	}
	else
	{
		// put the submit image
		$question_text = str_replace($tags["question_submit"], '<input type="image" src="'.$path.$image_vote.'" value="vote" border="0" name="vote" align="absmiddle">', $question_text);

		// link to results
		if ($mode == SM_QUICK)
			$question_text = str_replace($tags["survey_results"], $path.'quickpoll.php?survey='.$row_survey["id"].'&result=1', $question_text);
	}

	// done
	echo $question_text;
}

It’s the same as before, just with $cat_count changed to $cat at the mysql_fetch_array function. Thanks!

function showcategories($row_survey, $row_question, $row_category, $mode)
{
	global $path;
	global $template;
	$count = 0;
	$cat_count = 1;

	// get the template and tags
	$question_text = gettemplate($row_survey, 'vote');
	include($path."includes/tags.inc.php");

	// get special config for this template
	$tpl = $template;
	if ($tpl=="")
		$tpl = $row_survey["template"];
	include($path.'templates/'.$tpl.'/config.inc.php');

	// replace survey and question tags
	$question_text = surveytags($question_text, $row_survey);
	$question_text = categorytags($question_text, $row_category);
	$question_text = questiontags($question_text, $row_question);
	$answer_name = ($mode == SM_ONEPAGE) ? "answer" . $row_question["id"] : "answer";
	
	while ($cat = mysql_fetch_array($res_question))
	{
		$cat_count++;
		
		// get the category template
			$start = strpos($question_text, $tags["category_start"]);
			$end = strpos($question_text, $tags["category_end"]);
			if ($start==FALSE || $end==FALSE)
			{
				if ($mode==SM_QUICK)
				{
					echo 'error in template';
					return;
				}
				else
				{
					error($row_category, "error in template");
				}
			}
			$category_tmpl = substr($question_text, $start, $end-$start+strlen($tags["category_end"]));
			
			$res_cat = mysql_query("SELECT * FROM spherispoll_category WHERE id=$category");

			if ($res_cat == FALSE || mysql_numrows($res_cat) !=1)

				error($row_cat, "error in database - category");
	
			$row_cat = mysql_fetch_array($res_cat);
			
			// build the category
			$cat_build = substr($category_tmpl, strlen($tags["category_start"]), strlen($category_tmpl)-strlen($tags["category_start"])-strlen($tags["category_end"]));
			$cat_build = str_replace($tags["category_id"],' ', $cat_build);
			$cat_build = str_replace($tags["category_title"], $row_cat["title"], $cat_build);

			// if this is the last question then we stop adding the template
			if ($cat_count == mysql_numrows($res_question))
				$category_tmpl = "";

			// new string
			$question_text = substr($question_text, 0, $start) . $cat . $category_tmpl . substr($question_text, $end+strlen($tags["category_end"]));
	}
		

	if ($row_question["type"] == QT_FREE)
	{
		$start = strpos($question_text, $tags["answer_start"]);
		$end = strpos($question_text, $tags["answer_end"]);

		// build the answer
		$answer_tmpl = substr($question_text, $start, $end-$start+strlen($tags["answer_end"]));
		$answer = substr($answer_tmpl, strlen($tags["answer_start"]), strlen($answer_tmpl)-strlen($tags["answer_start"])-strlen($tags["answer_end"]));
		$answer = str_replace($tags["answer_radio"], "&nbsp;&nbsp;&nbsp;", $answer);
		$answer = str_replace($tags["answer_id"], $row_question["id"], $answer);
		$answer = str_replace($tags["answer_text"], '<input type="text" name="'.$answer_name.'" size=40>', $answer);
		$question_text = substr($question_text, 0, $start) . $answer . substr($question_text, $end+strlen($tags["answer_end"]));
	}
	else
	{
		// type of option
		$type = "radio";
		if ($row_question["type"] == QT_MULTI)
			$type = "checkbox";

		// get the list of answers
		$survey_id = $row_survey["id"];
		$question_id = $row_question["id"];
		$res_answer = mysql_query("select * from nabopoll_answers where survey=$survey_id and question=$question_id order by id");
		if ($res_answer == FALSE || mysql_numrows($res_answer) == 0)
			error($row_survey, "error in database - answers");

		while ($row_answer = mysql_fetch_array($res_answer))
		{
			$count++;

			// get the answer template
			$start = strpos($question_text, $tags["answer_start"]);
			$end = strpos($question_text, $tags["answer_end"]);
			if ($start==FALSE || $end==FALSE)
			{
				if ($mode==SM_QUICK)
				{
					echo 'error in template';
					return;
				}
				else
				{
					error($row_survey, "error in template");
				}
			}
			$answer_tmpl = substr($question_text, $start, $end-$start+strlen($tags["answer_end"]));

			// final answer name
			$real_answer_name = $answer_name;
			if ($row_question["type"] == QT_MULTI)
				$real_answer_name = $real_answer_name."_".$count;

			// build the answer
			$answer = substr($answer_tmpl, strlen($tags["answer_start"]), strlen($answer_tmpl)-strlen($tags["answer_start"])-strlen($tags["answer_end"]));

			$answer = str_replace($tags["answer_radio"],' <input type="'.$type.'" name="'.$real_answer_name.'" value="'.$row_answer["id"].'">', $answer);
			$answer = str_replace($tags["answer_id"], $row_answer["id"], $answer);
			$answer = str_replace($tags["answer_text"], $row_answer["answer"], $answer);

			// if this is the last answer then we stop adding the template
			if ($count == mysql_numrows($res_answer))
				$answer_tmpl = "";

			// new string
			$question_text = substr($question_text, 0, $start) . $answer . $answer_tmpl . substr($question_text, $end+strlen($tags["answer_end"]));
		}
	}

	if ($mode == SM_ONEPAGE)
	{
		// remove last tags
		$question_text = str_replace($tags["question_submit"], "", $question_text);

		// link to results
		if ($mode == SM_QUICK)
			$question_text = str_replace($tags["survey_results"], "", $question_text);
	}
	else
	{
		// put the submit image
		$question_text = str_replace($tags["question_submit"], '<input type="image" src="'.$path.$image_vote.'" value="vote" border="0" name="vote" align="absmiddle">', $question_text);

		// link to results
		if ($mode == SM_QUICK)
			$question_text = str_replace($tags["survey_results"], $path.'quickpoll.php?survey='.$row_survey["id"].'&result=1', $question_text);
	}

	// done
	echo $question_text;
}

Minor changes made, still no change in output though :injured:

Right, we need to know whats actually expected as output and what is being output because in your first post you just said you’re having trouble with the function and that it was the while () which was messed up.

We’ve helped you to diagnose that but now you’re not telling us the latest problem. Additionally you’re now using $cat = mysql_fetch_array() but nowhere are you actually using $cat itself in your code - which in essence means that outside of the function you’re running a query for no apparent reason at all.

I mentioned in a previous post that the output should be:

Category ID
Category Title

Question Number 1
Question Answer
Question Answer
Question Answer

Question Number 2
Question Answer
Question Answer
Question Answer

and so on. Each page should display all questions assigned to that category, increment the $cat_count, and then display the next category and it’s questions after submit has been hit. In the template I have four tags, <!-- CATEGORY START –> <!-- CATEGORY END –> <!-- ANSWER START –> and <!-- ANSWER END –>. When viewing the output, the answer tags are being replaced as they should be, but the category tags are not.

There is no latest problem, it’s the same problem. In a bit more detail, only one question is being output per page, and after category one is finished, an error is displayed meaning that the $cat_count incrementation isn’t working as well. You can see the outcome here: Paparazzi Studio - Customer Satisfaction Survey

My coding skills are not great, and as we’ve said it’s bodged together. I think the while statement (while ($cat = mysql_fetch_array($res_question)) ) is either placed wrong, or the wrong stuff is contained within it. As I said, the category tags are not being replaced/removed so could this have some relation as to why the other questions aren’t being displayed as well?

Many Thanks

John

Right I’m seeing the bigger picture now. What you mean is that you want one categories worth of questions displayed on the page at a time and that you need to save the next category number for the next page.

What you’re doing in your code is trying to loop through every category using mysql_fetch_array() and incrementing $cat_count which won’t work. All that will happen is that it for the first category go to the last category and then thats it -you then have to start over and I suspect that is why your code then errors.

You won’t like me for saying this but you need to start again from scratch with your logic flow because its wrong.

Get the current category, increment it and store it in the session. Next time the script is called check the session for a category, if it exists then generate the new category and questions based on that value and then increment it. Next time the script is called… and so on.

I’m out for a couple of hours this evening but please download teamviewer if you’re still struggling and we’ll have a dsktop session some time this evening and get it working between us.

I had a feeling that was going to be your answer! Yeah I understand, I’ll give it a go now and get teamviewer downloaded. Thanks so much for your help mate

No worries, give it a shot (remember to save the existing code in a .txt file etc so you can still reuse important bits) and then let me know. No idea what time I’ll be out as its a mission the missus is dragging me on (mystery shopping at an establishment) but if you get stuck, download TV, PM me your email and we’ll get something sorted out.

Also remember to echo out variables through strategic parts of your script so you can see whats actually going on and fix accordingly. PHP programming isn’t about being able to write code its about being able to debug and fix it :wink: