2

私は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($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) "語学" ["count"]=> string(0) "" } } [3]=> object(Subject)#6 (1) { ["data":protected]=> array(3) { ["subject_id"]=> string(1) "5" ["subject_name"]=> string(10) "文学" ["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) "社会科" ["count"]=> string(0) "" } } [6]=> object(Subject)#9 (1) { [ "data":protected]=> array(3) { ["subject_id"]=> string(1) "2" ["subject_name"]=> string(10) "ビジュアル アート" ["count"]=> string(0) "" } } [7]=> object(Subject)#10 (1) { ["data":protected]=> array(3) { ["subject_id"]=> 文字列(1) "1" ["subject_name"]=> string(3) "Art" ["count"]=> 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() );
          }
       }
4

1 に答える 1

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>';
于 2013-07-30T18:15:45.673 に答える