在我看來,我試圖只顯示頁面頂部的user_username,然後顯示所有消息和下面的消息時間。我對foreach($message as $row)
部件沒有問題,並且顯示正確。但是我無法獲得Messages by <?php echo $message['user_username']; ?>
以顯示消息海報的名稱。任何幫助我如何做到這一點將不勝感激。在視圖中顯示單行sql查詢結果
查看
<html>
<head>
<title> View Messages </title>
</head>
<body>
Messages by <?php echo $message['user_username']; ?>:
<?php foreach($message as $row){?>
<?php echo $row->text;?>
<br>
<?php echo $row->posted_at;?>
<br><br>
<?php }?>
</body>
</html>
控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
public function view($name){
//Error reporting if no name entered in url
if ($name == NULL) {
echo "no name entered in url";
return;
} else {
$this->load->model('messages_model');
$message['message'] = $this->messages_model->getMessagesByPoster($name);
$this->load->view('ViewMessages',$message);
}
}
}
型號
<?php
class Messages_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
//Return all messages posted by user with specified username
public function getMessagesByPoster($name) {
$sql = "SELECT user_username, text, posted_at FROM Messages WHERE user_username = ?;";
$query = $this->db->query($sql, $name);
return $query->result();
}
}
解釋這**我不能得到的海報的名稱顯示在該循環之外**並添加您的數據數組 –
'user_username'有一個值或多個值? –
將有多個值,但將所有的回答以下 – JL9