2017-10-15 87 views
-2

導出我的SQL數據庫轉換成文本(database.txt)PHP的txt文件,文件是這樣的:閱讀與像數據庫

id|name_sname|identity|address|region|hours|destination|price; 
-------------------------------------------------------------------- 
1|Ezra Mod|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
2|Ezra Mod|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
3|Ezra Mod|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
4|Ezra Mod|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
5|Ezra Mod|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
6|Ezra Mod|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 

,現在我想讀的PHP文件,並把結果從各行中另一個文件:contracts.php ID = 1,其中id需要從我database.txt, 我嘗試閱讀database.txt在此模式下:

$data = file_get_contents("database.txt"); //read the file 
$convert = explode("|", $data); //create array separate by new line 

foreach($convert_row as $rand) { 
    echo $rand; 
    echo "<br /><hr /></br />"; 
} 

什麼需要做的創建變量對於每個值afther /在「|」之前和每一行?像這樣: $ id,$ name,....

+2

請在他們解決您的問題時接受答案。 https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work(對於你所有的問題,不只是這一個,例如https://stackoverflow.com/questions/41934091/2-循環與隨機和同一後,wordpress和https://stackoverflow.com/questions/29769831/multiple-if-in-if-with-get-url) – chris85

+2

社區工作,雙向和isn不是一條單行道。你需要回饋它。 –

回答

2

我認爲保持數據庫將是一個更好的方法,無論是性能和功能。

你的問題,你的第二行開始,註釋有不正確的:

$convert = explode("|", $data); //create array separate by new line 

不是由新線分割。它正在分裂| s,這會破壞你的列,但是不可能找到列以這種方式結束/開始的地方。您需要拆分新行或使用file()來讀取文件。然後您可以迭代數據並構建變量或數組。

$lines = explode(PHP_EOL, $data); 
foreach($lines as $key => $line){ 
    $convert = explode("|", $line); //create array separate by new line 
    if($key == 0) { 
     foreach($convert as $column_name) { 
      $columns[] = $column_name; 
     } 
    } 
    if(is_numeric($convert[0])) { 
     $data[$columns[0]][] = $convert[0]; 
    } 
} 

演示:https://3v4l.org/NIPRE

is_numeric是忽略


文件0將是id所以應該總是一個整數。

1

這裏是你爲你的數據庫解決方案風格: 活生生的例子:https://3v4l.org/Xhr68

<?php 
$data = file_get_contents("database.txt"); //read the file 
$rows = explode("\n", $data); //create array separate by new line 
$rows = array_map("trim", $rows); // for removing any unwanted space 
$rowCount = count($rows); // Count your database rows 

function CountCol($data){ 
    $col = explode("|", $data); 
    return count($col); 

} 

// Creating array from our text database 
// in our database first two rows are not needed so I am skipig this 
// by using i = 2 
// and $rowCount -1 because of our array start from 0 but count start from 1 
// adding $id[$i-2] and like other because of we have started our first loop from 2 and I want to 
// create my new array starting from 0 . 

for ($i=2; $i <$rowCount-1 ; $i++) { 
    for ($j=0; $j < CountCol($rows[$i]) ; $j++) { 
     $column = explode("|", $rows[$i]); 

     $id[$i-2] = $column[0]; 
     $name_sname[$i-2] = $column[1]; 
     $identity[$i-2] = $column[2]; 
     $address[$i-2] = $column[3]; 
     $region[$i-2] = $column[4]; 
     $hours[$i-2] = $column[5]; 
     $destination[$i-2] = $column[6]; 
     $price[$i-2] = $column[7]; 
    } 


} 

// let's print our data 
// I am using 3 here because of we have no need first 2 rows and our array start from 0 
for ($i=0; $i < $rowCount-3 ; $i++) { 

      echo "ID: ".$id[$i] ."<br>"; 
      echo "name_sname: ".$name_sname[$i]."<br>"; 
      echo "identity: ".$identity[$i] ."<br>"; 
      echo "address: ".$address[$i] ."<br>"; 
      echo "region: ".$region[$i] ."<br>"; 
      echo "hours: ".$hours[$i] ."<br>"; 
      echo "destination: ".$destination[$i] ."<br>"; 
      echo "price: ".$price[$i] ."<br>"; 

} 

// 
// get it like below 

echo "printing information using id number <br>"; 

echo $name_sname[1]; // here 1 is row number or id number 
echo $name_sname[2]; 
echo "<br>"; 


?> 

//但我喜歡創建數據庫像下面。這意味着不需要第一個兩行的列名稱和 - 行

1|Ezra Mod1|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
2|Ezra Mod2|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
3|Ezra Mod3|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
4|Ezra Mod4|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
5|Ezra Mod5|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 
6|Ezra Mod6|123456789|husi, stanilesti, vaslui|HUSI - IASI|20:00|HUSI|100; 

如果您創建數據庫,如下所示是解決方案。

<?php 
$data = file_get_contents("database.txt"); //read the file 
$rows = explode("\n", $data); //create array separate by new line 
$rows = array_map("trim", $rows); // for removing any unwanted space 
$rowCount = count($rows); // Count your database rows 

function CountCol($data){ 
    $col = explode("|", $data); 
    return count($col); 
} 

// Creating our array database 

for ($i=0; $i <$rowCount-1 ; $i++) { 
    for ($j=0; $j < CountCol($rows[$i]) ; $j++) { 
     $column = explode("|", $rows[$i]); 

     $id[$i] = $column[0]; 
     $name_sname[$i] = $column[1]; 
     $identity[$i] = $column[2]; 
     $address[$i] = $column[3]; 
     $region[$i] = $column[4]; 
     $hours[$i] = $column[5]; 
     $destination[$i] = $column[6]; 
     $price[$i] = $column[7]; 
    } 


} 

// now we have full control and everything dynamic let's print 
for ($i=0; $i < $rowCount-1 ; $i++) { 

      echo "ID: ".$id[$i] ."<br>"; 
      echo "name_sname: ".$name_sname[$i]."<br>"; 
      echo "identity: ".$identity[$i] ."<br>"; 
      echo "address: ".$address[$i] ."<br>"; 
      echo "region: ".$region[$i] ."<br>"; 
      echo "hours: ".$hours[$i] ."<br>"; 
      echo "destination: ".$destination[$i] ."<br>"; 
      echo "price: ".$price[$i] ."<br>"; 

} 

// 
// get it like below 

echo "Tow number<br>"; 

echo $name_sname[1]; // here 1 is row number or id number 
echo $name_sname[2]; 

?>