[英]CodeIgniter/PHP Error Message
該應用程序的目標是能夠單擊一個視圖中的鏈接以從另一個視圖獲取數據。 第一個視圖工作正常,我也獲得了正確的PK。 當我單擊鏈接時,我遇到了問題。
從此視圖獲取“消息:為foreach()提供了無效的參數”:
<?php
echo "<h2>View</h2>";
foreach ($rows as $r){
echo '<br /><h3>'; echo $r->tHandle; echo'</h3>';
echo "<li>Sent at: "; echo $r->content; echo"</li><br />";
echo'<li>Created: '; echo $r->created; echo'</li><br />';
}
?>
這是進行數據庫查詢的模型和功能:
<?php
class Tweets_model extends CI_Model{
public function __construct() {
$this->load->database();
}
public function getTweetDetails($id){
$this->db->select('*')->from('tweets')->where('tweetId', $id);
$q = $this->db->get();
if($q->num_rows > 0){
foreach($q->result() as $row){
$data[]=$row;
}
return $data;
}
}
}
?>
控制器:
<?php
class Tweets extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->model('tweets_model');
}
public function details(){
$data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(2));
$this->load->view('header');
$this->load->view('tweet_details', $data);
$this->load->view('footer');
}
}
?
>
任何人都可以幫助我克服錯誤消息嗎?
謝謝。
嘗試這個:
視圖:
<?php
echo "<h2>View</h2>";
foreach ($rows as $r){
echo '<br /><h3>'; echo $r['tHandle']; echo'</h3>'; // EDIT THIS LINE
echo "<li>Sent at: "; echo $r['content']; echo"</li><br />";
echo'<li>Created: '; echo $r['created']; echo'</li><br />';
}
?>
模型:
<?php
class Tweets_model extends CI_Model{
public function __construct() {
$this->load->database();
}
public function getTweetDetails($id){
$this->db->select('*')
$this->db->from('tweets')
$this->db->where('tweetId', $id);
return $this->db->get()->result_array();
}
}
?>
控制器:
<?php
class Tweets extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->model('tweets_model');
}
public function details(){
$data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(2));
$this->load->view('header');
$this->load->view('tweet_details', $data);
$this->load->view('footer');
}
}
?>
更新:
段從左到右編號。 例如,如果您的完整網址是:
段號是這樣的:
$this->uri->segment(1) // with return -> news
$this->uri->segment(2) // with return -> local
$this->uri->segment(3) // with return -> metro
$this->uri->segment(4) // with return -> crime_is_up
在您的情況下, $this->uri->segment(2)
將返回details
。 並且查詢將返回0行。 僅出於測試目的,您可以這樣做:
public function details(){
$tweet_id = 1 // for example
$data['rows'] = $this->tweets_model->getTweetDetails($tweet_id);
$this->load->view('header');
$this->load->view('tweet_details', $data);
$this->load->view('footer');
}
或者您可以從url中獲取它,例如:
public function details($id){ // Here you will have the $tweet_id
$data['rows'] = $this->tweets_model->getTweetDetails($this->uri->segment(3));
$this->load->view('header');
$this->load->view('tweet_details', $data);
$this->load->view('footer');
}
您需要在此處進行一些故障排除。 您所有的錯誤是因為數據庫未返回任何數據。
確保tweetId實際上在段2中,這些段開始在base_url之后開始計數,因此,如果您的基本URL是example.com,則段2將類似於example.com/tweet/2,其中2是正確的段。
在此行中將您知道存在的tweetId傳遞給模型:
$ data ['rows'] = $ this-> tweets_model-> getTweetDetails($ this-> uri-> segment(2));
所以像這樣:
$data['rows'] = $this->tweets_model->getTweetDetails(1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.