2011-01-14 9 views
1

私は現在プライベートメッセージングシステムで作業しています。今は受信トレイをコーディングしていて、受信トレイが正常に動作することをテストするためにメッセージテーブルに2行の一時データを入れました。データベース内の特定のテーブルから複数の行を返します

私は、このクエリが真であるすべての行を返すようにしようとしています:

<?php 
class Messages_model extends CI_Model { 


    public function inbox() 
    { 
     $user_id = $this->session->userdata('user_id'); 
     $username = $this->session->userdata('username'); 

     return $this->db->query("SELECT * FROM messages WHERE to_id = $user_id AND to_user = '$username'"); //to_id = id of currently logged in and to_user = username 
    } 
} 

は、ここに私のコントローラです。私は現在、両方の行を返す必要があるにもかかわらず、1つの行だけを返しています。私はto_idとto_userをセッションデータと等しいdbに両方の行を作成しました。だから、彼らはどちらも返されるべきではありません。 row_arrayを使用すると、CIのユーザーガイドで説明したように最初の行が返されます。 result_array()を使用すると、まだ1行だけが返されます。

私がしたいことは、いくつかの列を持つhtmlで表を作成し、各列のタイトルのために1行を作成することです。件名、ステータス、日付。次に、その下の行が実際の件名データとメッセージのステータスになります。次に、受信トレイのメッセージテーブルから行が返されるたびに空の行をエコーするwhileループまたはforeachループを実行します。私は私がやろうとしているかを説明して明らかだった願っています

<?php 
class Messages extends Public_Controller { 
    public function __construct() { 
     parent::__construct(); 

     $this->load->model('messages_model'); 

    } 

    public function inbox() 
    { 


     $query = $this->messages_model->inbox(); // return from model which is basically a row from messages table 
     if ($query->num_rows() != 0) // if rows returned is equal to 1 

     {  
     foreach ($query->result_array() as $row) // give array variable the value row_array 

     // grab specific elements from row and assign to variables 
     $row['id']; 
     $row['to_id']; 
     $row['to_user']; 
     $row['from_id']; 
     $row['from_user']; 
     $row['time_sent']; 
     $row['subject']; 
     $row['message']; 
     $row['opened']; 
     $row['replied']; 

     $this->load->view('messages/inbox', $row); 
     } 

     else 
     { 
      echo "You have 0 messages in your inbox"; 
     } 
    } 
} 

は、ここに私のコントローラです。 ありがとうございます。

答えて

1

まず、私はCodeIgniterをあまりよく知らないことに注意してください。

foreach ($query->result_array() as $row) // give array variable the value row_array 

    // grab specific elements from row and assign to variables 
    $row['id']; 
    $row['to_id']; 
    $row['to_user']; 
    $row['from_id']; 
    $row['from_user']; 
    $row['time_sent']; 
    $row['subject']; 
    $row['message']; 
    $row['opened']; 
    $row['replied']; 

    $this->load->view('messages/inbox', $row); 
} 

あなたが行に遭遇するたびに、あなたは$this->load->view()を呼んでいる:あなたの問題は、しかし、かなり一般的なPHPです。私はあなたが渡したいすべてのデータを使って、ビューを一度呼び出すだけでよいと信じています。

$data = array(); 
foreach ($query->result_array() as $row) { 
    $data[] = array (
     'id' => $row['id'], 
     'to_id' => $row['to_id'], 
     'to_user' => $row['to_user'], 
     'from_id' => $row['from_id'], 
     'from_user' => $row['from_user'], 
     'time_sent' => $row['time_sent'], 
     'subject' => $row['subject'], 
     'message' => $row['message'], 
     'opened' => $row['opened'], 
     'replied' => $row['replied'] 
    ); 
} 
$this->load->view('messages/inbox', $data); 

次に、複数のメッセージを処理する必要があります。

+0

私は多分このように未定義の変数を得ているので実際のビューでforeachループを行うべきだと思っていますが、私は私のビューに多くのPHPを入れたくありませんでしたが、これは大丈夫だと思います受信トレイのテーブル。 – LondonGuy

+0

@Psychoneticsあなたの視点のループは、必要であれば完璧なセマンティックセンスを作ります。 – lonesomeday

+0

これにはまだ問題があります。モデルのInboxメソッドの出力をビューに渡すことを考えていましたが、それは悪い習慣です。私が達成しようとしているものを達成する最良の方法は何でしょうか? – LondonGuy

2

あなたのforループでかっこが欠落しているように見える:あなたは中括弧で体を包囲しない限り、forループ

foreach ($query->result_array() as $row) // give array variable the value row_array 
{ 
    // grab specific elements from row and assign to variables 
    $row['id']; 
    $row['to_id']; 
    $row['to_user']; 
    $row['from_id']; 
    $row['from_user']; 
    $row['time_sent']; 
    $row['subject']; 
    $row['message']; 
    $row['opened']; 
    $row['replied']; 
} 

Aは、最初の行のみに適用されます。

関連する問題