简体   繁体   English

在mysql表中用主键命名cytoscape的节点

[英]name cytoscape's nodes by primary key in mysql table

I'm trying to display a network using cytoscape based on a mysql database, I've got two tables, one contains the name ( device_id ), ip and SN that means serial number (PK) of each device in the network, the otherone has relation's info: origin's SN destiny's SN , interface and port . 我正在尝试使用基于mysql数据库的cytoscape显示一个网络,我有两个表,一个表包含名称( device_id ), ipSN ,表示网络中每个设备的序列号(PK),另一个具有关系的信息: origin's SN destiny's SNinterfaceport

I've made those three querys: 我做了这三个查询:

$query = 'SELECT * FROM dispositivos';
$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());
$numdispositivos = mysql_num_rows($result);

then I've tried to make a for loop to create the nodes based on the number of rows at the table: 然后我尝试制作一个for循环,以根据表上的行数创建节点:

 var numerodispositivos = "<?php echo $numdispositivos; ?>";

using this loop im drawing nodes: 使用此循环即时绘图节点:

for (var i = 0; i < numerodispositivos; i++) {
            cy.add({
                data: { id: 'node' + i }
                }
            );
            var source = 'node' + i;
            cy.add({
                data: {
                    id: 'edge' + i,
                    source: source,
                    target: (i % 2 == 0 ? 'a' : 'b')
                }
            });

I would like to name those nodes with the primary key of "dispositivos" but i don't know how to iterate through the table row by row or how to extract that info. 我想用“ dispositivos”的主键命名那些节点,但是我不知道如何逐行遍历表或如何提取该信息。

Any help? 有什么帮助吗?

Thanks in advance 提前致谢

You could try using an array that includes your primary key in php instead of just the number of rows (you'll need to specify what your primary key is): 您可以尝试使用在php中包含主键而不是仅包含行数的数组(您需要指定主键是什么):

$j=0;
while ($row = mysql_fetch_array($result)) {
    $primary_key = $row["PRIMARY_KEY"];
    $php_array[$primary_key] = $j;
    $j++;
}

$json_from_php = json_encode($php_array);

Th resulting JSON array could then be echo'd in JS (where i is your primary key in the loop), allowing you to run a for loop that you could then pull primary key strings/numbers from: 然后,可以在JS(其中i是循环中的主键)中回显生成的JSON数组,从而允许您运行for循环,然后可以从中提取主键字符串/数字:

var jsonArray = "<?php echo $json_from_php; ?>";

for (var i in jsonArray) {
    cy.add({
        data: { id: i }
        }
    );
    var source = i;
    cy.add({
        data: {
            id: i,
            source: source,
            target: (jsonArray[i] % 2 == 0 ? 'a' : 'b')
        }
    });
}

You probably also want to move to mysqli or PDO instead of mysql . 您可能还想转移到mysqliPDO而不是mysql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM