2011-02-05 3 views
1

아래에서이 함수를 사용하여 데이터베이스에서 검색 한 배열 데이터를 처리 한 다음이 데이터를 Excel로 내 보냅니다. Excel로 내보내기 전에 데이터를 테이블 형식으로 만들려면

# feed the final items to our formatting function... 
$contents = get_excel_data($items); 

function get_excel_data($items){ 

    # set the variable 
    $output = null; 

    # check if the data is an items and is not empty 
    if (is_array($items) && !empty($items)) 
    { 
     # start the row at 0 
     $row = 0; 

     # loop the items 
     # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice. 
     # foreach(array_values($items) as $item) 
     foreach($items as $item) 
     { 
      if (is_array($item) && !empty($item)) 
      { 
       if ($row == 0) 
       { 
        # write the column headers 
        $output = implode("\t",array_keys($item)); 
        $output .= "\n"; 
       } 
        # create a line of values for this row... 
        $output .= implode("\t",array_values($item)); 
        $output .= "\n"; 

        # increment the row so we don't create headers all over again 
        $row++; 
      } 
     } 
    } 

    # return the result 
    return $output; 
} 

그것은 다음과 같은 출력을 처리

,

cat_id cat_name cat_order cat_hide cat_important cat_created cat_updated 
1 Primary image 1 0 1 0000-00-00 00:00:00 2011-01-17 17:26:51 
2 Secondary image 2 0 1 0000-00-00 00:00:00 2011-01-17 17:27:01 
3 Tertiary image 3 0 1 0000-00-00 00:00:00 2010-10-08 20:03:56 
4 Quartary image 4 0 1 0000-00-00 00:00:00 2010-10-08 20:03:56 

하지만 이상적으로이 같은 테이블의 출력을 포장 할,

<table border="1"> 
<tr> 
<th>cat_id</th> 
<th>cat_name</th> 
<th>cat_order</th> 
<th>cat_hide</th> 
<th>cat_important</th> 
<th>cat_created</th>  
<th>cat_updated</th> 
</tr> 

<tr> 
<td>1</td> 
<td>Primary image</td> 
<td>1</td> 
<td>0</td> 
<td>1</td> 
<td>0000-00-00 00:00:00</td>  
<td>2011-01-17 17:26:51</td> 
</tr> 

<td>2</td> 
<td>Secondary image</td>  
<td>2</td> 
<td>0</td> 
<td>1</td> 
<td>0000-00-00 00:00:00</td>  
<td>2011-01-17 17:26:51</td> 
</tr> 
... 
</table> 

어떤 아이디어 나에 추가해야하는지 위의 테이블을 출력하는 원래 코드는 무엇입니까?

아래 수정 버전을 사용해 보았지만 실패했습니다!

function get_excel_data($items){ 

    # set the variable 
    $output = null; 

    # check if the data is an items and is not empty 
    if (is_array($items) && !empty($items)) 
    { 
     # start the row at 0 
     $row = 0; 

     # loop the items 
     # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice. 
     # foreach(array_values($items) as $item) 
     foreach($items as $key => $value) 
     { 

       if ($row == 0) 
       { 
        /* 
        # write the column headers 
        $output = implode("\t",array_keys($item)); 
        $output .= "\n"; 
        */ 

        $output = '<table border="1"><tr>'; 
        $output .= '<th>'.$key.'</th>'; 
        $output .= '</tr>'; 
       } 
        /* 
        # create a line of values for this row... 
        $output .= implode("\t",array_values($item)); 
        $output .= "\n"; 
        */ 

        $output .= '<tr>'; 
        $output .= '<td>'.$value.'</td>'; 

        $output .= '</tr>'; 


       if ($row == 0) 
       { 
        $output .= '</table>'; 
       } 

        # increment the row so we don't create headers all over again 
        $row++; 

     } 
    } 

    # return the result 
    return $output; 
} 

감사합니다.

+1

탭으로 구분 된 파일 또는 HTML 파일이 Excel 파일 인 것처럼 가장하기보다는 실제 Excel 파일을 만드는 것이 어떻습니까? –

+0

'진짜 Excel 파일을 만들어 보지 않으시겠습니까?'- Microsoft Excel을 사용하여 Excel 파일을 만드시겠습니까? – laukok

+0

@lauthiamkok : 아니요, 그는 프로그래밍 방식으로 실제 엑셀 파일을 만드는 것을 의미합니다 ... phpexcel - http://phpexcel.codeplex.com/을 확인하십시오. :-) – prodigitalson

답변

1

다음은 작동 가능성이있는 테스트되지 않은 변형입니다.

# feed the final items to our formatting function... 
$contents = get_excel_data($items); 

function get_excel_data($items){ 

    # set the variable 
    $output = null; 

    # check if the data is an items and is not empty 
    if (is_array($items) && !empty($items)) 
    { 
     # start the row at 0 
     $row = 0; 

     $output = '<table border="1">' 

     # loop the items 
     # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice. 
     # foreach(array_values($items) as $item) 
     foreach($items as $item) 
     { 
      if (is_array($item) && !empty($item)) 
      { 
       if ($row == 0) 
       { 
        $output .= '<tr>'; 
        foreach(array_keys($item) as $header) 
        { 
         $output .= '<th>'.$header.'</th>'; 
        } 
        $output .= '</tr>'; 
       } 

       $output .= '<tr>'; 
       foreach(array_values($item) as $cell) 
       { 
        $output .= '<td>'.$cell.'</td>'; 
       } 
       $output .= '</tr>'; 

       # increment the row so we only create headers once 
       $row++; 
      } 
     } 
     $output .= '</table>'; 
    } 

    # return the result 
    return $output; 
} 
+1

$ 출력. = ''이 (가) 잘못된 위치에 있습니까? foreach 루프 바깥쪽에있는 다음 중괄호 뒤에 오는 것이 맞습니까? –

+0

코드에 대한 감사 - 사소한 오류를 시도하고 수정했습니다. - 그러나 배열 키는 - cat_id, cat_name, cat_order 등과 같은 출력으로 만들어지지 않습니다. – laukok

+0

@Jared Farrish : 네 말이 맞아. 바로 브라우저에서 직접 코드를 작성하려고 할 때 발생합니다. 결정된. – btilly