Welcome Guest, Not a member yet? Register   Sign In
Codeigniter + Ajax best practise?
#1

[eluser]omgnoseat[/eluser]
Hello,

I'm actually pretty new to HTML but do have experience with other programming languages. I couldn't handle the total mess with html, php and javascript and decided to pick up codeigniter to have a little more structure in my projects.
I really like the MVC pattern so far.

Anyway, I have been looking up on the best ways to implement ajax in a codeigniter project.
This is the solution I have come up so far:

controller
Code:
Class Home extends Controller{
    
    public function Home(){
        
        parent::controller();
    }
    
    public function index(){
        
        $this->load->view('home');
        
    }
    
    public function nieuws(){
        
        
        $result = $this->nieuws_model->get_article(1);
        echo $result; //what whould be the right way to pass the retrieved data to javascript/html?
    }
}

Model
Code:
Class nieuws_model extends Model{
    
    function get_article($ID){
        
        
        
        $this->db->where('ID', $ID);
        $query = $this->db->get('producten');
        
        return $query->result();
    }
    
}

Javascript (jquery)
Code:
$(document).ready(function(){
                
                $.ajax({
                    type: "POST",
                    url: "http://localhost/website/index.php/home/nieuws",
                    //url: "<?php echo base_url(); ?> /index.php/home/nieuws",
                    data: "testdata=1",
                    
                    beforeSend: function(){
                    
                    //$('#loginform').fadeOut('slow');
                        alert("going to send");
                    },
                    success: function(data){
                        //display message back to user here
                        alert(data);
                    },
                    
                    complete: function(data){
                    
                    },
                    
                    error: function(data, data2){
                    
                        alert("error: " + data + data2)
                    }
                });
                
            });

I actually do get the data from the model, I just don't know what is the best way to pass it back to the javascript so I can use it to update a part of the website (without a reload). I can't acces the retrieved data like normally $result['variable']. Print_r on the result shows the following:

Code:
Array ( [0] => stdClass Object ( [ID] => 1 [product_naam] => scooter 1 [product_omschrijving] => beschrijving 1 [product_afbeelding] => afbeelding1.jpg [product_datum] => 0000-00-00 00:00:00 ) )
I'm unfamiliar with the stdClass Object, so I have no idea how to resolve this.

edit:
Just found out about result_array(), it's a little step forward but it still makes it hard to handle a large about of data being returned to javascript. this still feels like a very inefficient way thought, since I would have to format all the data in javascript. I think it would be best to somehow passed the retrieved data by ajax to php and handle it as you normally would handle retrieved data from codeigniter.

Any help would greatly be appreciated. I'm pretty new on codeigniter so don't be too hard on me haha.

Thanks in advance.
#2

[eluser]Unknown[/eluser]
in controller
$data = json_encode($result);
echo $data;

in javascript
success: function (data) {
result = eval('(' + data + ')');
...

now you should have your php array as javascript array




Theme © iAndrew 2016 - Forum software by © MyBB