2010-11-29 34 views
0

製品情報のあるテキストファイルから読んでいます。異なるコンピュータの4つのテーブルがあります。コンピュータ1は、file1から読み込まれるテーブル1を表示し、コンピュータ2は、ファイル2などのようなものですが何らかの理由でコンピュータ3 & 4表2の情報も表示します3 & 4私のコードはより良い見方をすることができますか?ファイルから表に表示されたデータを繰り返す

表1

<?php 
    error_reporting(E_ALL^E_NOTICE); 
    $row = 0; 
    $sortby =''; 
    $sortkey=''; 

echo '<h1><span class="sortby"></span></h1> 
<table cellspacing="5" summary="QA Tables"> 
<tr> 
<th>Products</th> 
<th>Types</th> 
<th>Keys</th> 
<th>Date & Time Added</th> 
</tr>'; 

$fp = fopen('key_QA_N1.txt','r'); 
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} 

while (!feof($fp)) { 
    $line = fgets($fp,1024); //use 2048 if very long lines 
    $row++; 
    list ($products,$types, $keys, $date) = explode('|', $line); 
    if ($sortby == 'Products') $sortkey = strtolower($products); 
    if ($sortby == 'Types') $sortkey = strtolower($types); 
    if ($sortby == 'Keys') $sortkey = strtolower($keys); 
    if ($sortby == 'Date & Time Added') $sortkey = strtolower($date); 
     $col[$row] = array($sortkey, $products, $types, $keys, $date); 
} 

fclose($fp); 

$arrays = count($col) - 1; 

$loop = 0; 
while ($loop < $arrays) { 
    $loop++; 
    echo ' 
<tr> 
<td>'.$col[$loop][1].'</td> 
<td>'.$col[$loop][2].'</td> 
<td>'.$col[$loop][3].'</td> 
<td>'.$col[$loop][4].'</td> 
</tr>'; 
} 

echo ' 
</table> 
' 
?> 

表2

<?php 
    error_reporting(E_ALL^E_NOTICE); 
    $row = 0; 
    $sortby =''; 
    $sortkey=''; 

echo '<h1><span class="sortby"></span></h1> 
<table cellspacing="5" summary="QA Tables"> 
<tr> 
<th>Products</th> 
<th>Types</th> 
<th>Keys</th> 
<th>Date & Time Added</th> 
</tr>'; 

$fp = fopen('key_QA_N2.txt','r'); 
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} 

while (!feof($fp)) { 
    $line = fgets($fp,1024); //use 2048 if very long lines 
    $row++; 
    list ($products,$types, $keys, $date) = explode('|', $line); 
    if ($sortby == 'Products') $sortkey = strtolower($products); 
    if ($sortby == 'Types') $sortkey = strtolower($types); 
    if ($sortby == 'Keys') $sortkey = strtolower($keys); 
    if ($sortby == 'Date & Time Added') $sortkey = strtolower($date); 
     $col[$row] = array($sortkey, $products, $types, $keys, $date); 
} 

fclose($fp); 

$arrays = count($col) - 1; 

$loop = 0; 
while ($loop < $arrays) { 
    $loop++; 
    echo ' 
<tr> 
<td>'.$col[$loop][1].'</td> 
<td>'.$col[$loop][2].'</td> 
<td>'.$col[$loop][3].'</td> 
<td>'.$col[$loop][4].'</td> 
</tr>'; 
} 

echo ' 
</table> 
' 
?> 

表3

<?php 
    error_reporting(E_ALL^E_NOTICE); 
    $row = 0; 
    $sortby =''; 
    $sortkey=''; 

echo '<h1><span class="sortby"></span></h1> 
<table cellspacing="5" summary="QA Tables"> 
<tr> 
<th>Products</th> 
<th>Types</th> 
<th>Keys</th> 
<th>Date & Time Added</th> 
</tr>'; 

$fp = fopen('key_QA_N3.txt','r'); 
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} 

while (!feof($fp)) { 
    $line1 = fgets($fp,1024); //use 2048 if very long lines 
    $row++; 
    list ($products1,$types1, $keys1, $date1) = explode('|', $line1); 
    if ($sortby == 'Products') $sortkey = strtolower($products1); 
    if ($sortby == 'Types') $sortkey = strtolower($types1); 
    if ($sortby == 'Keys') $sortkey = strtolower($keys1); 
    if ($sortby == 'Date & Time Added') $sortkey = strtolower($date1); 
    $col[$row] = array($sortkey, $products1, $types1, $keys1, $date1); 
} 

fclose($fp); 

$arrays = count($col) - 1; 

$loop = 0; 
while ($loop < $arrays) { 
    $loop++; 
    echo ' 
<tr> 
<td>'.$col[$loop][1].'</td> 
<td>'.$col[$loop][2].'</td> 
<td>'.$col[$loop][3].'</td> 
<td>'.$col[$loop][4].'</td> 
</tr>'; 
} 

echo ' 
</table> 
' 
?> 

表4

<?php 
    error_reporting(E_ALL^E_NOTICE); 
    $row = 0; 
    $sortby =''; 
    $sortkey=''; 

echo '<h1><span class="sortby"></span></h1> 
<table cellspacing="5" summary="QA Tables"> 
<tr> 
<th>Products</th> 
<th>Types</th> 
<th>Keys</th> 
<th>Date & Time Added</th> 
</tr>'; 

$fp = fopen('key_QA_N4.txt','r'); 
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} 

while (!feof($fp)) { 
    $line2 = fgets($fp,1024); //use 2048 if very long lines 
    $row++; 
    list ($products2,$types2, $keys2, $date2) = explode('|', $line2); 
    if ($sortby == 'Products') $sortkey = strtolower($products2); 
    if ($sortby == 'Types') $sortkey = strtolower($types2); 
    if ($sortby == 'Keys') $sortkey = strtolower($keys2); 
    if ($sortby == 'Date & Time Added') $sortkey = strtolower($date2); 
     $col[$row] = array($sortkey, $products2, $types2, $keys2, $date2); 
} 

fclose($fp); 

$arrays = count($col) - 1; 

$loop = 0; 
while ($loop < $arrays) { 
    $loop++; 
    echo ' 
<tr> 
<td>'.$col[$loop][1].'</td> 
<td>'.$col[$loop][2].'</td> 
<td>'.$col[$loop][3].'</td> 
<td>'.$col[$loop][4].'</td> 
</tr>'; 
} 

echo ' 
</table> 
' 
?> 

敬具E.P

答えて

0

各セクションの冒頭に$col = array();を追加してみてください。

+0

あなたは配列の内容をリセットすることを意味します$ col = array(); –

+0

ありがとうございます。私はRubyを長時間使っています:P – rwilliams

+0

ありがとうございました –

0

私はそれがLimpepが提案するものであることを賭けるだろうが、私はよりよい解決策を提案します:

error_reporting(E_ALL^E_NOTICE); 
// create a wrapper function to prevent duplicate code! 
function displayQANFile($fname, $sortby = '') 
{ 
    $col = array(); 
    // NOTE: $row, $loop, and $arrays are no longer needed. 
    echo '<h1><span class="sortby"></span></h1> 
    <table cellspacing="5" summary="QA Tables"> 
    <tr> 
    <th>Products</th> 
    <th>Types</th> 
    <th>Keys</th> 
    <th>Date & Time Added</th> 
    </tr>'; 

    $fp = fopen($fname,'r'); 
    if (!$fp) {echo 'ERROR: Unable to open file ' . $fname . '.</table></body></html>'; exit;} 

    while (!feof($fp)) { 
     $line2 = fgets($fp,1024); //use 2048 if very long lines 
     list ($products2,$types2, $keys2, $date2) = explode('|', $line2); 
     if ($sortby == 'Products') $sortkey = strtolower($products2); 
     if ($sortby == 'Types') $sortkey = strtolower($types2); 
     if ($sortby == 'Keys') $sortkey = strtolower($keys2); 
     if ($sortby == 'Date & Time Added') $sortkey = strtolower($date2); 
     // did you mean to call array_sort on col at some point? 
     $col[] = array($sortkey, $products2, $types2, $keys2, $date2); 
    } 
    fclose($fp); 

    foreach($col as $row) { 
     echo ' 
    <tr> 
    <td>'.$row[1].'</td> 
    <td>'.$row[2].'</td> 
    <td>'.$row[3].'</td> 
    <td>'.$row[4].'</td> 
    </tr>'; 
    } 

    echo ' 
    </table> 
    '; 
} 
displayQANFile('key_QA_N1.txt'); 
displayQANFile('key_QA_N2.txt'); 
displayQANFile('key_QA_N3.txt'); 
displayQANFile('key_QA_N4.txt'); 

この方法では、あなたは別の、あなたを上書きする変数を持っていないことを確認することができますがその複製されたコードのすべてを防ぐことができます...

+0

すべてのデータとテーブルを別々にしておきたいのは、簡単に管理してMVCパターン –

関連する問題