如何将这三个PHP提要合并为一个提要

时间:2019-06-04 20:47:58

标签: php mysql

我制作了3个feed(请参见代码),它们各自按预期工作。但事实是,我想将所有3个提要合并在一起,并按时间戳将它们合并为一个大提要。

我已经尝试研究UNION ALL,但这似乎是一种不好的方法。我真的找不到实现此目的的方法-因为我找不到任何有这么多表和这些要求的人。

我当然简化了代码,因此更容易理解。

$m_id = $_SESSION['myid'];

//LIMIT THE LENGHT OF THE ANSWER
function str_limit_answer($value, $limit = 250, $end = '...')
{
    $limit = $limit - mb_strlen($end); // Take into account $end string into the limit
    $valuelen = mb_strlen($value);
    return $limit < $valuelen ? mb_substr($value, 0, mb_strrpos($value, ' ', $limit - $valuelen)) . $end : $value;
}

echo "<h1>Questions that have been asked by people you follow</h1>";

//GET ALL QUESTIONS FROM THE PEOPLE THE USER FOLLOWS
$queryQ = "SELECT 
    questions.q_id,
    questions.question,
    questions.m_id,
    questions.timestamp,
    member.m_id AS M_mid,
    member.navn AS name
        FROM
    questions AS questions
        INNER JOIN
    members AS member ON questions.m_id = member.m_id
        WHERE questions.m_id IN( SELECT following FROM following_users WHERE m_id = ? )
    ORDER BY timestamp DESC
";
if ($stmtQ = $con->prepare($queryQ)) {

    $stmtQ->bind_param('s', $m_id);

    /* execute statement */
    $stmtQ->execute();

    /* bind result variables */
    $stmtQ->bind_result(
        $Q_q_id,
        $Q_question,
        $Q_m_id,
        $Q_timestamp,
        $Q_M_mid,
        $Q_name
        );

    /* fetch values */
    while ($stmtQ->fetch()) {
        //ECHO STUFF HERE
        echo $Q_question."<br />";
        echo $Q_name." asked this question<br /><br />";
    }
}

echo "<h1>Questions that have been answered by people you follow</h1>";

//GET ALL THE ANSWERS TO QUESTIONS FROM PEOPLE THE USER FOLLOWS
//ALSO GET THE QUESTION
$queryA = "SELECT 
    answers.a_id,
    answers.answer,
    answers.m_id,
    answers.timestamp,
    question.q_id,
    question.question,
    member.m_id AS M_mid,
    member.navn AS name
        FROM
    answers AS answers
        INNER JOIN
    questions AS question ON question.q_id = answers.q_id
        INNER JOIN
    members AS member ON answers.m_id = member.m_id
        WHERE answers.m_id IN( SELECT following FROM following_users WHERE m_id = ? )
    ORDER BY timestamp DESC
";
if ($stmtA = $con->prepare($queryA)) {

    $stmtA->bind_param('s', $m_id);

    /* execute statement */
    $stmtA->execute();

    /* bind result variables */
    $stmtA->bind_result(
        $A_a_id,
        $A_answer,
        $A_m_id,
        $A_timestamp,
        $A_q_id,
        $A_question,
        $A_M_mid,
        $A_name
        );

    /* fetch values */
    while ($stmtA->fetch()) {
        //ECHO STUFF HERE
        echo $A_question."<br />";
        echo $A_name." answered this question<br />";
        echo str_limit_answer($A_answer)."<br /><br />";
    }
}

echo "<h1>Questions that have been asked in the subjects you follow</h1>";

//GET ALL THE QUESTIONS FROM THE SUBJECTS THAT THE USER FOLLOWS
$queryT = "SELECT
    question.q_id,
    question.question,
    question.timestamp,
    question.m_id,
    member.m_id AS M_mid,
    member.navn AS name,
    questions_tags.q_id AS Q_qid,
    questions_tags.t_id,
    tags.t_id AS T_tid,
    tags.tag
        FROM
    questions AS question
        INNER JOIN
    members AS member ON question.m_id = member.m_id
        INNER JOIN
    questions_tags AS questions_tags ON questions_tags.q_id = question.q_id
        INNER JOIN
    tags AS tags ON questions_tags.t_id = tags.t_id
        WHERE questions_tags.t_id IN( SELECT following FROM following_tags WHERE m_id = ? )
    GROUP BY question.question
    ORDER BY timestamp DESC
";
if ($stmtT = $con->prepare($queryT)) {

    $stmtT->bind_param('s', $m_id);

    /* execute statement */
    $stmtT->execute();

    /* bind result variables */
    $stmtT->bind_result(
        $T_q_id,
        $T_question,
        $T_timestamp,
        $T_m_id,
        $T_M_mid,
        $T_name,
        $T_Q_id,
        $T_t_id,
        $T_T_tid,
        $T_tag
        );

    /* fetch values */
    while ($stmtT->fetch()) {
        //ECHO STUFF HERE
        echo $T_question."<br />";
        echo $T_name." asked this question<br />";
        echo "Because you're following the tag <strong>".$T_tag."</strong><br /><br />";
    }
}

那我应该从这里去哪里?谢谢。

1 个答案:

答案 0 :(得分:1)

我会这样:

while ($stmtQ->fetch()) {
    $feed[$Q_timestamp] = ['type' => 'Q', 'data' => [/* values from the row */]];
}
while ($stmtA->fetch()) {
    $feed[$A_timestamp] = ['type' => 'A', 'data' => [/* values from the row */]];
}
while ($stmtT->fetch()) {
    $feed[$T_timestamp] = ['type' => 'T', 'data' => [/* values from the row */]];
}

ksort($feed);  // or krsort depending on the order you want

foreach ($feed as $timestamp => $post) {
    // output the post using the correct format for the post type.
    // Maybe render a template? Just a suggestion 
}

(进一步简化,显然您仍然需要绑定params,values / execute / etc。)