【问题标题】:How to print multiple row with several columns in a template?如何在模板中打印多行多列?
【发布时间】:2017-10-24 11:01:01
【问题描述】:

我很难找到一种方法将 MySQL-Result 返回到一个数组中,该数组包含来自语句中每一行和每一列的数据。

controller.class.php:

class Controller {

  private $template;
  private $view;
  private $data;

  public function __construct() {
    $this->view = new View();
    $this->data = new Model();
  }

  public function display() {
      $this->view->setTemplate();
      $this->view->setContent("title", "Songs");
      $this->view->setContent("content", $this->data->getAllDataFromSongs());
      $this->view->setContent("footer", "&copy My name is Jeff\n");

      return $this->view->parseTemplate();
  }

}

model.class.php:

class Model {
  public $db_connection = null;

  public function __construct() {
    $this->openDatabaseConnection();
  }

  private function openDatabaseConnection() {     
    $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if ($this->db_connection->connect_error) {
      die('Connect Error (' . $this->db_connection->connect_errno . ') '
        . $this->db_connection->connect_error);
    }
  }

  public function getAllDataFromSongs() {
    $query = "SELECT * FROM songs";
    $row_content = array();

    if ($result = $this->db_connection->query($query)){

        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
          array_push($row_content, $row['id']);
          array_push($row_content, $row['artist']);
          array_push($row_content, $row['song']);
          array_push($row_content, $row['year']);
        }

        $result->free();

      return $row_content;
    }
    else
      echo 'No results';
  }

}

view.class.php:

class View {

  private $path         = 'templates';
  private $template;
  private $content  = array();

  public function setContent($key, $value){
    $this->content[$key] = $value;
  }

  public function setTemplate($template = 'default') {
    $this->template = $this->path . DIRECTORY_SEPARATOR . $template . '.tpl.php';
  }

  public function parseTemplate() {
    if (file_exists($this->template)) {

            ob_start();
            require_once('templates/header.tpl.php');
            include_once $this->template;
            require_once('templates/footer.tpl.php');
            $output = ob_get_contents();
            ob_end_clean();

      return $output;
    }
    else{
      return "Can't find ".$this->template." Template";
    }
  }
}

default.tpl.php:

<table>
  <tr>
    <th>ID</th>
    <th>artist</th>
    <th>song</th>
    <th>year</th>
  </tr>
  <tr>
    <?php 
    foreach ($this->content['content'] as $con){
        echo '<td>'.  $con . '</td>';
    }
    ?>
  </tr>
</table>

<br>
<?php echo $this->content['footer']; ?>

这不是完整的代码,但它应该向您展示我正在尝试的内容。 我现在遇到的问题是 getAllDataFromSongs() 的结果是一个数组,所有数据都在一个后面。我不能在一张桌子上把它们分开。

这是输出:

Array ( 
  [0] => 1 [1] => Artist 1 [2] => Song 1 [3] => Year 1
  [4] => 2 [5] => Artist 2 [6] => Song 2 [7] => Year 2
  [8] => 3 [9] => Artist 3 [10] => Song 3 [11] => Year 3 
  [12] => 4 [13] => Artist 4 [14] => Song 4 [15] => Year 4
  [16] => 5 [17] => Artist 5[18] => Song 5 [19] => Year 5 
)

ID artist song year 
1 Artist 1Song 1Year 12Artist 2Song 2Year 2 3Artist 3Song 3Year 3...

我希望你能把我要解释的内容联系起来..

【问题讨论】:

    标签: php arrays mysqli foreach


    【解决方案1】:
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
          array_push($row_content, $row['id']);
          array_push($row_content, $row['artist']);
          array_push($row_content, $row['song']);
          array_push($row_content, $row['year']);
        }
    

    你的问题是你为每一列创建一个新的数组元素,所以最后你会一个接一个地拥有它们,一个相同的级别。

    要么先创建一个临时数组,然后分配它(这样你就可以得到列的行),

        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
          $temp = [];
          array_push($temp, $row['id']);
          array_push($temp, $row['artist']);
          array_push($temp, $row['song']);
          array_push($temp, $row['year']);
    
          array_push($row_content, $temp);
        }
    

    或者,直接分配整行数组:

        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
          array_push($row_content, $row);
          // $row_content[] = $row; // same thing
        }
    

    如果您从 SELECT 语句中获得了不需要的其他列,那么您应该直接在 SELECT 语句中命名这些列,而不是选择 *。)

    此外,mysqli_result::fetch_all 也存在。该函数一次性将结果集中的 所有 行放入一个数组中 - 因此您可以完全取消 while 循环。

    【讨论】:

    • 我已经尝试了第一个解决方案,现在我得到了一个数组,其中包含每一行的数组...如何在我的 default.tpl.php 文件中获取表格?
    • @Alex 将嵌套的 foreach 添加到模板中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多