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

如何链接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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度