2017-12-13 12 views
0

내 스크립트에서 무엇이 잘못 되었습니까? 내보내기는 잘되지만 모든 결과 필드의 첫 번째 열을 반복하십시오. 파일을 다운로드하고 싶지 않고 그냥 저장하고 여기까지 괜찮습니다. Header sabe OK,하지만 30 개의 결과가 모두 동일하게 유지됩니다. 내가 뭘 잘못하고 있니?Excel로 내보내기 오류 -이 스크립트에서 동일한 열을 반복하십시오.

<?php 
/** 
* Created by PhpStorm. 
* User: leafar 
* Date: 11/12/17 
* Time: 09:48 PM 
*/ 

namespace App\Action; 


use Interop\Container\ContainerInterface; 
use Interop\Http\ServerMiddleware\DelegateInterface; 
//use Interop\Http\ServerMiddleware\MiddlewareInterface; 
use Psr\Http\Message\ResponseInterface; 
use Psr\Http\Message\ServerRequestInterface; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Db\Sql\Sql; 
use Zend\Db\Sql\Where; 
use Zend\Diactoros\Response\JsonResponse; 
use Zend\Stratigility\MiddlewareInterface; 

class ExportAction implements MiddlewareInterface{ 

private $dbAdapter; 

public function __construct(AdapterInterface $adapter) { 
    $this->dbAdapter = $adapter; 
} 


public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) 
{ 


    $sql  = new Sql($this->dbAdapter); 

    $select = $sql->select(['a'=>'wp_posts']); 
    $select->columns(['ID','post_content','post_title','post_name']); 
    $select->where(function(Where $where){ 
     $where->equalTo('post_type','product'); 
     $where->equalTo('post_status','publish'); 
    }); 

    $select->limit(30); 

    $prep = $sql->prepareStatementForSqlObject($select); 
    $exec = $prep->execute(); 

    $ii = 0; 

    foreach ($exec as $row): 


     $dataExport[$ii]['post_title'] = $row['post_title']; 
     $dataExport[$ii]['post_name'] = $row['post_name']; 
     $dataExport[$ii]['post_content'] = $row['post_content']; 

     $postid = $row['ID']; 

     $sql2 = new Sql($this->dbAdapter); 
     $select2 = $sql2->select(['b'=>'wp_postmeta']); 
     $select2->where(function(Where $where) use($postid){ 
      $where->equalTo('post_id',$postid); 
     }); 
     $prep2 = $sql2->prepareStatementForSqlObject($select2); 
     $exec2 = $prep2->execute(); 

     foreach ($exec2 as $row2): 
      if ($row2['meta_key'] == '_price'){ 
       $dataExport[$ii]['post_price'] = $row2['meta_value']; 
      } 
     endforeach; 

    $ii++; 
    endforeach; 


    $columns = [ 
     '"Título do produto"', 
     '"Nome do produto"', 
     '"Preço com promoção"', 
     '"Preço de venda em reais"', 
     '"Descrição simplificada"', 
     '"HTML da descrição completa"', 
     '"Endereço da imagem principal do produto"' 
    ]; 

    $headers = ''; 
    $line = ''; 
    $data = ''; 

    for($i=0;$i<=count($columns)-1;$i++){ 
     $headers .= $columns[$i] . "\t"; 
    } 

    @unlink(TMP_PATH.'test.xls'); 

    $fp = fopen(TMP_PATH.'test.xls',"a+"); 
    fwrite($fp,$headers); 
    fclose($fp); 

    for($a=0;$a<=count($dataExport);$a++){ 

     $fp = fopen(TMP_PATH.'test.xls',"a+"); 

     $line .= (empty($dataExport[$a]['post_title'])) ? "\t" : '"' . $dataExport[$a]['post_title'] . '"' . "\t"; 
     $line .= (empty($dataExport[$a]['post_name'])) ? "\t" : '"' . $dataExport[$a]['post_name'] . '"' . "\t"; 
     $line .= (empty($dataExport[$a]['post_price'])) ? "\t" : '"' . $dataExport[$a]['post_price'] . '"' . "\t"; 
     $line .= (empty($dataExport[$a]['post_price'])) ? "\t" : '"' . $dataExport[$a]['post_price'] . '"' . "\t"; 
     $line .= (empty($dataExport[$a]['post_name'])) ? "\t" : '"' . $dataExport[$a]['post_name'] . '"' . "\t"; 
     $line .= (empty($dataExport[$a]['post_content'])) ? "\t" : '"' . $dataExport[$a]['post_content'] . '"' . "\t"; 
     $line .= "\t"; 

     fwrite($fp, str_replace("\r","",trim($line)."\n")); 
     fclose($fp); 

    } 


    echo "OK!"; 

    return $response->withStatus(200); 


    } 
} 

나는 여러 가지 방법으로 파일 xls에 줄을 써 봅니다. 그러나 항상 같은 결과. $dataExport[$a]['post_title']을 인쇄하면 루프가 잘 진행되지만 $line 변수를 쓰면 첫 번째 결과가 반복됩니다.

+0

루프 시작 부분에'$ line'을 다시 설정해야합니다. $ line = ''; – Shibi

답변

0

방금 ​​가져 왔는데, probem 변수를 연결했다, 나는 그것을 연결없이 작성하고 잘 간다. 변수 $headers = $headers."\n"을 변경하십시오.

for($a=0;$a<=count($dataExport)-1;$a++){ 

     $fp = fopen(TMP_PATH.'test.xls',"a+"); 

     $line = 
      '"' . $dataExport[$a]['post_title'] . '"' . "\t" . 
      '"' . $dataExport[$a]['post_name'] . '"' . "\t" . 
      '"' . $dataExport[$a]['post_price'] . '"' . "\t" . 
      '"' . $dataExport[$a]['post_price'] . '"' . "\t" . 
      '"' . $dataExport[$a]['post_name'] . '"' . "\t" . 
      '"' . $dataExport[$a]['post_content'] . '"' . "\t" . 
      '""' . "\t"; 

     fwrite($fp, str_replace("\r","",trim($line)."\n")); 
     fclose($fp); 
     unset($line); 

    }