总体思路是在div中绘制某人的得分图表。 我有一个按钮,单击该按钮即可运行图形绘制功能。 我还有另一个函数,该函数使用switch语句从数据库中检索数据,因为该函数与其他按钮共享。

我的数据检索功能:

var getdata = function(button_id) {
    $.ajax({
        type: "POST",
        url: "../scripts/getdata.php",
        dataType: "html",
        data: {id: button_id},
        success: function(result){
            $("#profilebox").html(result);
        }
    });
};

运行getdata.php并将值返回到空白div中。

访问getdata.php:

<?php
session_start();
$switchcase = $_POST['id'];
$email = $_SESSION['user']['email'];
//connect to database here
$result=mysqli_query($con,"SELECT * FROM users WHERE email = '$email'");
switch ($switchcase) {
    case "profile_home":
        while($row = mysqli_fetch_array($result)) {
        echo $row['username'] . "'s Profile<br><br>";
        echo "Name: " . $row['firstname'] . ' ' . $row['lastname'] . "<br><br>";
        echo "Things I like:<br>";
        echo $row['like'] . "<br><br>";
        echo "Things I dislike:<br>";
        echo $row['dislike'] . "<br><br>";
        echo "Other Sports:<br>";
        echo $row['sports'];
    };
        break;
    case "profile_scores":
        while($row = mysqli_fetch_array($result)) {
        $row['correct'];
        $row['incorrect'];
    };
        break;
    case "profile_merits":
        //CODE GOES HERE;
        break;
    case "profile_help":
        //CODE GOES HERE;
        break;
    case "profile_edit":
        //CODE GOES HERE;
        break;
}
mysqli_close($con);
?>

接收POST的div ID(profile_scores),从数据库获取数据,切换到第二种情况。 现在这是问题所在,我不确定如何通过

        $row['correct'];
        $row['incorrect'];

值返回到原始页面,并使它们显示在图形中

/* correct value */

/* incorrect value */

是。

图形绘制功能:

function drawVisualization() {

    getdata("profile_scores");

    // Create and populate the data table.
    var data = google.visualization.arrayToDataTable([
        ['', 'Correct', 'Incorrect'],
        ['Scores',  /* correct value */, /* incorrect value */],
    ]);

    var options = {
            'title': 'Total Scores Overall',
            'width': 600,
            'height': 400,
            'hAxis': {title: ''},
            'backgroundColor': 'transparent'
    };
    // Create and draw the visualization.
    new google.visualization.ColumnChart(document.getElementById('profilebox')).
    draw(data, options);
};

用户单击按钮后便运行此函数,因此它将调用getdata函数,然后使用从getdata.php接收的值绘制图形。

任何帮助表示赞赏:)

===============>>#1 票数:0

您的PHP代码可以将返回的参数编码为(JSON编码)关联数组。 然后,您可以通过ajax响应中的键分别检索它们中的每一个,并将它们传递到JavaScript代码中。

===============>>#2 票数:0 已采纳

我完全不确定您要的是什么,但这是我的猜测。

如果要从案例数据库中的案例2中获取$ result 然后,您可以使用JSON将PHP数组传递给Javascript,然后完成其余的工作。 就像假设这是您的switch语句的第2种情况

case "profile_scores":
{
    $row = $result->fetch_array(MYSQLI_NUM);
    echo json_encode($row);
    break;
}

然后在您的ajax响应中执行此操作

$.ajax({
    type: "POST",
    url: "../scripts/getdata.php",
    dataType: "html",
    data: {id: button_id},
    success: function(result){
        console.log(result);
        console.log(JSON.parse(result));
        var phparray = JSON.parse(result);
        var correct = phparray[12];   //<---This will return 100 
        var incorrect = phparray[13];  //<---This will return 10
        //I Dont know which one is your correct or incorrect column number but you can have an idea now
    }
});

这样,您就可以将PHP数组添加到javascript,这是数据库的结果。 我也建议您更正Switch语法。 这不会有多大区别,但是应该小心,因此这是正确的语法。

switch($switchcase)
{
   case "case1":
   {
       //Code to go
       break;
   }
   case "case2":
   {
       //Code to go
       break;
   }
   case "case3":
   {
       //Code to go
       break;
   }
}

注意,我已经休息了; 开关盒内的命令

===============>>#3 票数:0

尝试使用json.php获取javascript调用的json解析字符串。 下载链接: class.json.php

JavaScript的变化:

   var getdata = function(button_id) {
        $.ajax({
            type: "POST",
            url: "../scripts/getdata.php",
            dataType: "json", // html -> json
            data: {id: button_id},
            success: function(result){

                $("#profilebox").html(result.html);

                return result;
            }
        });
    };

    function drawVisualization() {

        var data = getdata("profile_scores");

        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
            ['', 'Correct', 'Incorrect'],
            ['Scores',  data.correct, data.incorrect],
        ]);

        var options = {
                'title': 'Total Scores Overall',
                'width': 600,
                'height': 400,
                'hAxis': {title: ''},
                'backgroundColor': 'transparent'
        };
        // Create and draw the visualization.
        new google.visualization.ColumnChart(document.getElementById('profilebox')).
        draw(data, options);
    };

getdata.php中的更改:

<?php
    // INCLUDE JSON
    // ----------------------------
    include_once("class.json.php");
    $json = new JSON();

    $coreJSON = array();
    // ----------------------------

    session_start();

    $switchcase = $_POST['id'];

    $email = $_SESSION['user']['email'];

    // call as result.html in javascript
    $coreJSON['html'] = ""; 
    // call as result.correct in javascript
    $coreJSON['correct'] = "";
    // call as result.incorrect in javascript
    $coreJSON['incorrect'] = "";

    //connect to database here
    $result=mysqli_query($con,"SELECT * FROM users WHERE email = '$email'");
    switch ($switchcase) {

        case "profile_home":  // <- CHANGES

            while($row = mysqli_fetch_array($result)) {

                $coreJSON['html'].= <<<EOF
                    {$row['username']}'s Profile<br /><br />
                    Name: {$row['firstname']} {$row['lastname']}<br /><br />
                    Things I like:<br>
                    {$row['like']} <br /><br />
                    Things I dislike:<br />
                    {$row['dislike']}<br /><br />
                    Other Sports:<br />
                    {$row['sports']}
EOF;
            }
            /* Start the closing EOF at the beginning of the line, and delete all spaces and tabs after the semicolon. Next code must start in a new line.*/

            break;

        case "profile_scores":

            while($row = mysqli_fetch_array($result)) {
                $coreJSON['correct'].= $row['correct']; // <- CHANGES
                $coreJSON['incorrect'].= $row['incorrect']; // <- CHANGES
            }

            break;
        case "profile_merits":
            //CODE GOES HERE;
            break;
        case "profile_help":
            //CODE GOES HERE;
            break;
        case "profile_edit":
            //CODE GOES HERE;
            break;
    }

    mysqli_close($con);

    // CLOSING AND CONVERTING
    // ----------------------------
    $encodeJSON = $json->encode($coreJSON);

    header('Content-Type: text/json'); // text/plain is good to
    header('Content-Length: '.strlen($encodeJSON));

    die($encodeJSON); 
    // ----------------------------
?>

希望对您有所帮助。

  ask by Jake Mansell translate from so

未解决问题?本站智能推荐: