2016-11-30 89 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