<?php
error_reporting( E_ALL&~E_NOTICE ); //未定义变量时会报错 加上这个
require_once('comment/startsession.php');
$title = "Quistionaire";
require_once('comment/header.php');
require_once('comment/navmenu.php');
require_once('dbc.php');
//第一步当用户有响应时创建一个登录用户响应表 response_id topic_id response,topic_name
$query = "select * from match_response where user_id = '".$_SESSION['user_id']."'";
$data = mysqli_query($dbc,$query);
if (mysqli_fetch_array($data) != 0) {
$query = "select mr.response_id,mr.topic_id,mr.response,mt.name as topic_name from match_response as mr INNER JOIN match_topic AS mt USING (topic_id) WHERE mr.user_id = '".$_SESSION['user_id']."'";
$data = mysqli_query($dbc,$query);
$user_response = array();
while ($row=mysqli_fetch_array($data)) {
array_push($user_response,$row);
}
echo '<pre>';
// var_dump($user_response);
//创建变量用于对比跟储存
$mismatch_score = 0; //分数
$mismatch_user_id = -1; //其他用户
$mismatch_topics = array();//主题
//第二步查找其他用户的响应数据
$query = "select user_id from match_user where user_id != '".$_SESSION['user_id']."'";
$data = mysqli_query($dbc,$query);
while ($row=mysqli_fetch_array($data)) { //循环查找用户
// echo $row['user_id'].'<br>';
//查找其他用户的响应生成$mismatch_responses
$query2 = "select response_id,topic_id,response from match_response where user_id = '".$row['user_id']."'";
$data2 = mysqli_query($dbc,$query2);
$mismatch_responses = array();
while ($row2=mysqli_fetch_array($data2)) {
array_push($mismatch_responses, $row2);
}
// var_dump($mismatch_responses);//1 3 4
//将两个表格进行比对 1+2=3记录到score中,主题保存到数组 //int将2转为整数
$score = 0;
$topics = array();
for ($i=0; $i < count($user_response); $i++) {
if (((int)$user_response[$i]['response']) + ((int)$mismatch_responses[$i]['response']) == 3) {
$score += 1;
array_push($topics,$user_response[$i]['topic_name']);
// var_dump($topics);
}
}
//对比分数取最大分数
if ($score > $mismatch_score) {
$mismatch_score = $score;
$mismatch_user_id = $row['user_id']; //记录匹配的用户id
$mismatch_topics = array_slice($topics,0);//array_slice()从数组中抽取一片 这里直接复制数组
}
// var_dump($mismatch_topics).'<br>';
}
// die();
if ($mismatch_user_id != -1) {
$query = "select username,first_name,last_name,city,state,picture from match_user where user_id = '".$mismatch_user_id."'";
// echo $query;
$data = mysqli_query($dbc,$query);
if (mysqli_num_rows($data)==1) {
$row = mysqli_fetch_array($data);
// var_dump($row);
//展示匹配的用户信息
echo '<table><tr><td class="lable">';
if (!empty($row['first_name'] && !empty($row['last_name']))) {
echo $row['first_name'].' '.$row['last_name'].'<br>';
}
if (!empty($row['city'] && !empty($row['state']))) {
echo $row['city'].' '.$row['state'].'<br>';
}
echo '</td><td>';
if (!empty($row['picture'])) {
echo '<img src="'.UPLOADPATH.$row['picture'].'" alt="'.$row['picture'].'" width="100px" height="100px"/><br>';
}
echo '</td></tr></table>';
//展示匹配的主题
echo '<h4>You are mismatch on the following '.count($mismatch_topics).' topics: </h4>';
foreach ($mismatch_topics as $topic) {
echo $topic.'<br>';
}
// 用户链接
echo '<h4>Viwe <a href="mismatch_viewprofile.php?user_id='.$mismatch_user_id.'">'.$row['first_name'].'\' s profile </a>.</h4>';
}
}
}else{
echo '<p>You must first<a href="mismatch_questionnaire.php"> anser the questionnaire </a> before you can be mismatched.</p>';
}
require_once('comment/footer.php');
?>