2016-10-20 425 views
-1

我正試圖在工作中創建一個迷你「評論板」。我們中有10個人,我希望我們每個人都能夠添加項目,讓每個人都可以查看。這個想法是把它構造成一個HTML網頁(僅供內部使用),數據被寫入和讀取將駐留在服務器上相同文件夾中的CSV文件並從中讀取。我想在網頁上將其顯示爲表格,並使個人能夠添加新的條目,這些條目將寫入CSV文件並顯示在表格中。我對基本的HTML沒問題,但我不知道如何去閱讀和寫入CSV文件。我已閱讀了關於Javascript,PHP,SQL等的一些內容,但我不確定最佳方法。任何指導(包括指向代碼工作示例的指針)都將非常感謝。從網頁中讀取/寫入.CSV文件

+0

如果你有SQL,爲什麼你不想把它作爲表存儲? – Terry

+0

我不認爲我有SQL!只是我閱讀過的許多工具之一。 – Richard

+0

如果您想要一個非常簡單的解決方案,爲什麼不要使用久經考驗的解決方案,如Google Docs? – Terry

回答

0

如果你想只用Javascript做到這一點,你可以使用Node.Js服務器來檢索和存儲所有消費者的數據。但是,即使您創建了PHP,Node.Js,Ruby,Python(...)腳本,您也必須開發API以便能夠檢索數據並將它們存儲在單個數據庫中(CSV,MySQL,MongoDB,等等。)。

實時可能是一個很好的解決方案,類似Pusher。所有這些都取決於你真正想要的東西。有一些關於RestAPI的文檔,有或沒有some PHP frameworks

+0

對不起,但我對這一切都是全新的,所以我實際上並不瞭解上述任何一點!我曾希望可能有一個非常簡單的解決方案,可以讓我從CSV文件中獲取數據並將其顯示在HTML表格中 - 也許只是我可以嵌入到HTML中的幾行JavaScript代碼? – Richard

0

您可以將下面的代碼作爲您的迷你電路板的起點。只需將構造函數傳遞給可由Web服務器寫入的路徑即可。

以下只是採取一種形式,它將採取由時間戳,名稱和評論組成的消息。發佈後,這將保存到CSV文件。

所有註釋都存儲在一個數組中,並且每次添加註釋時都將其添加到數組中,然後將整個數組作爲CSV保存到磁盤。

沒有備份董事會,沒有文件鎖定,它缺乏技巧。 也沒有用戶登錄系統或任何東西。我會把它作爲你的練習。

它可以作爲您可以自由定製的純html/php解決方案。

<?php 
class Board 
{ 
    public $path_to_csv; 
    public $comments = []; 

    public function __construct($path_to_csv) 
    { 
     $this->path_to_csv = $path_to_csv; 
     $this->load(); 
    } 

    public function load() 
    { 
     if(!is_file($this->path_to_csv)) 
      file_put_contents($this->path_to_csv, ''); 
     if (($fh = fopen($this->path_to_csv, "r")) !== FALSE) { 
      while (($data = fgetcsv($fh)) !== FALSE) { 
       $this->comments[] = $data; 
      } 
      fclose($fh); 
     } 
    } 

    public function addComment($ts, $username, $comment) 
    { 
     $this->comments[] = array($ts, $username, $comment); 
     $this->save(); 
    } 

    public function getComments() 
    { 
     return $this->comments; 
    } 

    public function save() 
    { 
     $fp = fopen($this->path_to_csv, 'w'); 
     foreach($this->comments as $comment) { 
      fputcsv($fp, $comment); 
     } 
     fclose($fp); 
    } 
} 

class ViewHelpers 
{ 
    public function form($action = '', $ts) 
    { 
     ?> 
     <form method="POST" action="<?=$action ?>"> 
      <input type="hidden" name="ts" value="<?=$ts ?>"> 
      <label for="username">Name:</label> 
      <input type="text" name="username"><br /> 
      <label for="comment">Comment:</label> 
      <textarea name="comment"></textarea> 
      <input type="submit" name="submit" value="Submit comment"> 
     </form> 
     <?php 
    } 
    public function escape($string) 
    { 
     return htmlspecialchars($string); 
    } 
    public function table(array $comments) 
    { 
     if(!count($comments)) return; // No comments equals don't show. 
     //$comments = array_reverse($comments); 
     ?> 
     <table> 
      <thead> 
       <tr> 
        <th> 
         Date 
        </th> 
        <th> 
         User 
        </th> 
        <th> 
         Comment 
        </th> 
       </tr> 
      </thead> 
      <tbody> 
      <?php foreach($comments as $comment) { ?> 
       <tr> 
        <td> 
         <?=date('r', $this->escape($comment[0])); ?> 
        </td> 
        <td> 
         <?=$this->escape($comment[1]); ?> 
        </td> 
        <td> 
         <?=nl2br($this->escape($comment[2])); ?> 
        </td> 
       </tr> 
      <?php } ?> 
      </tbody> 
     </table> 
     <?php 
    } 
    public function feedback($string) 
    { 
     if(!$string) return; 
     ?> 
     <p> 
      <?=$this->escape($string); ?> 
     </p> 
     <?php 
    } 
} 

// Path to where you want to store your csv file. 
$board = new Board('/tmp/comments.csv'); 
$feedback = ''; 

// Process form submissions. 
if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $ts = isset($_POST['ts']) ? $_POST['ts'] : null; 
    $username = isset($_POST['username']) ? $_POST['username'] : null; 
    $comment = isset($_POST['comment']) ? $_POST['comment'] : null; 
    if(!empty($username) && !empty($comment)) { 
     $board->addComment($ts, $username, $comment); 
    } 
    else { 
     $feedback = 'Please fill in all fields.'; 
    } 
} 
$viewHelpers = new ViewHelpers; 
// Your html here. 
$viewHelpers->table($board->getComments()); 
$viewHelpers->feedback($feedback); 
$viewHelpers->form("", time()); 
+0

評論時間是在呈現表單時設置的,這可能不是您想要的。要改變提交評論的時間,只需在表單處理塊中放置'$ ts = time();',忽略發佈的值。 – Progrock