2016-11-30 98 views
1

我曾試圖產生從PHP foreach循環txt文件, 它的工作原理,但結果是不是水貨一樣,如果使用的表在這裏:PHP生成foreach循環的數據上的txt文件,表格格式

我的期望是這樣的:

這是我的代碼:

 $output[] = ''."\n".''; 
    $output[] = ' No '.'     Name Product     '.' Total '."\n".''; 
    foreach($query->result() as $row){ 
     $results = $row->id_product; 
     $product = $this->db->query("select product_name from master_product WHERE product_id='".$results."'"); 
     $product_name = $product->row()->product_name; 
     $output[] = ' '.$no.str_repeat(" ",20).''. $product_name.str_repeat(" ",20).''.$row->quantity.' '.$row->scale."\n".''; 
     $no++; 
    } 
    file_put_contents(APPPATH."txt/test.txt", $output); 

請別人幫我。

+0

間距不會與文本文件中的動態內容一起使用。 – Naga

回答

0

你爲什麼不實現這樣的功能:

function padColumnText($text, $colWidth) 
{ 
    if (strlen($text) > $colWidth) 
    { 
     return substr($text, 0, $colWidth); 
    } 
    return $text . str_repeat(" ", $colWidth - strlen($text)); 
} 

然後,你想打印一列的任何時間,像這樣做:

output[] = padColumnText($no, 5) . padColumnText($product_name, 20) ... 

我假設你想要這種打印到固定寬度字體的某種純文本文件。如果你正在爲網絡做些什麼,這是錯誤的方法(你應該使用HTML格式)。

+0

你好,謝謝你的回答,這是工作,非常感謝 – gong