PHP仅显示与用户ID关联的mysql表中的行

时间:2013-08-28 15:37:55

标签: php mysql wordpress

我希望有人可以帮助我,

我有两个表wp_wp_pro_quiz_statistic_refwp_wp_pro_quiz_statistic,它们之间的公共链接是statistic_ref_id

要显示的数据位于wp_wp_pro_quiz_statistic我可以使用以下内容获取:

<?php
global $wpdb;
$current_user = wp_get_current_user();
$current_user_statid = $wpdb->get_results( "SELECT * FROM wp_wp_pro_quiz_statistic_ref WHERE user_id = $current_user->ID");

$result = $wpdb->get_results( "SELECT * FROM wp_wp_pro_quiz_statistic WHERE $current_user_statid = statistic_ref_id");

echo "Question:"."  "."Points:"."<br><br>";
foreach($result as $row)
{

echo $row->question_id." ".$row->points."<br>";

}
?>

wp_wp_pro_quiz_statistic_ref存储user_idstatistic_ref_id 可以使用$current_user = wp_get_current_user();或类似内容来提取用户ID。  我不知道如何使用'statistic_ref_id'来显示与statistic_ref_idwp_wp_pro_quiz_statistic的值匹配的行。

更新:          

    <table border="1" width="500px">
        <tr>
            <th>Question Number</th>
<th>Clause</th>
<th>Subject</th>
            <th>Score</th>
        </tr>
<?php
global $wpdb;
$current_user = wp_get_current_user();
$result = $wpdb->get_results( "
SELECT stats.*
  FROM wp_wp_pro_quiz_statistic stats
       JOIN wp_wp_pro_quiz_statistic_ref refs on stats.statistic_ref_id = refs.statistic_ref_id
WHERE refs.user_id= $current_user->ID ");
foreach($result as $row) {
echo "<tr>
                <td>$row->question_id</td>
<td>some clause</td>
<td>some subject</td>
                <td>$row->points</td>
            </tr>";
}
?>
</table>
<table border="1" width="500px">
<tr><td width="445px">Total Score (Maximum 125)</td><td width="55px">0</td> </tr>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果我理解了你的问题,你应该可以使用SQL连接;像这样的东西:

<?php
global $wpdb;
$current_user = wp_get_current_user();
$result = $wpdb->get_results( "
    SELECT stats.*
      FROM wp_wp_pro_quiz_statistic stats
           JOIN wp_wp_pro_quiz_statistic_ref refs on stats.statistic_ref_id = refs.statistic_ref_id
    WHERE refs.user_id= $current_user->ID ");

echo "Question:"."  "."Points:"."<br><br>";
foreach($result as $row) {
    echo $row->question_id." ".$row->points."<br>";
}
?>