0

I have a problem in jQuery, on how to get data from my codeigniter controller to jQuery function.

I am having a progress bar in jQuery and i need the value of the progress bar will be depend on the output of the controller; i use json_encode in controller.

Jquery

$(function() {
  $( "#progressbar" ).progressbar({
  value: //get value from the controller json_encode
      });
  });

Controller

public function countScoreCh(){
$id = $this->session->userdata('userID');
$data['getScore'] = $this->lawmodel->battleUserID($id);
foreach($data['getScore'] as $row){
    $scoreCH = $row->challengerScore;
    echo json_encode(
    array(
    'scoreCH' => $scoreCH,
            )
        );
}

}

the progress bar function will be something like this.

$(function() {
  $( "#progressbar" ).progressbar({
  value:   //jsn.scoreCH value
      });
  });

Is there's a possible way of doing it? I don't know if using json_encode is the correct way. But any solution will do..:)

Thanks..

3
  • AJAX is what ya need! Commented Jan 19, 2014 at 2:39
  • @Zurreal: What controller do you mean? This $id = $this->session->userdata('userID'); could be php code. Are you using php? Commented Jan 19, 2014 at 4:20
  • @threeFourOneSixOneThree: yeah..im using PHP with COdeigniter Framework..xD Commented Jan 19, 2014 at 4:26

1 Answer 1

3

I think your controller doesnt produce the valid json. since it would produce a json string like this one :

{scoreCH:<SomeValue>}{ScoreCH:<SomeValue>}{ScoreCH:<Somevalue>}

It would be better if you put the set of ScoreCH value inside some "json wrapper" so you should modify you controller to create a temporary variable that contain all your values from the model like this one :

public function countScoreCh(){
    $id = $this->session->userdata('userID');
    $data['getScore'] = $this->lawmodel->battleUserID($id);
    // Here is "my hack"
    $output = array(); // it will wrap all of your value
    foreach($data['getScore'] as $row){
          unset($temp); // Release the contained value of the variable from the last loop
          $temp = array();

          // It guess your client side will need the id to extract, and distinguish the ScoreCH data
          $temp['id_of_ScoreCH'] = $row->id;
          $temp['ScoreCH'] = $row->ScoreCH;

          array_push($output,$temp);
    }
    // Now the $output supposed to be a two dimensional array looks like this one
    // array(
    //    [0]=>array("id_of_ScoreCH"=>SomeID,"ScoreCH"=>SomeValue),
    //    [1]=>array("id_of_ScoreCH"=>SomeID,"ScoreCH"=>SomeValue),
    //    [2]=>array("id_of_ScoreCH"=>SomeID,"ScoreCH"=>SomeValue)
    //   );

    // Then emit the wrapped value It would produce array of object JSON
    // Dont forget to put the header format
    header('Access-Control-Allow-Origin: *');
    header("Content-Type: application/json");
    echo json_encode($output);
 }

Next on your client side (HTML) use the JSON.parse(<string_retrieved_from_controller>) like this one :

$.ajax({
    url:<your_controller_name/you_method_name>,
    data: <your_specific_data_you_want_to_pass_to_the_server>,
    method:<post/get>
    success:function(retrieved_data){
         // Your code here.. use something like this
         var Obj = JSON.parse(retrieved_data)

         // Since your controller produce array of object you can access the value by using this one :
         for(var a=0; a< Obj.length; a++){
              alert("the value with id : " + Obj.id_of_scoreCH + "is " + Obj.ScoreCH);
         }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

wow..its really work..i find it a little bit hard at first..but i got it anyway..:) thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.