2013-08-28 34 views
2

相關的MySQL表顯示的行我希望有人能幫助我,PHP只從使用用戶ID

我有兩個表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 用戶ID可使用$current_user = wp_get_current_user();或類似的東西被拉動。 我不知道如何使用'statistic_ref_id'來只顯示wp_wp_pro_quiz_statistic中匹配值爲statistic_ref_id的行。

更新:

<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> 
+0

啊一個smurf命名約定:) http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html一個嚴肅的筆記爲什麼你不執行一個連接? –

回答

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