2016-07-05 3 views
0

기본적으로 다운로드하기 전에 메모리 및 ZIP에 다른 CSV 파일 (테이블 당 하나의 CSV 파일)을 생성 할 프로젝트 용 다운로드 버튼을 만들고 싶습니다. 그것은 잘 작동하지만 문제는 내가 얼마나 많은 데이터베이스에 저장된에 따라 행을 반환하기로되어 각 mysql_fetch_array에 하나의 행 (마지막 결과)지고있다. 이 코드는 가치가 떨어지고 미안합니다. 사전에mysql_fetch_array는 쿼리의 마지막 값만을 반환합니다.

<?php 

require("../includes/connection.php"); 
require("../includes/myLib.php"); 
//get the ID that is passed 
$ID = $_REQUEST['q']; 
$ID = xdec($ID); 

//All queries to fetch data 
$query = mysql_query("SELECT * FROM `ge2`.`projects` WHERE `projects`.`proj_id`='$ID'"); 
$query2 = mysql_query("SELECT * FROM `ge2`.`attributes` WHERE `attributes`.`proj_id`='$ID'"); 
$query3 = mysql_query("SELECT * FROM `ge2`.`category` WHERE `category`.`proj_id`='$ID'"); 
$query4 = mysql_query("SELECT * FROM `ge2`.`multipletarget` WHERE `multipletarget`.`proj_id`='$ID'"); 
$query5 = mysql_query("SELECT * FROM `ge2`.`data_cut` WHERE `data_cut`.`proj_id`='$ID'"); 
$query6 = mysql_query("SELECT * FROM `ge2`.`raw` WHERE `raw`.`proj_id`='$ID'"); 

//getting all array 
    while($row = mysql_fetch_array($query)){ 

      $proj_alias = $row['proj_alias']; 
      $proj_id = $row['proj_id']; 
      $date_added = $row['date_added'];   
    } 

    while($row1 = mysql_fetch_array($query2)){ 

      $attrib_param_id = $row1['param_id']; 
      $attrib_proj_id = $row1['proj_id']; 
      $attrib_cat_id = $row1['cat_id']; 
      $attrib_val_id = $row1['val_id']; 
      $attrib_name = $row1['name']; 
      $attrib_isCust = $row1['isCust'];   
     } 

    while($row2 = mysql_fetch_array($query3)){ 

      $category_cat_id = $row2['cat_id']; 
      $category_name = $row2['name']; 
      $category_proj_id = $row2['proj_id']; 
      $category_desc = $row2['desc']; 
     } 
     while($row3 = mysql_fetch_array($query4)){ 

      $multipletarget_id = $row3['id']; 
      $multipletarget_proj_id = $row3['proj_id']; 
      $multipletarget_mtarget1 = $row3['mtarget1']; 
      $multipletarget_mtarget2 = $row3['mtarget2']; 
     } 
    while($row4 = mysql_fetch_array($query5)){ 

      $data_cut_id = $row4['id']; 
      $data_cut_proj_id = $row4['proj_id']; 
      $data_cut_name = $row4['name']; 
      $data_cut_param = $row4['param']; 
      $data_cut_lvl = $row4['lvl']; 
      $data_cut_val = $row4['val']; 
     } 

    while($row5 = mysql_fetch_array($query6)){ 

      $raw_id = $row5['raw_id']; 
      $raw_proj_id = $row5['proj_id']; 
      $raw_p_id = $row5['p_id']; 
      $raw_url = $row5['url']; 
      $raw_ip = $row5['ip']; 
      $raw_pos = $row5['pos']; 
      $raw_datetaken = $row5['datetaken']; 
      $raw_used = $row5['used']; 
      $raw_fdc_id = $row5['fdc_id']; 
      $raw_dq = $row5['dq']; 
     } 



     // some data to be used in the csv files 
     $records = array(
      $proj_alias, $proj_id, $date_added 
     ); 
     $records2 = array(
      $attrib_param_id, $attrib_proj_id, $attrib_cat_id, $attrib_val_id, $attrib_name, $attrib_isCust 
     ); 

     $records3 = array(
      $category_cat_id, $category_name, $category_proj_id, $category_desc 
     ); 
     $records4 = array(
      $multipletarget_id, $multipletarget_proj_id, $multipletarget_mtarget1, $multipletarget_mtarget2 
     ); 
     $records5 = array(
      $data_cut_id, $data_cut_proj_id, $data_cut_name, $data_cut_param,$data_cut_lvl,$data_cut_val 
     ); 
     $records6 = array(
      $raw_id, $raw_proj_id, $raw_p_id, $raw_url,$raw_ip,$raw_pos,$raw_datetaken,$raw_used,$raw_fdc_id,$raw_dq 
     ); 
//making an array to be used in loop  
     $set = array($records,$records2,$records3,$records4,$records5,$records6); 
//names to be named for each csv file  
     $names = array('projects', 'attributes', 'category', 'multipletarget', 'data_cut', 'raw'); 

     // create zip file 
     $zipname = $proj_alias; 
     $zip = new ZipArchive; 
     $zip->open($zipname, ZipArchive::CREATE); 

     // loop to create csv files 
     $n = 0; 
     foreach ($set as $setk=>$sets) { 
     $n+= 1; 
      $fd = fopen('php://temp/maxmemory:1048576', 'w'); 
      if (false === $fd) { 
       die('Failed to create temporary file'); 
      } 

       fputcsv($fd, $sets); 

      // return to the start of the stream 
      rewind($fd); 

      // add the in-memory file to the archive, giving a name 
      $zip->addFromString('BrainLink-'.$proj_alias."-".$names[$setk].'.csv', stream_get_contents($fd)); 
      //close the file 
      fclose($fd); 
     } 
     // close the archive 
     $zip->close(); 


     header('Content-Type: application/zip'); 
     header('Content-disposition: attachment; filename='.$zipname.'.zip'); 
     header('Content-Length: ' . filesize($zipname)); 
     readfile($zipname); 

     // remove the zip archive 
     // you could also use the temp file method above for this. 
     unlink($zipname); 
     ?> 

감사 :

여기 내 코드입니다.

답변

0

글쎄, 모든 쿼리 결과를 반복적으로 반복하고 변수를 계속해서 덮어 쓰는 것 같습니다. 따라서 결국에는 마지막 테이블 결과 만 사용할 수 있습니다.

$query = mysql_query('SELECT proj.*, attrs.* 
FROM `projects` AS proj 
JOIN `attributes` AS attrs ON (attrs.project_id=proj.project_id) 
<..more tables to join in the same manner..> 
WHERE proj.`proj_id`= ' . $projectId); 

를 그리고, 당신은 단지 하나의 검색어와 자원을 반복해야합니다 :

당신은 하나의 큰 쿼리 결과 행의 모든 ​​항목을 얻을 수있는 MySQL JOIN 또는 UNION SELECT를 사용하여 시도 할 수 있습니다.

while ($row = mysql_fetch_array($query)) { 
//your code here 
} 

참고 테이블이있는 경우 JOIN 'ED가 같은 열 이름을 가지고, 그들은 서로를 "덮어 쓰기"되며 "즉시"그들에게 자신의 이름을 변경해야합니다. 그래서 같이

:

SELECT proj.field, proj.another_field, proj.conflicting_field_name AS unique_field_name 
0

나는 당신의 모든 코드를 읽어 보지 않았지만, 각 루프 동안, 당신은 단지 마지막 레코드를 저장합니다. 그것은 이런 식이어야합니다.

while($row = mysql_fetch_array($query)){ 

      $proj_alias[] = $row['proj_alias']; 
      $proj_id[] = $row['proj_id']; 
      $date_added[] = $row['date_added'];   
    } 

및 상기와 같은 것들.

+0

나는 그것을 시험해 보았지만 CSV 출력 파일의 "배열"을 보여주었습니다. –

+0

@AlexCheddar는 물론 배열입니다. '$ proj_alias []','$ attrib_param_id []'...을 배열로 가지므로 나머지 코드는 변경해야합니다. 당신의 질문에'mysql_fetch_array는 쿼리의 마지막 값만을 반환합니다 .' 그리고 나는 당신에게 단서를주고 시도해 보았습니다. – Amir

관련 문제