我怎么能返回数组并在数据库的另一个类中使用它

我怎么能返回数组并在数据库的另一个类中使用它

问题描述:

I have a problem. I'd like to get a all row from and I have to write it to display. I write a code but it does not comply with the rules of MVC. I never did similar thing so I'd like to get all row from database and this method call in another class where I can use like :

<td><?=$data['colummn_name']?></td>

This is my wrong code:

public function getFiles(){


    $id = $_SESSION['user_logged_status']; 
    $sql = "SELECT * FROM file WHERE user_id=".$id;
    $res = $this->conn->query($sql);
    if($res->fetchColumn() > 0){
       echo "<center><table border='1' width='100%' height='auto'>";
       echo "<tr><td><b>Number</b><td><b>Name</b></td><td><b>Size</b></td><td><b>Date</b></td><td><b>Delete</b></td></tr>";
       foreach ($this->conn->query($sql) as $row) {
         $x++; 
         $class = ($x%2 == 0)? 'whiteBackground': 'graybackground';
         echo "<tr class='$class'><td>".$x."</td><td>".$row['name']."</td><td>".$row['size']."</td><td>".$row['file_date']."</td><td><form method='post' action=''><input type='hidden' name='id_to_be_deleted' value=".$row['id']."/><input type='submit' name='delete_file' value='Delete'/></form></td>";           
     }
     echo "</table></center>";
   }
}

I hope that it was clear and somebody can help me.

Try this and move the "html part" to template like
$data = $obj->getFiles();
foreach($data as $k=>$v){//etc.}

public function getFiles(){

    $return = array();
    $id = $_SESSION['user_logged_status']; 
    $sql = "SELECT * FROM file WHERE user_id=".$id;
    $res = $this->conn->query($sql);
      if($res->fetchColumn() > 0){   
         foreach ($this->conn->query($sql) as $row) {
             $return[] = $row;
         }   
    }
    return $return;
}