2017-10-24 5 views
-1

私は、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()の結果がすべてのDatasが互いに背後にある配列だということです。私はテーブルでそれらを分離することはできません。

これが出力されます:私はあなたが私が説明しようとしているものを関連付けることができると思い

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... 

..

答えて

0
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']); 
    } 

ここにあなたの問題は、あなたがのために新しい配列要素を作成しているということですそれぞれの列は、最終的には、あなたはそれらを1つずつ、同じレベルのものにします。

のどちらか、

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ループと離れて行うことができます。

+0

私は最初のソリューションをしようと試みていると私は私が私のdefault.tpl.phpファイル内のテーブルを取得することができますどのように...今内部の行ごとに配列の配列を取得していますか? – Alex

+1

@Alexネストしたforeachをテンプレートに追加しますか? –

関連する問題