2013-10-30 18 views
0

我有一個php代碼,目前從txt文件創建一個html文件,並更新每兩個小時在我的網站上。我想將其轉換爲數據表。有沒有簡單的方法鏈接到使用jQuery,數據表和PHP的txt文件?使用數據表和php

這是我提出:http://live.datatables.net/eduvin/edit#javascript,html,live

,我使用的是一種將使用PHP爲HTML一個txt文件的文件。 PHP每兩小時在服務器上更新一次。我想用jQuery數據表替換所有這些,並每兩個小時更新一次。我使用的更新程序是Windows Server 2003中的Windows任務計劃程序。這可能嗎?如果是的話,那麼最好的辦法是什麼。

這是我的PHP代碼:

<?php 
set_time_limit(0); 
function csv_split($line,$delim=',',$removeQuotes=true) { 
#$line: the csv line to be split 
#$delim: the delimiter to split by 
#$removeQuotes: if this is false, the quotation marks won't be removed from the fields 
$fields = array(); 
$fldCount = 0; 
$inQuotes = false; 
for ($i = 0; $i < strlen($line); $i++) { 
    if (!isset($fields[$fldCount])) $fields[$fldCount] = ""; 
    $tmp = substr($line,$i,strlen($delim)); 
    if ($tmp === $delim && !$inQuotes) { 
     $fldCount++; 
     $i += strlen($delim)-1; 
    } else if ($fields[$fldCount] == "" && $line[$i] == '"' && !$inQuotes) { 
     if (!$removeQuotes) $fields[$fldCount] .= $line[$i]; 
     $inQuotes = true; 
    } else if ($line[$i] == '"') { 
     if ($line[$i+1] == '"') { 
      $i++; 
      $fields[$fldCount] .= $line[$i]; 
     } else { 
      if (!$removeQuotes) $fields[$fldCount] .= $line[$i]; 
      $inQuotes = false; 
     } 
    } else { 
     $fields[$fldCount] .= $line[$i]; 
    } 
} 
return $fields; 
} 
$html_body = 'HTML goes here' 
$fp=fopen("csv/inventory4.html",'w'); 
$write=fputs($fp,$html_body,strlen($html_body)); 
$i=0; 
$content = file("webinvt.txt"); 
foreach($content as $line) 
{ 
$l=csv_split($line); 
if(!strstr($l[11],"SET")) 
{ 
if($i==10) 
{ 
    $tmp = '</table> 
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">  <tbody><tr> 
    '; 
    $write=fputs($fp,$tmp,strlen($tmp)); 
    $i=0; 
} 
$onhand = (int)$l[15]; 
$committed = (int)$l[16]; 
$avail = $onhand - $committed; 
$wcdate = substr($l[23],4); 
$eastdate = substr($l[19],4); 

if(strstr($l[1],"DISC")) 
{ 
    $html_body ='<tr style="color:#FF0000 "> 
    <td width="12%" >'.$l[0].'</td> 
    <td width="30%" >'.$l[1].'</td> 
    <td width="8%" >'.$l[12].'</td> 
    <td width="8%" >'.$avail.'</td> 
    <td width="8%" >'.$l[17].'</td> 
    <td width="8%" >'.$l[18].'</td> 
    <td width="8%" >'.$eastdate.'</td> 
    <td width="8%" >'.$l[21].'</td> 
    <td width="8%" >'.$l[22].'</td> 
    <td width="8%" >'.$wcdate.'</td> 
    </tr>'; 
} 
else 
{ 
    $html_body ='<tr> 
    <td width="12%" >'.$l[0].'</td> 
    <td width="30%" >'.$l[1].'</td> 
    <td width="8%" >'.$l[12].'</td> 
    <td width="8%" >'.$avail.'</td> 
    <td width="8%" >'.$l[17].'</td> 
    <td width="8%" >'.$l[18].'</td> 
    <td width="8%" >'.$eastdate.'</td> 
    <td width="8%" >'.$l[21].'</td> 
    <td width="8%" >'.$l[22].'</td> 
    <td width="8%" >'.$wcdate.'</td> 
    </tr> 
    '; 
} 

$write=fputs($fp,$html_body,strlen($html_body)); 
$i++; 
} 
} 

$html_body='<tr> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
<td scope="col"></td> 
</tr> 

</table> 
</body> 
</html>'; 

$write=fputs($fp,$html_body,strlen($html_body)); 

fclose($fp); 

?> 

回答

0

我認爲你正在尋找這個錯誤的方式。爲什麼不讓php查看數據庫並每次渲染頁面?這是大多數網站所做的,並且有很多例子。

不需要中間文本文件。

php文件可以保持原樣,而不是打開和閱讀您打開並閱讀數據庫的csv文件。然後你可以把結果放到字段數組中,所以PHP文件的其餘部分不需要改變。

這是一個很好的參考,以連接到數據庫:

此處作爲一例是一些代碼:

$res = $mysqli->query("SELECT field1,field2,field3 FROM items"); 

$res->data_seek(0); 
while ($row = $res->fetch_assoc()) { 
    $fields[0] = $row['field1']; 
    $fields[1] = $row['field2']; 
    $fields[2] = $row['field3']; 
} 

類似的東西。

+0

這就是我的想法,但我不知道如何去掉txt文件,因爲它包含了我所有的數據。 – user458474

+0

你有沒有例子?它應該很容易。 –

+0

我加了php代碼看一下 – user458474