简体   繁体   English

PHP PDO输出MySQL查询结果到多个表列

[英]php pdo output mysql query results into multiple table columns

I am using php PDO. 我正在使用php PDO。 I would like to output the results of the query into 3 different columns. 我想将查询结果输出到3个不同的列中。 The total row count is 8. 总行数为8。

It should display as follows: 它应显示如下:

value value value 值值值
value value value 值值值
value value 价值

I am only getting the first 3 values. 我只得到前三个值。

Here is my code: 这是我的代码:

  <?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>';
  ?>

Here is a var_dump($subjects) 这是一个var_dump($ subjects)

array(8) { [0]=> object(Subject)#3 (1) { ["data":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) "7" ["subject_name"]=> string(7) "Science" ["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) "5" ["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]=> o array(8){[0] => object(Subject)#3(1){[“” data“:protected] => array(3){[” subject_id“] => string(1)” 8“ [” subject_name“] =>字符串(7)”剧院“ [” count“] =>字符串(0)”“}} [1] => object(Subject)#4(1){[”“ data”:protected] = > array(3){[“” subject_id“] => string(1)” 7“ [” subject_name“] => string(7)” Science“ [” count“] => string(0)”“}} [ 2] => object(Subject)#5(1){[“” data“:protected] => array(3){[”“ subject_id”] =>字符串(1)“ 6” [“ subject_name”] =>字符串(13)“语言艺术” [“ count”] =>字符串(0)“”}} [3] =>对象(对象)#6(1){[“ data”:protected] => array(3) {[“” subject_id“] =>字符串(1)” 5“ [” subject_name“] =>字符串(10)” Literature“ [” count“] =>字符串(0)”“}} [4] =>对象(主题)#7(1){[“” data“:protected] => array(3){[”“ subject_id”] => string(1)“ 4” [“ subject_name”] => string(4)“ Math “ [” count“] =>字符串(0)”“}} [5] => object(Subject)#8(1){[”“ data”:protected] => array(3){[“” subject_id“] =>字符串(1)“ 3” [“ subject_name”] =>字符串(14)“社会研究” [“ count”] =>字符串(0)“”}} [6] => o bject(Subject)#9 (1) { ["data":protected]=> array(3) { ["subject_id"]=> string(1) "2" ["subject_name"]=> string(10) "Visual Art" ["count"]=> string(0) "" } } [7]=> object(Subject)#10 (1) { ["data":protected]=> array(3) { ["subject_id"]=> string(1) "1" ["subject_name"]=> string(3) "Art" ["count"]=> string(0) "" } } } bject(Subject)#9(1){[“” data“:protected] => array(3){[”“ subject_id”] => string(1)“ 2” [“ subject_name”] => string(10)“视觉艺术“ [” count“] =>字符串(0)”“}} [7] => object(Subject)#10(1){[” data“:protected] => array(3){[”“ subject_id “] => string(1)” 1“ [” subject_name“] => string(3)” Art“ [” count“] => string(0)”“}}}

Here is my Subject class: 这是我的学科课:

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() );
          }
       }

Try this: 尝试这个:

$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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM