doforumda Posted August 26, 2011 Share Posted August 26, 2011 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); } }); }) }); Quote Link to comment https://forums.phpfreaks.com/topic/245746-need-help-in-displaying-images-using-codeigniter-and-ajax/ Share on other sites More sharing options...
doforumda Posted August 26, 2011 Author Share Posted August 26, 2011 anyone who can help? Quote Link to comment https://forums.phpfreaks.com/topic/245746-need-help-in-displaying-images-using-codeigniter-and-ajax/#findComment-1262292 Share on other sites More sharing options...
doforumda Posted August 27, 2011 Author Share Posted August 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245746-need-help-in-displaying-images-using-codeigniter-and-ajax/#findComment-1262553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.