2015-05-20 46 views
0

有其中由每天更新一天遠程服務器的js array來源:解析數據,並用簡單的HTML DOM結合

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); 

我已經做了解析的網站(也使用這個信息js數組)簡單的html dom(它是一個有很多行的簡單表,並且每行都有信息,沒有問題,但我想從這個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

我更新了我與完成的代碼問題。我希望有人能幫助我。 – Terminus

回答

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(); 
+0

嗨,謝謝你的回答,但我需要從遠程服務器的array.js文件中添加額外的信息。一個例子:我需要每個數組的第5個和第12個數據(或第x個),我必須添加到我完成的簡單html dom查詢行。我希望我很清楚。在我回應結果之後,我必須補充一下。喜歡:名字,姓氏,出生城市+從js數組到每一行的額外信息 – Terminus

相關問題