How to pull out of the database to make a FAQ page?

Ok, I have a database with three tables. Q & A, Categories and Identifiers. Categories contain categories, Q&A has questions and answers, and identifiers have identifiers of various categories and related questions. How can I extract from a database to get a category, and then a Q & A loop that falls into this category?

function print_categories() {
global $MySQL;


if (($categories = $MySQL->select_all("SELECT * FROM QandA_wiki.categories")) !== false) {
foreach ($categories as $citem) {
$counter = 1;
print"<div id=\"categories_start\" style=\"width:424px; margin:0 auto;\">\n";
print"<div id=\"category\" >\n";
print"<h2> {$citem['description']}</h2>";
print  "<div class=\"col-lg-12 footerLine\"><hr /></div>";
    if (($questions = $MySQL->select_all("SELECT * FROM QandA_wiki.qanda")) !== false) {
    foreach ($questions as $qitem) {
        print"<div id=\"question_number\" >\n";
        print"<div id=\"questions\" >\n";
        print"<p>#: {$counter} -<strong>{$qitem['question']}</strong></p>";

        print"</div>\n";
        print"<p>     {$qitem['answer']}</p>";
        print  "<div class=\"col-lg-12 footerLine\"><hr /></div>";


        print"</div>\n";

        print"<div style=\"clear:both;\"></div>\n";
        print"<br />\n";
        $counter=$counter+1;
        }
      }
print"</div>\n";
print"</div>\n";

print"<div style=\"clear:both;\"></div>\n";
print"<br />\n";

}}}}
+4
source share
1 answer

I'm not sure about your table and column names, but this is an approximate idea LEFT JOINthat might get what you need

SELECT QandA_wiki.qanda.*, QandA_wiki.qanda.id, QandA_wiki.qanda.name_of_your_column
FROM QandA_wiki.qanda
LEFT JOIN QandA_wiki.categories
ON QandA_wiki.qanda.category_id = QandA_wiki.categories.id // This is a guess here...likely will need to modify
WHERE QandA_wiki.categories.name_of_your_column = name_you_want; // This where clause does the filtering as you needed

, , $MySQL, mysqli ( $link - ):

$query = mysqli_query($link,
    "SELECT QandA_wiki.qanda.*, QandA_wiki.qanda.id, QandA_wiki.qanda.name_of_your_column
    FROM QandA_wiki.qanda
    LEFT JOIN QandA_wiki.categories
    ON QandA_wiki.qanda.category_id = QandA_wiki.categories.id
    WHERE QandA_wiki.categories.name_of_your_column = name_you_want;");

$query. , . , .

+1

Source: https://habr.com/ru/post/1610283/


All Articles