I am working on a project and I just wanted to get some help on how to approach this problem and help clean up the code and remove a lot of duplication.
I have an FAQ database table with "id, user_id, faq_question, faq_text, sort_id". Now every time a dynamic site is created, I have an insert query which creates the site and then also inserts into the FAQ table with the default questions and answers that I want to use.
$insertFAQ = array(
'user_id' => $user_id,
'faq_question' => 'Default Question',
'faq_text' => 'Default Answer',
'sort_id' => '1'
);
$Db->insert('faq', $insertFAQ);
The sort_id is there because they have the ability to drag the que开发者_如何学运维stions in a different order and it updates the database and shows on the website in the new order.
Now the problem is that I want to have 10 default FAQ questions and answers that will be created for every website. If we get hundreds of sites in the database, that will be a ton of records in the FAQ database table which is pretty much all duplicated.
I know there has to be an easier way to do it, the only thing that throws me off is the sort_id because they could all have the same questions and answers but sort them differently. And then of course they have the ability to add custom questions.
P.S. - How can I add multiple questions/answers in the above array that I posted?
AS for having multiple question in the array you posted in the question, you can just have a multi dimensional array:
$insertFAQ = array(
0 =>array(
'user_id' => $user_id,
'faq_question' => 'Default Question',
'faq_text' => 'Default Answer',
'sort_id' => '1'
),
1 =>array(
'user_id' => $user_id,
'faq_question' => 'Default Question',
'faq_text' => 'Default Answer',
'sort_id' => '1'
),
2 => array(
'user_id' => $user_id,
'faq_question' => 'Default Question',
'faq_text' => 'Default Answer',
'sort_id' => '1'
) );`
and so on and son on.
as for the sorting - You can create a separate table that will just hold the sorting order, site id and question id.
id, faq_id, site_id, sort_order
Id a new site is created you just create a new entry for it in this table, and than the questions don't have to repeat themselves, your only specifying what order goes to what site.
One way would be to have a site_id
field in the table. For global faqs the site_id
could be null
.
Then to get the faqs for a site:
SELECT * FROM faq WHERE site_id IS NULL OR site_id = $site_id
If I understand correctly your question... To me it seems you simply need to split the question/answer from the user record, and have instead a reference to a question/answer entry in the user record:
// insert if it doesn't exist
$insertFAQ = array(
'faq_id' => $faq_id,
'faq_question' => 'Default Question',
'faq_text' => 'Default Answer'
);
// get faq id of newly inserted or existing faq entry
...
// insert user
$insertUser = array(
'user_id' => $user_id,
'faq_id' => $faq_id,
'sort_id' => '1'
);
精彩评论