2013-07-30 96 views
2

。我想將查詢結果輸出到3個不同的列中。總行數是8。PHP PDO輸出MySQL查詢結果到我使用PHP PDO多個表列

它應該顯示如下:

值值值
值值值
值值

我只得到所述第一3個值。

這是我的代碼:

<?php 
    $subjects = Subject::getAllSubjects(); 

    $rows = 8; 
    $rcounter = 1; 
    $cols = 3; 
    echo '<table>'; 

for($i = 0; $i < $rows/$cols; $i++) { 
    foreach($subjects as $subject){ 
     echo '<tr>'; 

     for($j=0; $j < $cols && $rcounter <= $rows ;$j++, $rcounter++) { 
      echo "<td>".$subject->getValueEncoded('subject_name')."</td>"; 
     } 
     echo '</tr>'; 
    } 
} 
    echo '</table>'; 
    ?> 

這裏是一個的var_dump($受試者)

陣列(8){[0] =>對象(受試者)#3(1){[」數據「:protected] => array(3){[」subject_id「] => string(1)」8「[」subject_name「] => string(7)」Theatre「[」count「] => string(0 )「>」}} [1] => object(Subject)#4(1){[「data」:protected] => array(3){[「subject_id」] => string(1)科學「[」count「] => string(0)」「}} [2] => object(Subject)#5(1){[」data「:protected] = > array(3){[「subject_id」] => string(1)「6」[「subject_name」] => string(13)「Language Arts」[「count」] => string(0 )「>}} [3] => object(Subject)#6(1){[」data「:protected] => array(3){[」subject_id「] => string(1) subject_name「] => string(10)」Literature「[」count「] => string(0)」「}} [4] => object(Subject)#7(1){[」data「:protected] = > array(3){[「subject_id」] => string(1)「4」[「subject_name」] => string(4)「Math」[「count」] => string(0)「」}} 5] => object(Subject)#8(1){[「data」:protected] => array(3){[「subject_id」] => string(1)「3」[「subject_name」] => string (14)「Social Studies」[「count」] => string(0)「」}} [6] => object(Subject)#9(1){[「data」:protected] => array(3) {[ 「subject_id」] =>串(1) 「2」[ 「SUBJECT_NAME」] =>串(10) 「視覺藝術」[ 「計數」] =>串(0) 「」}} [7] => object(Subject)#10(1){[「data」:protected] => array(3){[「subject_id」] => string(1)「1」[「subject_name」] => string(3)「技術」 [ 「計數」] =>串(0) 「」}}}

這是我的主題類:

require_once("DataObject.class.php"); 

class Subject extends DataObject { 

    protected $data = array(
     "subject_id" => "", 
     "subject_name" => "", 
     "count" => "" 
    ); 

     public function getCount() { 
     $conn = parent::connect(); 
     $sql = "SELECT subject_name, count(*) as count FROM " . TBL_SUBJECT; 

     try { 
      $st = $conn->prepare($sql); 
      $st->execute(); 
      $subjects = array(); 
      foreach ($st->fetchAll() as $row) { 
       $subjects[] = new subject($row); 
      } 
      parent::disconnect($conn); 
      return $subjects; 
      } catch (PDOException $e) { 
      parent::disconnect($conn); 
      die("Query failed: " . $e->getMessage()); 
      } 
     } 


     public function getAllSubjects() { 
     $conn = parent::connect(); 
     $sql = "SELECT * FROM " . TBL_SUBJECT . " ORDER BY subject_id DESC"; 

     try { 
      $st = $conn->prepare($sql); 
      $st->execute(); 
      $subjects = array(); 
      foreach ($st->fetchAll() as $row) { 
       $subjects[] = new subject($row); 
      } 
      parent::disconnect($conn); 
      return $subjects; 
      } catch (PDOException $e) { 
      parent::disconnect($conn); 
      die("Query failed: " . $e->getMessage()); 
      } 
     } 
+0

它看起來像行計數器停留在1 - 嘗試添加'$ rcounter ++'你'foreach'塊的底部。 – ygesher

+0

的$ rcounter沒有工作 – user2091928

回答

0

試試這個:

$subjects = Subject::getAllSubjects(); 

$rows = 8; 
$rcounter = 1; 
$cols = 3; 

echo '<table>'; 

foreach($subjects as $subject){ 
    echo '<tr>'; 
    for($i = 0; $i < $rows ; $i++) { 
     // echo "<td>".$subject->getValueEncoded('subject_name')."</td>"; 
     echo "<td>".$subject[$i]->getValueEncoded('subject_name')."</td>"; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
+0

我得到相同的結果 – user2091928

+0

請問這個:$主題=主題:: getAllSubjects();回報? – Maximus2012

+0

查看已更新的答案。這是否工作? – Maximus2012