根据ID值选择数据

时间:2017-01-19 15:55:42

标签: php jquery mysql chart.js

我正在尝试根据值选择数据,我有一个从团队表中选择数据的表单,我需要从select下拉列表中获取该行的ID(我可以这样做)然后使用用于确定在执行AJAX请求时需要选择的数据。这是:

团队页面 - 此页面绘制我的图表,该图表通过AJAX提取数据以绘制图表及其值:

            <form action="teams.php?dashboard_id=<?php echo $dashboard_id; ?>" method="POST">

                <select name="teamId">
                <option selected="true" disabled="disabled">Please choose...</option>
                <?php

                    $sql = "SELECT * FROM teams WHERE dashboard_id = $dashboard_id";
                    $result = $conn->query($sql);

                    if($result->num_rows > 0){
                        while($row = $result->fetch_assoc()){
                            echo '<option value=' . $row["team_id"] . '>' . $row["team_name"] . '</option>';        
                        }
                    }
                ?>
                </select>
                <button class="compare">View</button>
            </form>


            <?php
            if (!empty($_POST["teamId"])) {
                $teamSelect = $_POST["teamId"];
                echo $teamSelect;    
            }else{  
                echo "";
            }
            ?>

            <div style="max-width: 450px;">
              <canvas id="mycanvas" class="container"></canvas>
            </div>

这是我正在进行SELECT的PHP页面:

<?php

include 'config.php';

$teamID = $_POST['teamId'];

$query = sprintf("SELECT
tm.member_id,
tm.team_id,
m.member_id,
m.firstName,
m.lastName,
m.score_1,
m.score_2,
m.score_3,
m.score_4,
m.score_5,
m.score_6,
m.score_7,
m.score_8,
m.dashboard_id,
t.team_id,
t.team_name,
t.dashboard_id
FROM team_members tm
JOIN members AS m
on m.member_id = tm.member_id
JOIN teams AS t
on t.team_id = tm.team_id
WHERE tm.team_id = '$teamID'"); // This need to be dynamic and got from the POST request on the form above.

$result = $conn->query($query);

$data = array();
foreach ($result as $row) {
  $data[] = $row;
}

$result->close();
$conn->close();
header('Content-type: application/json');
print json_encode($data);

?>

我通过另一个文件绘制图表:

$(document).ready(function(){ 

$.ajax({ 
url : "http://localhost/acredashAdv/teamData.php", 
type : "GET", 
success :function(data){ 
console.log(data); 


var chartata = { 
labels: [ 
"Strategic Development and Ownership", 
"Driving change through others", 
"Exec Disposition", 
"Commercial Acumen", 
"Develops High Performance Teams", 
"Innovation and risk taking", 
"Global Leadership", 
"Industry Leader" 
]}; 

var ctx = $("#mycanvas"); 

var config = { 
    type: 'radar', 
    data: chartata, 
    animationEasing: 'linear',
        options: {
         legend: {
            display: true,
            position: 'bottom'
        },
         tooltips: {
            enabled: true
        },
        scale: {
            ticks: {
                fontSize: 15,
                beginAtZero: true,
                stepSize: 1
            }
        } 

    },
}, 

LineGraph = new Chart(ctx, config); 

var colorArray = [
    ["#7149a5", false],
    ["#58b7e0", false],
    ["#36bfbf", false],
    ["#69bd45", false],
    ["#5481B1", false],
    ["#6168AC", false]
];


for (var i in data) { 
    tmpscore=[]; 
    tmpscore.push(data[i].score_1); 
    tmpscore.push(data[i].score_2); 
    tmpscore.push(data[i].score_3); 
    tmpscore.push(data[i].score_4); 
    tmpscore.push(data[i].score_5); 
    tmpscore.push(data[i].score_6); 
    tmpscore.push(data[i].score_7); 
    tmpscore.push(data[i].score_8); 

    var color, done = false;
    while (!done) {
        var test = colorArray[parseInt(Math.random() * 6)];
        if (!test[1]) {
            color = test[0];
            colorArray[colorArray.indexOf(test)][1] = true;
            done = !done;
        }
    }


newDataset = { 
    label: data[i].firstName+' '+data[i].lastName, 
     borderColor: color,
    backgroundColor: "rgba(0,0,0,0)", 
    data: tmpscore, 
}; 

    config.data.datasets.push(newDataset); 

} 

LineGraph.update(); 
},  
}); 

});

这一切都很棒但是在我的选择查询中我需要WHERE子句向我显示根据团队ID的值确定的数据而不是静态值,但我不知道如何将ID从选择传递回到AJAX文件?

1 个答案:

答案 0 :(得分:1)

然后使用会话。 $_SESSION['teamID'] = $teamID。然后你可以在任何地方引用它。一定要使用会话在每个php文件的顶部启动会话。

session_start();
$teamID = $_POST['teamId'];
$_SESSION['teamID'] = $teamID;

参考文献: