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