PHP looping one question category to the answers of that cat

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

PHP looping one question category to the answers of that cat

Post by paulng »

Hello there!

Could some one please help me with the code below. I'll explain what Im trying to do.
Basically, I have an FAQ application and I'm looping to output the question category and all the answers for that category. At the moment it diplaying all the categoties on top and then all the questions for all categories. Would you please help with a way of having to display a category with the answers for that category then the next and so on based on the code Im providing below...Your help with be much appreciated. Many thanks in advance.

Code: Select all

if(isset($_GET['q_id'])) { $q_id = $_GET['q_id']; }
            if($q_id != "") {
            $question_query = mysql_query("SELECT * FROM faq_questions WHERE q_id='$q_id'");
            $question = mysql_fetch_assoc($question_query);
            if(mysql_num_rows($question_query) == 0) {
            echo "<h2>An error has occurred:</h2>
            You are attempting to view an FAQ question that does not exist.
                ";
                }
            $answerQ = "
            <b>$question[question]</b><br><br>
            $question[answer]
            <form action='/.../help/$LANG' method='POST'>
            <input type='submit' value='Back to FAQ'>
            </form>
            ";
            } else {
            // IF USER IS LOOKING AT THE MAIN PAGE
            $totalcount = 0;
            $faq_questions = mysql_query("SELECT * FROM FROM faq_questions");

            $faqcat = mysql_query("SELECT * FROM faq_categories ORDER BY c_order ASC");
            $num_of_kitties = mysql_num_rows($faqcat);
            while($faqcat_info = mysql_fetch_assoc($faqcat)) {
            $questions = mysql_query("SELECT q_id, c_id, question FROM faq_questions WHERE c_id='$faqcat_info[c_id]' ORDER BY q_order");


            // SHOW CATEGORY NAME, IF IT CONTAINS QUESTIONS AND CATEGORY NAMES ARE TURNED ON
            if(mysql_num_rows($questions) > 0) {
            $faqcat_infomain = "<b>$faqcat_info[category]</b>"; 
            }

            // SHOW QUESTIONS
            $count = 0;
            while($question = mysql_fetch_assoc($questions)) {
            $totalcount++;
            $count++;
            // SHOW NUMBERS IF ENABLED
            if($admin_info[shownumbers] == 1) {
            if($admin_info[showcats] == 1) { echo "$count. "; } else { echo "$totalcount. "; }
            }

            $Questionmain .= $count."&nbsp;&nbsp;<a href='/.../help/$LANG?q_id=$question[q_id]'>$question[question]</a><br>";
          
           $Questionmain = $faqcat_infomain."<br>".$Questionmain;
//echo $Questionmain;

}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here is how I handled my FAQs...

Code: Select all

<?php
//
//Grab the question categories
//
$sql = "SELECT cat_id, cat_title FROM " . Q_CATS_TABLE . " ORDER BY cat_order ASC";

if ( !$result=$db->sql_query($sql) )
{
	set_error('Could not get FAQ categories: ' . mysql_error());
}

while ( $categories[] = $db->sql_fetchrow($result) )
{
	$total_cats = count($categories);
}


//
//get the questions
//
$sql = "SELECT faq_id, cat_id, faq_question, faq_answer, faq_show, faq_passcode, faq_views 
		FROM " . QUESTION_TABLE . " 
		WHERE faq_show = " . TRUE . " 
		ORDER BY faq_views DESC"; 

if ( !$result=$db->sql_query($sql) )
{
	set_error('Could not get FAQ question list: ' . mysql_error());
}

while ( $questions[] = $db->sql_fetchrow($result) )
{
	$total_q = count($questions);
}

//
// Show the categories
//
for ($i = 0; $i < $total_cats; $i  )
{
	$category = $categories[$i]['cat_title'];
	$cat_id = $categories[$i]['cat_id'];
	echo '<h1>' . $category . '</h1>';

	//
	// Show the questions
	//
	for ($j = 0; $j < $total_q; $j  )
	{
		if ( $questions[$j]['cat_id'] == $cat_id )
		{
			echo '<p>' . $questions[$j]['faq_question'] . '</p>';,
			echo '<p>This question has been viewed ' . $questions[$j]['faq_views'] . ' times.</p>';
		}
	}
}
?>
Post Reply