从javascript数组中解析数据并与简单的html dom结合使用

时间:2015-05-20 06:56:49

标签: javascript php arrays simple-html-dom

远程服务器中有一个js array源,它每天都在更新:

var io = new Array();
nsi[0] = new Array('','Frank','','Factory worker','Mercedes',374.0,26.2,76,181,'',75,'Audi',1456.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

nsi[1] = new Array('','Joseph','','Bank Assistant','Volvo',397,374.0,26.2,76,181,'',75,'BMW',1875.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

nsi[2] = new Array('','Travis','','Shop Assistant','Peugeot',397,374.0,26.2,76,181,'',75,'Ferrari',1866.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

nsi[3] = new Array('','Aaron','','Snow plower','Opel',397,374.0,26.2,76,181,'',75,'Lamborghini',1986.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

我已经用简单的html dom解析了一个网站(也使用了这个js数组的信息)(它是一个包含大量行的简单表,每行都有信息。没有问题。但是我想要从这个js数组源的每一行添加额外的信息。

示例:

row 1: x   d    s   f     + the extra informations from the js array  nsi[0]  nth elements, like Factory worker, 1456
row 2: z   3    e   g     + the extra informations from the js array  nsi[1]  nth elements  like Bank Assistant, 1875
row 3: 5   6    g   s     + the extra informations from the js array  nsi[2]  nth elements like Shop Assistant, 1866
row 4: g   b    z   3     + the extra informations from the js array  nsi[3]  nth elements  like Snow plower, 1986

这个js array正在远程服务器上运行,但我不知道如何与简单的html dom结合使用。 (我对js数组非常缺乏经验,如果有人解释我怎么能这样做,我会很高兴。)

    <?php

include ("mysql.php");
include_once('simple_html_dom.php');

ini_set('display_errors', true);
error_reporting(E_ALL);


    $html = file_get_html('source.php');

class persons {
        var $day;
    var $first_name;
    var $last_name;
        var $birth_city;


    function persons ($day, $first_name, $last_name, $birth_city){
                $this->first_name=$first_name;
        $this->last_name=$last_name;
                $this->birth_city=$birth_city;
        return $this;
    }
}

$i=0;

$day=$html->find('h3',0);
$day->plaintext;

foreach($html->find('table.workers') as $workers) 
{




    foreach ($workers->find('tr[class=white],tr[class=grey]') as $workersTR) {

$first_name=$workersTR->find('td[class=a1],td[class=a2]',0)->plaintext;
$last_name=$matchesTR->find('td',1)->plaintext;
$birth_city=$matchesTR->find('td[class=bc]');

$i++;

echo $first_name;
echo $last_name;
echo $birth_city;


    }
        }

?>

1 个答案:

答案 0 :(得分:0)

这是一个可能的基于JavaScript的解决方案。为简单起见,它是用常规javascript编写的,我删除了大部分值以保持示例简单。只需使用链接忽略代码;我不知道如何正确粘贴它。

http://jsfiddle.net/omikey/9gj0cyrt/1/

<table id="table">
</table>
<br>
Add Name: <input type="text" id="Name" />
Add Job: <input type="text" id="Job" />
Add Car: <input type="text" id="Car" />
<br>
<br>
<button type="button" onclick="addValues()">
    Submit
</button>

var nsi = new Array();
nsi[0] = new Array('','Frank','','Factory worker','Mercedes',374.0,26.2,76,181,'',75,'Audi',1456.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

nsi[1] = new Array('','Joseph','','Bank Assistant','Volvo',397,374.0,26.2,76,181,'',75,'BMW',1875.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

nsi[2] = new Array('','Travis','','Shop Assistant','Peugeot',397,374.0,26.2,76,181,'',75,'Ferrari',1866.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

nsi[3] = new Array('','Aaron','','Snow plower','Opel',397,374.0,26.2,76,181,'',75,'Lamborghini',1986.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);

window.refresh = function(){
var inner =
"<tr style='font-weight:600'>" +
    "<td>Name</td>" +
    "<td>Job</td>" +
    "<td>Car</td>" +
"</tr>"

for (var i = 0; i < nsi.length; i++)
{
    inner +=
        "<tr>" +
        "<td>" + nsi[i][1] + "</td>" +
        "<td>" + nsi[i][3] + "</td>" +
        "<td>" + nsi[i][12] + "</td>" +
    "</tr>"
}

var dom = document.getElementById("table");
dom.innerHTML = inner;
}

window.addValues = function() {
    var name = document.getElementById("Name").value;
    var job = document.getElementById("Job").value;
    var car = document.getElementById("Car").value;
    nsi[nsi.length] = new Array('',name,'',job,'Opel',397,374.0,26.2,76,181,'',75,car,1986.5,27.1,5 ,183,'',9,19,'','',16,3,4,4,4,3,4,0,4,4,4,2,1,9,1,2,3,2,1,1,4,2,0,0,2,1,2,4,3,4,5,'  3',' 16',55,25,25,55,25,25,3,4,4,4,4,4,4,3,4,3,2,4,2,4,2,3,3,2,2,3,3,3,3,2);
    refresh();
}

refresh();