如何从数据库中提取FAQ页面?

时间:2015-10-05 14:27:45

标签: php mysql

好吧,我有一个包含三个表的数据库。 Q& A,类别和ID。类别包含类别,Q& A具有问题和答案,ID具有各种类别的ID以及附加的问题。如何从数据库中提取以便获得一个类别,然后是一个属于该类别的Q& A循环?

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";

}}}}

1 个答案:

答案 0 :(得分:1)

我不确定您的表/列名称,但这是LEFT JOIN可能得到您需要的内容的粗略概念

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。如果您通过用户输入获取类别,则应考虑使用预准备语句。我希望这能让你朝着正确的方向前进