ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mismatch_mymismatch 最佳匹配条形图

2021-03-21 15:59:49  阅读:149  来源: 互联网

标签:mismatch echo user response mymismatch array id 条形图


<?php 
//统计图  条形显示不出来 ,排查可能是绘制图形时出了问题   将类别转为二维数组 循环生成条形图
error_reporting( E_ALL&~E_NOTICE ); //未定义变量时会报错  加上这个
require_once('comment/startsession.php');
$title = "Quistionaire";
require_once('comment/header.php');
if(!isset($_SESSION['user_id'])){
    echo '<p>Please <a href="userlogin.php">Log In</a> to access this page.</p>';
    exit();
}
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,mc.name as category_name from match_response as mr INNER JOIN match_topic AS mt USING (topic_id)INNER JOIN match_category as mc USING (category_id) WHERE mr.user_id = '".$_SESSION['user_id']."'";
    // echo $query;
    $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 mr.response_id, mr.topic_id, mr.response, mt.name AS topic_name, mc.name AS category_name FROM match_response AS mr INNER JOIN match_topic AS mt USING ( topic_id ) INNER JOIN match_category AS mc USING ( category_id ) WHERE user_id  = '".$row['user_id']."'";
        $query2 = "select response_id, response, topic_id 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();
        $categories = 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']);
                array_push($categories,$user_response[$i]['category_name']);
                
            }
        }
        // print_r($topics);
        // print_r($categories);
        // foreach ($categories as $categorie) {
        //     if (empty($categorie)) {
        //         unset($categorie);
        //     }
        // }
        // print_r($categories);
        //对比分数取最大分数
        if ($score > $mismatch_score) {
            $mismatch_score = $score;
            $mismatch_user_id = $row['user_id']; //记录匹配的用户id
            $mismatch_topics = array_slice($topics,0);//array_slice()从数组中抽取一片  这里直接复制数组
            $mismatch_categories = array_slice($categories,0);//array_slice()从数组中抽取一片  这里直接复制数组 去除空数组
            }
            
        }
        // print_r($mismatch_topics).'<br>';
        // print_r($mismatch_user_id).'<br>';
        // print_r($mismatch_categories);
        
        //将类别转化为一个二维数组 新的从1开始,旧的在此之前上+1  数组从0开始需要-1
        $category_totals = array(array($mismacth_categories[0],0));//类别和汇总数初始化第一个元素
        foreach ($mismatch_categories as $category) {
            // var_dump($category);
            //数组从0开始,所以数组中的最后一个元素总是-1
            if($category_totals[count($category_totals)-1][0] != $category){
                //新类别,作为一个新的子数组添加到类别总
                array_push($category_totals, array($category,1));
            }else{
                //类别数+1
                $category_totals[count($category_totals)-1][1]++;
            }
        }
        // print_r($category_totals);
        // 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>Mismatched category brekdown</h4>';
                    
                    draw_bar_graph(480,240,$category_totals,5,UPLOADPATH.$_SESSION['user_id'].'-mymismatchgraph.png');
                    echo '<img src="'.UPLOADPATH.$_SESSION['user_id'].'mymismatchgraph.png" alt="Mismatch category graph"><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');


//直方图函数  宽  高  二维数组  最大范围值  文件名
function draw_bar_graph($width,$height,$data,$max_value,$filename){
    // echo $width.'<br>';
    // echo $height.'<br>';
    // // echo '<pre>'.'<br>';
    // print_r($data).'<br>';
    // echo $max_value.'<br>';
    // echo $filename.'<br>';
    $img = imagecreatetruecolor($width, $height);//空图形
    //定义颜色
    $bg_color = imagecolorallocate(    $img,255,255,215);//white
    $text_color = imagecolorallocate($img,255,255,255);
    $bar_color = imagecolorallocate($img,0,0,0);//black
    $border_color = imagecolorallocate($img,192,192,192);//light gray

    //清空背景
    imagefilledrectangle($img,0,0,$width,$height,$bg_color);

    //绘制矩形
    // echo count($data); //6
    $bar_width = $width/((count($data) *2) +1);
    // $bar_width = $width/11;
    // echo $bar_width.' '.$data[1][1];//图片显示不出来
    for ($i = 0; $i< count($data); $i++) { 
        //绘制矩形 imagefilledrectangle(img,0,0,captcha_width,captcha_height,color)
        imagefilledrectangle($img,($i*$bar_width *2)+($bar_width),$height,($i*$bar_width*2) + ($bar_width*2),$height-(($height/$max_value)*$data[$i][1]),$bar_color);
        //将矩形变成直条
        imagestringup($img, 5, ($i*$bar_width*2)+($bar_width), $height-5,$data[$i][0], $text_color);
    }
        // imagefilledrectangle($img,0,0,74,200,$bar_color);

    //将整个直方图外围绘制成一个矩形
    imagerectangle(    $img,0,0,$width-1,$height-1,$bar_color);
    for ($i    =1; $i<=$max_value ; $i    ++) {  //在直方图左上边界绘制文本串 img 字体范围,串左上角的xy坐标,文本串,颜色
        imagestring($img,5,0,$height-($i*($height/$max_value)),$i,$bar_color);
    }
    //写文件
    imagepng($img,$filename,5);//压缩级别1-9
    imagedestroy($img);
}
?>

标签:mismatch,echo,user,response,mymismatch,array,id,条形图
来源: https://blog.csdn.net/qq_39612654/article/details/115050219

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有