2012-11-28 29 views
5

我想在一個文本文件來記錄下載增量數

有人來到我的網站,下載的東西,它會添加一個新行到文本文件,如果它不是已經或增加當前一。

我已經試過

$filename = 'a.txt'; 
$lines = file($filename); 
$linea = array(); 

foreach ($lines as $line) 
{ 
    $linea[] = explode("|",$line); 
} 

$linea[0][1] ++; 

$a = $linea[0][0] . "|" . $linea[0][1]; 

file_put_contents($filename, $a); 

但它總是超過1

增加它的文本文件格式是

name|download_count 
+0

「.txt」文件中有多少行......? – NazKazi

+1

發佈你的'txt'文件的一些示例內容 –

+0

一個數據庫比這個文本文件要容易得多 – 2012-11-28 05:49:38

回答

2

你做你遞增for循環外,並且只能訪問[0] th元素,所以在其他地方沒有任何變化。

這也許應該是這個樣子:

$filename = 'a.txt'; 
$lines = file($filename); 

// $k = key, $v = value 
foreach ($lines as $k=>$v) { 
    $exploded = explode("|", $v); 

    // Does this match the site name you're trying to increment? 
    if ($exploded[0] == "some_name_up_to_you") { 
     $exploded[1]++; 

     // To make changes to the source array, 
     // it must be referenced using the key. 
     // (If you just change $v, the source won't be updated.) 
     $lines[$k] = implode("|", $exploded); 
    }   
} 

// Write. 
file_put_contents($filename, $lines); 

你或許應該使用這個數據庫,雖然。看看PDO和MYSQL,你會走向迷人的路。


編輯

要你在你的評論中提到的,你可以設置一個布爾標誌,並觸發它,你走過的陣列。這可以保證一個break也一樣,如果你只是在尋找一兩件事:它存儲在後您使用的是foreach循環

... 
$found = false; 
foreach ($lines as $k=>$v) { 
    $exploded = explode("|", $v); 

    if ($exploded[0] == "some_name_up_to_you") { 
     $found = true; 
     $exploded[1]++; 
     $lines[$k] = implode("|", $exploded); 
     break; // ??? 
    }   
} 

if (!$found) { 
    $lines[] = "THE_NEW_SITE|1"; 
} 

... 
+0

偉大的作品謝謝你,有沒有辦法,我可以檢查,如果線路先退出,增量或添加新行? –

+0

沒問題,歡迎來到SO。給我第二個找出你說的話:) – Ben

+0

好,我通常使用MySQL作爲後端,但即時通訊與我的主機問題,所以即時通訊使用文本文件,直到它被分類。 –

0

一隻手,另一隻手你是隻寫在第一行到您的文件$a ...它讓我迷惑你有什麼在你.txt文件...

試試這個下面的代碼...希望這本書能解決你的問題......

$filename = 'a.txt'; 
// get file contents and split it... 
$data = explode('|',file_get_contents($filename)); 
// increment the counting number... 
$data[1]++; 
// join the contents... 
$data = implode('|',$data); 
file_put_contents($filename, $data); 
+0

我希望它爲每行每個文件都有一個新的計數器 –

0

而不是創造的Y我們自己的結構在文本文件中,爲什麼不使用PHP數組來跟蹤?你也應該適用適當的鎖定以防止競爭條件:

function recordDownload($download, $counter = 'default') 
{ 
    // open lock file and acquire exclusive lock 
    if (false === ($f = fopen("$counter.lock", "c"))) { 
      return; 
    } 
    flock($f, LOCK_EX); 

    // read counter data 
    if (file_exists("$counter.stats")) { 
      $stats = include "$counter.stats"; 
    } else { 
      $stats = array(); 
    } 

    if (isset($stats[$download])) { 
     $stats[$download]++; 
    } else { 
     $stats[$download] = 1; 
    } 

    // write back counter data 
    file_put_contents('counter.txt', '<?php return ' . var_export($stats, true) . '?>'); 

    // release exclusive lock 
    fclose($f); 
} 

recordDownload('product1'); // will save in default.stats 
recordDownload('product2', 'special'); // will save in special.stats 
+0

因爲我需要對計數器進行排序以顯示最熱門下載屬性。 –

+0

@BrendanMullan在任何情況下,我確信你可以編寫一個shell腳本,我希望我的答案的其他部分涵蓋諮詢鎖定幫助。 –

+0

@BrendanMullan我用更通用的解決方案更新了我的答案;它會跟蹤一個文件中的所有下載計數。 –

0

我個人建議使用json blob作爲文本文件的內容。那麼你可以讀取文件到PHP中,解碼它(json_decode),處理數據,然後重新保存它。