duanmao2774
2016-02-17 00:36
浏览 84
已采纳

如何链接2个PHP脚本在d3中同时运行

I have two php scripts both connected to the same database but each script queries a different table. My question is how do I link these scripts so they run simultaneously. Currently they are running at the same time but completely independent of each other. For example I am working with a scatter plot in d3. One script queries information for the y axis and the other script for the x axis. This is the query in the php script for the x axis:

$myquery = "SELECT  `ID`, `TITLE`, `YEAR`, `In_library` FROM  `papers` where In_library = 1";

YEAR is then called in d3 and used to scale the x axis. The query for the y axis script is:

$myquery = "SELECT `ID_to`, count(*) as `counter` from `citations` group by `ID_to`";

counter is called by d3 and used as the y axis. These are the connections made in d3:

d3.json("connection1.php", function(error, data){
if (error) throw error;
data.forEach(function(d) {
    d['TITLE'] = d.TITLE.toString();
    d['YEAR'] = +d['YEAR']; 
     console.log(d['YEAR']);
    })
d3.json("connection2.php", function(error, data){
//if (error) throw error;
data.forEach(function(d) {
    d['counter'] = +d['counter'];
    console.log(d);
    })  

when ran exactly as above it will just return the points correctly in regards to counter but on top of the y axis with no x values(ie x=0). But when I separate the 2 functions as in close the parentheses from the first line of each function and define all other code ie. svg, domain range etc in BOTH functions they will both execute but mutually exclusive ie. the points displayed are duplicated - one set only with y values and the other set only with x values.

My mission is to integrate these so only one set of the specified points are displayed on the graph using both the x and y axis properly.
I would be grateful for any feedback and happy to post more of the code if I havent posted enough. thanks

EDIT: Here is my php script:

<?php
$username = "root"; 
$password = "";   
$host = "localhost";
$database="samplelit";

$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);

   $myquery = "SELECT  `ID`, `TITLE`, `YEAR`,`In_library` FROM  `papers` where In_library = 1";
$query = mysql_query($myquery);

if ( ! $query ) {
    echo mysql_error();
    die;
}

$data = array();

for ($x = 0; $x < mysql_num_rows($query); $x++) {
    $data[] = mysql_fetch_assoc($query);
}

echo json_encode($data);     

mysql_close($server);


?>

And the other script is the same with just the other query. Where do I assign the key value pairs? Thanks for the help I am very new to php and appreciate this a lot!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douqie1852 2016-02-17 18:06
    已采纳

    Disclaimer, I'm not a php developer and the below code is untested...

    This should be as simple as:

    <?php
    
    $username = "root"; 
    $password = "";   
    $host = "localhost";
    $database="samplelit";
    
    $server = mysql_connect($host, $username, $password);
    $connection = mysql_select_db($database, $server);
    
    // this is your return
    $data = array(
        query1Results => array(),
        query2Results => array(),
    );
    
    $query1 = mysql_query("SELECT  `ID`, `TITLE`, `YEAR`,`In_library` FROM  `papers` where In_library = 1");
    
    if ( ! $query1 ) {
        echo mysql_error();
        die;
    }
    
    for ($x = 0; $x < mysql_num_rows($query1); $x++) {
        $data['query1Results'][] = mysql_fetch_assoc($query1);
    }
    
    $query2 = mysql_query("SQL FOR SECOND QUERY");
    
    if ( ! $query2 ) {
        echo mysql_error();
        die;
    }
    
    for ($x = 0; $x < mysql_num_rows($query2); $x++) {
        $data['query2Results'][] = mysql_fetch_assoc($query2);
    }
    
    echo json_encode($data);     
    
    mysql_close($server);
    
    ?>
    

    Now back on the JavaScript side:

    d3.json("connection.php", function(error, data){
        if (error) throw error;
    
        // do stuff with results
        console.log(data.query1Results);
        console.log(data.query2Results);
    });
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题