Welcome Guest, Not a member yet? Register   Sign In
Problem with function appel in controler
#1

[eluser]Unknown[/eluser]
Hello,

I have a error, and i don't find any solution (i'm newbies).
this is the code of my controler :
Code:
<?php
class Messages extends Controller {
  function Messages(){
    parent::Controller();
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->helper('html');
    $this->load->model('Messages_model','message', true);
    $this->output->enable_profiler(true);
  }
  
  ... some code

  function valid_add(){ #validation du formulaire de saisie
    if ($_POST['site_id']!=0): //Si on écrit un message depuis la liste des sites    
         $this->message->insert($_POST['site_id']); <-- WORKS
        redirect('sites');
    else: //on viens de la gestion des messages, on ecrit un msg a tous lessites existants
        $sites = $this->db->get('sites');
        foreach($sites->result() as $site):
            $this->message->insert($_POST['site_id']); <-- ERROR
        endforeach;
        //redirect('messages');
    endif;
  }

  ... some code

}

?&gt;

my controler look like :

Code:
&lt;?php
class Messages_model extends Model {

    function Messages_model(){
        parent::Model();
        $this->load->helper('date');
    }
    
    function insert($id){
        $datestring = "%Y-%m-%d %h:%i:%s";
        $this->date = mdate($datestring, time());
        $this->titre   = $_POST['titre'];
        $this->message   = $_POST['texte'];
        $this->site_id   = $id;
        $this->db->insert('messages', $this);
    }

  some code ...

}
?&gt;


with this code, i have the folowing error :

Code:
Fatal error: Call to a member function insert() on a non-object in C:\wamp\www\SITE\system\application\controllers\messages.php on line 43

if i don't use foreach in my function it works.

Where is the error (don't say : a line 43 :lol: )

thank's, sorry for my english, i'm french.
#2

[eluser]webthink[/eluser]
what does print_r ($sites) look like?
#3

[eluser]Unknown[/eluser]
[quote author="webthink" date="1206620483"]what does print_r ($sites) look like?[/quote]

this is the result of the print_r function :

Code:
CI_DB_mysql_result Object ( [conn_id] => Resource id #39 [result_id] => Resource id #52 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 4 [row_data] => )

If i write this code :

Code:
foreach($sites->result() as $site):
    echo $site->id.'<br>';
endforeach;

i have the list of all my record. So, the result of my request is ok.
#4

[eluser]webthink[/eluser]
I'm not really sure how your app works but your conditionals are set up in such a way that it only ever gets to that line when $_POST['site_id'] == 0

So you're looping through all your sites and for each one you're doing the equivalent of $this->message->insert(0); where 0 becomes the ID of the message you're inserting. I don't think this was what you intended.




Theme © iAndrew 2016 - Forum software by © MyBB