php pdo将mysql查询结果输出到多个表列中

时间:2013-07-30 18:12:42

标签: php pdo

我正在使用php PDO。我想将查询结果输出到3个不同的列。总行数为8。

应显示如下:

价值值
价值值
价值

我只获得前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($ subject)

array(8){[0] =&gt; object(Subject)#3(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“8”[“subject_name”] =&gt; string(7)“Theatre”[“count”] =&gt; string(0)“”}} [1] =&gt; object(Subject)#4(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“7”[“subject_name”] =&gt; string(7)“Science”[“count”] =&gt; string(0)“”}} [2] =&gt; object(Subject)#5(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“6”[“subject_name”] =&gt; string(13)“Language Arts”[“count”] =&gt; string(0)“”}} [3] =&gt; object(Subject)#6(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“5”[“subject_name”] =&gt; string(10)“Literature”[“count”] =&gt; string(0)“”}} [4] =&gt; object(Subject)#7(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“4”[“subject_name”] =&gt; string(4)“Math”[“count”] =&gt; string(0)“”}} [5] =&gt; object(Subject)#8(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“3”[“subject_name”] =&gt; string(14)“Social Studies”[“count”] =&gt; string(0)“”}} [6] =&gt; object(Subject)#9(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“2”[“subject_name”] =&gt; string(10)“Visual Art”[“count”] =&gt; string(0)“”}} [7] =&gt; object(Subject)#10(1){[“data”:protected] =&gt; array(3){[“subject_id”] =&gt; string(1)“1”[“subject_name”] =&gt; string(3)“Art”[“count”] =&gt; string(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() );
          }
       }

1 个答案:

答案 0 :(得分: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>';