2016-02-05 75 views
0

每個人我有以下文件,我想顯示匹配的條件,並通過其他。PHP讀取文件匹配,如果語句

我有這個TXT文件:

Doc. number|Date|Price|Description|Name 
100|11/11/2015|99|Test 1|Alex 
101|11/11/2015|120|Test 2 
102|11/11/2015|100|Test 3|John 
102|11/11/2015|140|| 
103|11/11/2015|110|Test 4| 

,這是我的PHP代碼:

$file_handle = fopen("file.txt", "rb"); 
$i = 0; 
echo "<table border='1'>"; 
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>"; 
while (!feof($file_handle)) { 
    $line_of_text = fgets($file_handle); 
    $parts = explode('|', $line_of_text); 

    if($i > 1) { // Pass the first line 
     echo "<tr>"; 
      echo "<td>" . $parts[0] . "</td>"; // Doc. number 
      echo "<td>" . $parts[1] . "</td>"; // Date 
      echo "<td>" . $parts[2] . "</td>"; // Price 
      echo "<td>" . $parts[3] . "</td>"; // Description 
      echo "<td>" . $parts[4] . "</td>"; // Name 

     echo "</tr>"; 
    } 
    $i++; 
} 

fclose($file_handle); 
echo "</table>" 

如何檢查是否有表中沒有「說明」和/或「姓名」並通過這條線。我想顯示(獲得)只有線條匹配的條件。

如果有人有想法,我將非常感激。提前致謝。

+0

你知道'$部分[3]'和'$零件[4]''總是有分別與Description''Name'。所以在每次迭代中檢查它們。如果它們存在於數組中,執行if-body,則不傳遞迭代。 –

回答

1

就這麼簡單

$file_handle = fopen("file.txt", "rb"); 
$i = 0; 
echo "<table border='1'>"; 
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>"; 
while (!feof($file_handle)) { 
    $line_of_text = fgets($file_handle); 
    $parts = explode('|', $line_of_text); 

    if($i > 1 && !empty($parts[3]) && !empty($parts[4])) { // Pass the first line and lines without description/name 
     echo "<tr>"; 
      echo "<td>" . $parts[0] . "</td>"; // Doc. number 
      echo "<td>" . $parts[1] . "</td>"; // Date 
      echo "<td>" . $parts[2] . "</td>"; // Price 
      echo "<td>" . $parts[3] . "</td>"; // Description 
      echo "<td>" . $parts[4] . "</td>"; // Name 

     echo "</tr>"; 
    } 
    $i++; 
} 

fclose($file_handle); 
echo "</table>" 
1

只打印表行,如果我們有名稱和說明:

if($i > 1 && $parts[3] && $parts[4]) { 
1

你可以把條件echo語句之前,如果它是假的就跳過「回聲」 ;

if (count($parts) === 5) { 
    $error = 0; 
    foreach ($parts as $part) { 
     if (empty($part)) error++; 
    } 

    if($i > 1 && $error === 0) { 
      echo "<tr>"; 
       echo "<td>" . $parts[0] . "</td>"; // Doc. number 
       echo "<td>" . $parts[1] . "</td>"; // Date 
       echo "<td>" . $parts[2] . "</td>"; // Price 
       echo "<td>" . $parts[3] . "</td>"; // Description 
       echo "<td>" . $parts[4] . "</td>"; // Name 

      echo "</tr>"; 
     } 
} 
1

我有一個解決方案,可以幫助你。
但爲什麼我覺得你只需要琢磨標題行。所以我改變如果($ I> 1)是如果($ I> 0)

$file_handle = fopen("file.txt", "rb"); 
$i = 0; 
echo "<table border='1'>"; 
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>"; 
while (!feof($file_handle)) { 
    $line_of_text = fgets($file_handle); 
    $parts = explode('|', $line_of_text); 
    if($i > 0) { // Pass the first line 
     if ((!empty($parts[3])) && (!empty($parts[4]))){ 
      echo "<tr>"; 
       echo "<td>" . $parts[0] . "</td>"; // Doc. number 
       echo "<td>" . $parts[1] . "</td>"; // Date 
       echo "<td>" . $parts[2] . "</td>"; // Price 
       echo "<td>" . $parts[3] . "</td>"; // Description 
       echo "<td>" . $parts[4] . "</td>"; // Name 
      echo "</tr>"; 
     } 
    } 
    $i++; 
} 
fclose($file_handle); 
echo "</table>" 
+0

這會得到你:Alex&John Rows。 –

1

你的文件具有CSV結構,管道分隔。
因此將其解析爲CSV

請注意使用array_shift獲取CSV的標題並將分隔符參數傳遞給fgetcsv

也可以考慮使用implode而不是明確地在td標籤之間傳遞數組中的每個成員。

下面是一個使用兩個函數的例子,一個用於解析CSV並返回數據,另一個用於顯示數據並進行驗證。

function getData($file){ 

    $rows = array(); 

    if (($handle = fopen($file, "r")) !== FALSE) { 
     while (($data = fgetcsv($handle, null, "|")) !== FALSE) { 

      $rows[] = $data; 
     } 
     fclose($handle); 
    } 

    return $rows; 

} 

function displayData($rows){ 

    $header = array_shift($rows); 
    $output = '<table border="1">' . PHP_EOL; 
    $output .= '<tr><th>' . implode('</th><th>',$header) . '</th></tr>' . PHP_EOL; 

    foreach($rows as $row){ 

     if (isset($row[3]) and isset($row[4]) and $row[3]!='' and $row[4]!=''){ 
      $output .= '<tr><td>' . implode('</td><td>',$row) . '</td></tr>' . PHP_EOL; 
     } 

    } 

    $output .= '</table>'; 
    return $output; 

} 

$rows = getData("pipe.txt"); 

print displayData($rows); 

這將輸出以下

<table border="1"> 
<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr> 
<tr><td>100</td><td>11/11/2015</td><td>99</td><td>Test 1</td><td>Alex</td></tr> 
<tr><td>102</td><td>11/11/2015</td><td>100</td><td>Test 3</td><td>John</td></tr> 
</table>