簡體   English   中英

使用數據表和PHP

[英]use datatables and php

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

這就是我正在做的: http : //live.datatables.net/eduvin/edit#javascript,html,live

我使用的文件是一個txt文件,可使用php轉換為HTML。 PHP每兩小時在服務器上更新一次。 我想將所有這些替換為jquery數據表,並每兩個小時更新一次。 我使用的更新程序是Windows Server 2003中的Windows Task Scheduler。這可能嗎? 如果是這樣,那么這樣做的最佳方法是什么。

這是我的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);

 ?>

我認為您看錯了這條路。 為什么不讓php每次查看數據庫並渲染頁面? 大多數網站都是這樣做的,並且有很多示例。

無需中間文本文件。

php文件可以保持原樣,而不是打開並讀取csv文件,而是打開並讀取數據庫。 然后,您可以將結果放入fields數組中,因此無需更改其余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'];
}

這樣的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM