Welcome Guest, Not a member yet? Register   Sign In
Processing a Database Query
#1

[eluser]CodyyC[/eluser]
Hey, I know that there is something wrong with this model.

I was wondering if someone could please tell me whats wrong with it and why etc, so I can learn from what I did wrong.

Thanks in Advanced,
Cody

Here's the model:
Code:
<?php
class Message_read_m extends Model {
    
    var $username = '';
    var $last_message_id = '';
    
    function __construct()
    {
        parent::__construct();
    }
    
    function message_read(){
        
        $this->load->library('session');
        $this->username    = ($this->session->userdata('username')) ? $this->session->userdata('username') : 'Anonymous';
        $this->last_message_id = ($this->session->userdata('last_message_id')) ? $this->session->userdata('last_message_id') : '0';
        $get_messages = $this->db->get('chat_logs', $this->last_message_id);
        $last_message_row = $get_messages->last_row('array');
        $last_message_id = $last_message_row['message_id'];
        $this->session->set_userdata('last_message_id', $this->last_message_id);
        
        return $get_messages->result();
    }
}

?>
#2

[eluser]kaejiavo[/eluser]
Hi Cody,

What is your expected result from this model?
What makes you think there is something wrong?
Do you get any error messages?
How do you call the model in your controller?
...

There is possibly more than one thing wrong with your model. But how could anybody help you without having at least a little bit of information about the context of this code snippet?

Marco
#3

[eluser]CodyyC[/eluser]
Hey,

Sorry for not posting more details here goes.

Basically. A POST request (via AJAX) is sent to the controller, which calls the Model which sends the data back to the controller which then passes it to the message_view.php VIEW (to format the data) which then sends it back to the controller then to the main page for viewing.

I want the model to return an array, which can be echoed in a loop in the formatting View.

I can tell that something is wrong because the code is not doing what it's supposed to Tongue

The error in the JS console is:
Quote::8888/chatulo.us/[object Object]Failed to load resource: the server responded with a status of 404 (Not Found)

Here is the model again:

message_read_m.php
Code:
<?php
class Message_read_m extends Model {
    
    var $username = '';
    var $last_message_id = '';
    
    function __construct()
    {
        parent::__construct();
    }
    
    function message_read(){
        
        $this->load->library('session');
        $this->username    = ($this->session->userdata('username')) ? $this->session->userdata('username') : 'Anonymous';
        $this->last_message_id = ($this->session->userdata('last_message_id')) ? $this->session->userdata('last_message_id') : '0';
        $get_messages = $this->db->get('chat_logs', $this->last_message_id);
        $last_message_row = $get_messages->last_row('array');
        $last_message_id = $last_message_row['message_id'];
        $this->session->set_userdata('last_message_id', $this->last_message_id);
        
        return $get_messages->result();
    }
}

?>

The controller:
message.php
Code:
<?php

class Message extends Controller {
    
    function load_message_store_model(){
        $this->load->model('Message_store_m', '', TRUE);
        $this->Message_store_m->message_store();
    }
    
    function load_message_read_model(){
        $this->load->model('Message_read_m', '', TRUE);
        $formatted_data = $this->Message_read_m->message_read();
        $formatted_data = $this->load->view('home', $formatted_data, TRUE);
        return $formatted_data;
    }

}

?>

The formatting view:
Code:
<?php
foreach ($formattedData as $returned_messages)
{
?>
<p class="message">
    <span class="timestamp">&lt;?php echo $returned_messages['timestamp'];?&gt;</span>
    <span class="username">&lt;?php echo $returned_messages['username'];?&gt;</span>
    &lt;?php echo $returned_messages['message'];?&gt;
</p>
&lt;?php
}
?&gt;

Thank you for your help.
Cody
#4

[eluser]Dennis Rasmussen[/eluser]
Your JavaScript is trying to open a page that doesn't exist.
Could you give us the URL it's trying to open or maybe even the AJAX code?
#5

[eluser]CodyyC[/eluser]
Hey,

Here is the main page:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    &lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Chatulo.us /Home&lt;/title&gt;
        &lt;meta http=equiv"Content-Type" content="text/html; charset=UTF-8"&gt;
        &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;application/assets/css/styles.css" /&gt;
        [removed][removed]
        [removed]
        $(document).ready(function() {
             $("#sendMessageForm").submit(function(postMessage) {
                 postMessage.preventDefault();
                $.post("&lt;?php echo base_url(); ?&gt;index.php/message/load_message_store_model", { message: $("#messageInputField").val()}, function (data) {
                    alert(data);
                });
             });
            
            function update() {
                $.post({
                    type: "POST",
                    dataType: "json",
                    url: "&lt;?php echo base_url(); ?&gt;index.php/message/load_message_read_model",
                    success: function (message) {
                        $('#messageBox').html(message.message).attr('scrollTop', function(){ return $(this).attr('scrollHeight') })
                      },
                    complete: function () {
                        setTimeout(update, 1000)
                    }
                });
            }
        update();
         });
        [removed]
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form method="post" name="messageInput" id="sendMessageForm"&gt;
            &lt;input name="message" id="messageInputField" type="text" autocomplete="off"/&gt;
            &lt;input name="submit" type="submit" value="Send"/&gt;
        &lt;/form&gt;
        <div id="messageBox">
        </div>
        <div id="usernameBox">
    </div>
    <span id="dummy"></span>
    <img src="&lt;?php echo base_url(); ?&gt;application/assets/images/logo.png" width="175" height="50" alt="Logo" id="chatulouslogo"/>
    &lt;/body&gt;
&lt;/html&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    &lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Chatulo.us /Home&lt;/title&gt;
        &lt;meta http=equiv"Content-Type" content="text/html; charset=UTF-8"&gt;
        &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;application/assets/css/styles.css" /&gt;
        [removed][removed]
        [removed]
        $(document).ready(function() {
             $("#sendMessageForm").submit(function(postMessage) {
                 postMessage.preventDefault();
                $.post("&lt;?php echo base_url(); ?&gt;index.php/message/load_message_store_model", { message: $("#messageInputField").val()}, function (data) {
                    alert(data);
                });
             });
            
            function update() {
                $.post({
                    type: "POST",
                    dataType: "json",
                    url: "&lt;?php echo base_url(); ?&gt;index.php/message/load_message_read_model",
                    success: function (message) {
                        $('#messageBox').html(message.message).attr('scrollTop', function(){ return $(this).attr('scrollHeight') })
                      },
                    complete: function () {
                        setTimeout(update, 1000)
                    }
                });
            }
        update();
         });
        [removed]
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form method="post" name="messageInput" id="sendMessageForm"&gt;
            &lt;input name="message" id="messageInputField" type="text" autocomplete="off"/&gt;
            &lt;input name="submit" type="submit" value="Send"/&gt;
        &lt;/form&gt;
        <div id="messageBox">
        </div>
        <div id="usernameBox">
    </div>
    <span id="dummy"></span>
    <img src="&lt;?php echo base_url(); ?&gt;application/assets/images/logo.png" width="175" height="50" alt="Logo" id="chatulouslogo"/>
    &lt;/body&gt;
&lt;/html&gt;
#6

[eluser]kaejiavo[/eluser]
In your model:
Code:
$this->last_message_id = ($this->session->userdata('last_message_id')) ? $this->session->userdata('last_message_id') : '0';
        $get_messages = $this->db->get('chat_logs', $this->last_message_id);
...
return $get_messages->result();
0. db->get() -> see userguide
The second and third parameters enable you to set a limit and offset clause...
So what you do in this code is limit the number of records returned to the value of $this->last_message_id. Probably not your intention?
When this is corrected, you may move on Smile

1. If last_message_id = 0, your $get_messages is empty (or you have a record with id=0 in your db?).

2. result() (see userguide)
This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop, like this:
Code:
foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}
remember this for later.

In your controller:
Code:
function load_message_read_model(){
        $this->load->model('Message_read_m', '', TRUE);
        $formatted_data = $this->Message_read_m->message_read();
        //for debugging insert:
        echo "<pre>";
        print_r($formatted_data);
        echo "</pre>";
        // end insert
        $formatted_data = $this->load->view('home', $formatted_data, TRUE);
        return $formatted_data;
    }
3. To debug your problem insert the print_r line and see if the model gives you an array as expected. If not, you have errors in your model. Personally i suspect your controller and view code to be the error source...

4. I have no experience with ajax, so maybe the last two lines of code are correct in your ajax context. The "normal" codeigniter way would be to have only
Code:
$this->load->view('home', $formatted_data, TRUE);
and no return at the end of your function.

In your view:
Code:
<p class="message">
    <span class="timestamp">&lt;?php echo $returned_messages['timestamp'];?&gt;</span>
    <span class="username">&lt;?php echo $returned_messages['username'];?&gt;</span>
    &lt;?php echo $returned_messages['message'];?&gt;
</p>
5. remember the result() function in your model? You have to use something like:
Code:
&lt;?php echo $returned_messages->timestamp;?&gt;

Hope this can help you a bit.
And have a look at the userguide. It is really good and helpfull.

Marco
#7

[eluser]CodyyC[/eluser]
Wow that was really helpful. Thanks!

Hello, I was wanting to use the $last_message_id as on offset to return the rows after its value as a way to get only the messages after those which the user has read.

Okay I have managed to get the array to print using the following:

message.php
Code:
&lt;?php

class Message extends Controller {
    
    function load_message_store_model(){
        $this->load->model('Message_store_m', '', TRUE);
        $this->Message_store_m->message_store();
    }
    
    function load_message_read_model(){
        $this->load->model('Message_read_m', '', TRUE);
        $formatted_data = $this->Message_read_m->message_read();
        //for debugging insert:
        echo "<pre>";
        print_r($formatted_data);
        echo "</pre>";
        // end insert
        $formatted_data = $this->load->view('home', $formatted_data, TRUE);
        return $formatted_data;
    }

}

?&gt;

and message_read_m.php
Code:
&lt;?php
class Message_read_m extends Model {
    
    var $username = '';
    var $last_message_id = '';
    
    function __construct()
    {
        parent::__construct();
    }
    
    function message_read(){
        $this->load->library('session');
        $this->username    = ($this->session->userdata('username')) ? $this->session->userdata('username') : 'Anonymous';
        $this->last_message_id = ($this->session->userdata('last_message_id')) ? $this->session->userdata('last_message_id') : '0';
        $get_messages = $this->db->get('chat_logs');
        $last_message_row = $get_messages->last_row('Array');
        $last_message_id = $last_message_row['message_id'];
        $this->session->set_userdata('last_message_id', $this->last_message_id);
        
        return $get_messages->result();
    }
}

?&gt;

But I am still unable to format the results using the view (which I have also changed):
Code:
&lt;?php
foreach ($formattedData as $returned_messages)
{
?&gt;
<p class="message">
    <span class="timestamp">&lt;?php echo $returned_messages->timestamp;?&gt;</span>
    <span class="username">&lt;?php echo $returned_messages->username;?&gt;</span>
    &lt;?php echo $returned_messages-<message;?&gt;
</p>
&lt;?php
}
?&gt;
#8

[eluser]CodyyC[/eluser]
Okay, I have made some progress. I am now only having trouble with the loop in the view. The array is being passed to the controller, with fields accessible by $query['field'] etc.

Here is the controller:
Code:
&lt;?php

class Message extends Controller {
    
    function load_message_store_model(){
        $this->load->model('Message_store_m', '', TRUE);
        $this->Message_store_m->message_store();
    }
    
    function load_message_read_model(){
        $this->load->model('Message_read_m', '', TRUE);
        $formatted_data = $this->Message_read_m->message_read();
        foreach ($formatted_data->result_array() as $row)
        {
            echo $row['message'];
            
        }    
        $formatted_data = $this->load->view('message_view', $formatted_data, TRUE);
        //for debugging insert:

        // end insert
        $this->load->view('home', $formatted_data);
        return $formatted_data;
    }

}

?&gt;


and the view:
Code:
&lt;?php
foreach ($formatted_data as $returned_messages)
{
?&gt;
<p class="message">
    <span class="timestamp">&lt;?php echo $timestamp;?&gt;</span>
    <span class="username">&lt;?php echo $username;?&gt;</span>
    &lt;?php echo $message?&gt;
</p>
&lt;?php
}
?&gt;

Help would be appreciated Smile
#9

[eluser]CodyyC[/eluser]
Okay, I have a new update now.

First here's the model / view(s) / controller :

Model:
Code:
&lt;?php
class Message_read_m extends Model {
    
    var $username = '';
    var $last_message_id = '';
    
    function __construct()
    {
        parent::__construct();
    }
    
    function message_read(){
        $this->load->library('session');
        $this->username    = ($this->session->userdata('username')) ? $this->session->userdata('username') : 'Anonymous';
        $this->last_message_id = ($this->session->userdata('last_message_id')) ? $this->session->userdata('last_message_id') : '0';
        $get_messages = $this->db->get('chat_logs');
        $last_message_row = $get_messages->last_row('Array');
        $last_message_id = $last_message_row['message_id'];
        $this->session->set_userdata('last_message_id', $this->last_message_id);
        return $get_messages;

    }
}

?&gt;

Controller:
Code:
&lt;?php

class Message extends Controller {
    
    function load_message_store_model(){
        $this->load->model('Message_store_m', '', TRUE);
        $this->Message_store_m->message_store();
    }
    
    function load_message_read_model(){
        $this->load->model('Message_read_m', '', TRUE);
        $formatted_data = $this->Message_read_m->message_read();
        $formatted_data = $formatted_data->result();
        $data['formatted_data'] = $formatted_data;
        $this->load->view('message_view', $data);
        $this->load->view('home', $data);
        return $data;
    }

}

?&gt;

View (formatting)
Code:
<pre>
&lt;?php
var_dump($formatted_data);
foreach ($formatted_data as $returned_messages)
{
?&gt;

<p class="message">
    <span class="timestamp">&lt;?php echo $returned_messages->timestamp;?&gt;</span>
    <span class="username">&lt;?php echo $returned_messages->username;?&gt;</span>
    &lt;?php echo $returned_messages->message;?&gt;
</p>
&lt;?php
}
?&gt;

and main page:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    &lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Chatulo.us /Home&lt;/title&gt;
        &lt;meta http=equiv"Content-Type" content="text/html; charset=UTF-8"&gt;
        &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;application/assets/css/styles.css" /&gt;
        [removed][removed]
        [removed]
        $(document).ready(function() {
             $("#sendMessageForm").submit(function(postMessage) {
                 postMessage.preventDefault();
                $.post("&lt;?php echo base_url(); ?&gt;index.php/message/load_message_store_model", { message: $("#messageInputField").val()}, function (data) {
                    alert(data);
                });
             });
            
            function update() {
                $.post({
                    type: "POST",
                    url: "&lt;?php echo base_url(); ?&gt;index.php/message/load_message_read_model",
                    success: function (message) {
                        $('#messageBox').html(message.message).attr('scrollTop', function(){ return $(this).attr('scrollHeight') })
                      },
                    complete: function () {
                        setTimeout(update, 1000)
                    }
                });
            }
        update();
         });
        [removed]
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form method="post" name="messageInput" id="sendMessageForm"&gt;
            &lt;input name="message" id="messageInputField" type="text" autocomplete="off"/&gt;
            &lt;input name="submit" type="submit" value="Send"/&gt;
        &lt;/form&gt;
        <div id="messageBox">
        </div>
        <div id="usernameBox">
    </div>
    <span id="dummy"></span>
    <img src="&lt;?php echo base_url(); ?&gt;application/assets/images/logo.png" width="175" height="50" alt="Logo" id="chatulouslogo"/>
    &lt;/body&gt;
&lt;/html&gt;

Okay, basically, i cant access the values of the array. It seems that it is multidimensional.

Here is part of a var_dump (FYI the var dumps are the same in controller and view):
Quote:array(30) {
[0]=>
object(stdClass)#15 (5) {
["message_id"]=>
string(1) "1"
["message_type"]=>
string(1) "0"
["message"]=>
string(8) "blahblahblah"
["username"]=>
string(15) "testy"
["timestamp"]=>
string(5) "13:32"
}
[1]=>
object(stdClass)#16 (5) {
["message_id"]=>
string(1) "2"
["message_type"]=>
string(1) "0"
["message"]=>
string(4) "blah"
["username"]=>
string(0) ""
["timestamp"]=>
string(5) "12:25"
}
[2]=>
object(stdClass)#17 (5) {
["message_id"]=>
string(1) "3"
["message_type"]=>
string(1) "0"
["message"]=>
string(4) "blah"
["username"]=>

Any help with accessing the values in this array would be appreciated.

Regards and Thanks in Advanced
Cody
#10

[eluser]CodyyC[/eluser]
Anyone..?




Theme © iAndrew 2016 - Forum software by © MyBB