1

而不是我的消息出现在屏幕上,我只看到这个:

遇到 PHP 错误

严重性:警告
消息:为 foreach() 提供的参数无效

尽管我的视图页面中的 foreach 循环看起来不错:

foreach($messages as $message):// this is the line causing the error
      $delete = $message['message_id']; // i've even tried removing this line to no avail
      //var_dump($message); ?>
          <li><?=$message['from']?> says...: "<?=$message['message']?>"(<?=anchor("home/deleteMsg/$delete", 'delete')?>)</li>      
         <?php endforeach?> 

这是我的模型:

class Messages extends CI_Model
 {



function Messages()
{
parent::__construct();
}


function deleteMsg($message_id) // this message_id is the id field for each message in my database
       {


              $record = array('message_id' => $message_id); 
              $this->db->delete('messages', $record); 
      } 



  function addMessage($message_id, $from, $to, $message)//this fields come after the message_id field in the database
   {
$record = array(

        'to' => $to,
        'from' => $from,
        'message' => $message);
$this->db->insert('messages', $record); 

   }

   function getMessages($user)
   {

$this->db->select('*');
$this->db->from('messages');
$this->db->where('to', $user);
$messagesSet = $this->db->get();

$messages = array ();

foreach ($messagesSet->result() as $row)
{
    $messages[] = array(

             'from'    => $row ->from,
             'message' => $row ->message);

}       
    return $messages;

  }

 }

这是我的控制器:

 class Home extends CI_Controller 
 {
  function Home()
 {
  parent::__construct();
  $this->load->model('messages');
  $this->load->model('friends');
  $this->load->model("profiles");


  }

  function drop($member)
  {
   $username = $this->session->userdata('username');
   $this->friends->deleteFriend($username, $member);
   redirect('home');
  }



     function deleteMsg($messageid) {

    $this->messages->deleteMsg($messageid);

    redirect('home');

      }


function index()
{

 $username = $this->session->userdata('username');
 $membername = $this->session->userdata('membername');
 $viewData['membername'] = $membername;
 $viewData['username'] = $username;
 $viewData['following'] = $this->friends->getFollowing($username);
 $viewData['followers'] = $this->friends->getFollowers($username);
 $viewData['messages'] = $this->messages->getMessages($membername);
 $viewData['friends'] = $this->friends->getFriends($username);
 $viewData['messages'] = $this->messages->deleteMsg($membername);

 $this->load->view('shared/header');
 $this->load->view('home/hometitle', $viewData);
 $this->load->view('shared/nav');
 $this->load->view('home/homeview', $viewData);
 $this->load->view('shared/footer');
}


 }  
4

2 回答 2

1

你的messages可能是空的

<?php
if(!empty($messages)){
foreach($messages as $message):// this is the line causing the error
      $delete = $message['message_id']; // i've even tried removing this line to no avail
      //var_dump($message); ?>
          <li><?=$message['from']?> says...: "<?=$message['message']?>"(<?=anchor("home/deleteMsg/$delete", 'delete')?>)</li>      
         <?php endforeach?> 
<?php }else{ ?>

<?php echo 'Its empty !!!'; }?>
于 2012-12-04T03:49:57.223 回答
1

$messages是空的,因为它被这个覆盖了:

$viewData['messages'] = $this->messages->deleteMsg($membername);

在索引函数中。

于 2012-12-04T09:53:39.787 回答