2012-02-27 2 views
0

3 개 세포는 여기 보았다 : Array into a table with 5 cells in each rowPHP 테이블 : 디스플레이는 각 행

하지만 난

나는 테이블에 PHP 배열 결과를 표시해야 .. 내 코드를 적용 할 수 없다. 표에는 각 행마다 3 개의 셀이 있어야합니다.

나는 모든 3 행, 나는 테이블 3.

의 배수가 아닌 레코드 수를 포함하는 방법을 출력 마지막 경우 모르는 방법을 출력 모른다 여기 내 코드는 다음과 같습니다.

echo "<table>"; 


$num=mysql_numrows($result1); 
$i=0; 
while ($i < $num) 

{ 


$aaa=mysql_result ($result1,$i,"aaa"); 
$bbb=mysql_result ($result1,$i,"bbb"); 

echo " 

<td>$aaa $bbb</td> "; 

$i++; 

} 



echo "</table>"; 
+0

' ...'당 몇 개의 데이터베이스 행이 있습니까? – hakre

답변

4

세 번째 요소를 확인하려면 mod 연산자를 사용하십시오. 예 : 행을 제공 그것의 자신의 iterator로 포장하여 쉽게 가지 구성 가능 할 수

enter image description here

:

if($i%3 == 0) { 
    // this is the third element, start a new tr here 
} 
+1

결국'$ empty_cells = $ i % 3'과 같은 일을하고 while 루프를 사용하여 $ empty_cells 더 많은 tds를 그립니다. 리터럴 "3"을 변수로 바꾸면 행마다 다른 수의 열이 필요한 다른 프로젝트에이 코드를 복사하여 붙여 넣기가 쉽습니다. – TecBrat

0

echo "<table><tr>"; 
$num=mysql_numrows($result1); 
$i=0; 

while ($i < $num) 
{ 
    $aaa=mysql_result ($result1,$i,"aaa"); 
    $bbb=mysql_result ($result1,$i,"bbb"); 
    if($i %3 ==0) 
    { 
    echo"</tr><tr><td> $aaa $bbb </td>"; 
    } 
    else 
    { 
    echo"<td> $aaa $bbb </td>"; 
    } 
    $i++; 
} 
echo "</tr></table>"; 
1

이런 식으로 뭔가를 달성하기 위해 시도 열 :

/** 
* function to fetch one row from database 
*/ 
function fetch_row($resultSet = NULL) { 
    static $result; 
    static $i = 0; 
    if ($resultSet) { 
     $result = $resultSet; 
     return; 
    } 
    return mysql_fetch_assoc($result); 
} 

fetch_row($result1); // initialize fetch function 

$it = new FetchIterator('fetch_row'); 

$table = new TableIterator($it, 5); 

if ($table->valid()) : ?> 

<table border="1"> 

    <?php foreach ($table as $row => $columns) : ?> 

     <tr class="<?php echo $row % 2 ? 'odd' : 'even'; ?>"> 

      <?php foreach ($columns as $index => $column) : ?> 

      <td> 
       <?php if ($column) echo $column['aaa'], ' ', $column['bbb']; ?> 
      </td> 

      <?php endforeach; ?> 

     </tr> 

    <?php endforeach; ?> 

</table> 

<?php endif; 

일부 DemoSome PHP Iterator Fun도 참조하십시오.