Jump to content

need help in displaying images using codeigniter and ajax


doforumda

Recommended Posts

I am using json to display images using php codeigniter. What I am trying to do is when I click a particular t shirt image then it should display on container div tag. Currently I am getting json code in this format

 

[{"rows":[{"shirtid":"8","shirt_thumburl":"shirt_designs\/thumbs\/thumb_blue_four.png","shirturl":"shirt_designs\/blue_four.png"}]}]

 

So Now how can I get the shirturl from this json data to display that particular image?

 

here is my code

here is my controller

 

class Shirt extends CI_Controller {
    public function index() {
        $this->shirt_thumbs();
    }
    
    public function shirt_thumbs() {
        $data['rows'] = $this->Model_shirt_thumbs->get_thumbs();
        //var_dump($data);
        $this->load->view('view_home',$data);
    }
    
    public function get_tshirt() {
        
        extract($_POST);
        
        $data['rows'] = $this->Model_shirt_thumbs->get_tshirt($id);
        
        $JSON_array = array($data);
        $JSON_response = json_encode($JSON_array);
        header('Contents: type/json');
        echo $JSON_response;
    }
} 

 

here is my .js file

 

$(function(){
    $('.thumbs').click(function() {
        var thisid = $(this).attr('id');
        var url = 'http://localhost/shirt_designer/shirt/get_tshirt/';
        $.ajax({
            type: 'POST',
            url: url,
            data: 'id=' + thisid,
            dataType: 'json',
            success: function(result) {
                $('#contents').html(result.shirtid);
            }
        });
    })
}); 

Now I changed my .js file to this code. I am using getJSON to get photos from the server

 

$(function(){
    $('.thumbs').click(function() {
        var thisid = $(this).attr('id');
        var url = 'http://localhost/shirt/shirt/get_tshirt';
        
       $.getJSON(url,{id: thisid},function(data){
           alert(data.shurturl);
           $('#contents').append("<img src='" + data.shirturl + "' />");
       });
    });
});

 

I am using php with codeigniter on the servside.

 

here is the code for my controller

<?php
class Shirt extends CI_Controller {
    public function index() {
        $this->shirt_thumbs();
    }
    
    public function shirt_thumbs() {
        $data['rows'] = $this->Model_shirt_thumbs->get_thumbs();
        //var_dump($data);
        $this->load->view('view_home',$data);
    }
    
    public function get_tshirt() {
        
        extract($_GET);
        $sql = "SELECT * FROM shirts WHERE shirtid=$id";
        $query = mysql_query($sql);
        while($row = mysql_fetch_assoc($query)) {
            $rows[] = array(
                "shirtid" => $row['shirtid'],
                "shirt_thumb" => $row['shirt_thumburl'],
                "shirturl" => $row['shirturl']
            );
        }
        $json = json_encode($rows);
        //$callback = $_GET['callback'];
        echo $json;
    }
}
?>

 

here is what I am getting back from the as json data

[{"shirtid":"5","shirt_thumb":"shirt_designs\/thumbs\/thumb_blue_one.png","shirturl":"shirt_designs\/blue_one.png"}]

 

but when I alert shirturl then it displays "undefined"

 

anyone who can help? Please

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.