1

I am having this issue I have been struggling with for sometime and now I need help: I have the following array

array(2) {
  [0]=>
  object(stdClass)#4 (4) {
    ["id"]=>
    string(1) "1"
    ["idMake"]=>
    string(1) "1"
    ["modelName"]=>
    string(6) "Legend"
    ["modelYear"]=>
    string(4) "1986"
  }
  [1]=>
  object(stdClass)#5 (4) {
    ["id"]=>
    string(1) "2"
    ["idMake"]=>
    string(1) "1"
    ["modelName"]=>
    string(3) "MDX"
    ["modelYear"]=>
    string(4) "2000"
  }
}

How can I use it the $.get() function via jQuery in order to have something like this:

id   Model      Year
  1    Legend     1986
  2    MDX        2000

I have already tried the following:

a process.php file:

<?php
require 'DataLayer.class.php';
$dl = new DataLayer();

//get car make models
$models = $dl->getCarModels($id);
if(isset($models)){
    echo json_encode(json_encode($models));
}
else{
    echo 'failed';
}
?>

the getCarModels function:

public function  getCarModels($id){
    $stmt = $this->pdo->prepare("SELECT  * FROM model WHERE idMake=? ORDER BY modelName");
    $stmt->execute(array($id));
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}

Javascript function called upon clicking on some links

function getCarModels(id, make){
    $.get(process.php, function(data){       
        var models = $.parseJSON(data); 
        for(var model in models.model.modelName){ // I got stuck here

        } 
    });
} 

Hope this makes any sense to someone. Thanks.

Just find out some similar issue at this link: php multidimensional array into jQuery I am giving a try.


Like I said from the start, my issue is quite similar to the one posted at this link php multidimensional array into jQuery. My only question is that I really don't know the reason why they use the json_encode() function twice as all I did to make it work properly is to remove one of the json_encode() functions.

Will appreciate some explanation of the reason why I used only jsan_encode() function to get the result I wanted while in most tutorial it has been two time before outputting the data.

Thanks again.

4
  • 1
    It might help to use firebug to see the contents of models as well as the contents of the GET response from the server. Commented Mar 16, 2012 at 15:51
  • Thanks for suggestion: I have the following string: [{"id":"1","idMake":"1","modelName":"Legend","modelYear":"1986"},{"id":"2","idMake":"1","modelName":"MDX","modelYear":"2000"}] not too sure how I can handle this type of string instead of an array Commented Mar 16, 2012 at 15:55
  • oi, oi what's is returned is a json array, you can use Jquey $.each method to loop through it. if you are not sure use firebug debug mode to see what happens as suggested by @justin Commented Mar 16, 2012 at 16:10
  • Well, I have an empty response using the firebug debug mode. Quite weird... Commented Mar 16, 2012 at 16:17

1 Answer 1

1

You don't need to use parseJSON within jQuery ajax, jQuery will already handle that. Your data is an array of objects. Here's example to loop over it with $.each

Your getCarModels() function isn't set up to send data

function getCarModels(id, make){

     var dataToServer={ id: id, make: make};/* need to match these keys to $_GET keys in php*/
    $.get(process.php, dataToServer, function(data){       
        $.each( data, function(i, item){
            $('body').append('<p>'+item.modelName+'</p>');
        })
    });
}

The php has issues also , it doesn't appear you are looking for $_GET from the ajax to pass to your query methods. I don't recognize the framework functions used in your php but to pass the ID to getCarModels in pphp you would need something like:

 $id=$_GET['id'];
 //get car make models
 $models = $dl->getCarModels($id);
Sign up to request clarification or add additional context in comments.

2 Comments

without the parseJSON, 'alert(data)' gives me '"[{\"id\":\"1\",\"idMake\":\"1\",\"modelName\":\"Legend\",\"modelYear\":\"1986\"},{\"id\":\"2\",\"idMake\":\"1\",\"modelName\":\"MDX\",\"modelYear\":\"2000\"}]"' I guess it is because of the 'json_encode(json_encode($models))' I used in the process.php file. I found a quite similar issue at this page: [Stack Overflow](stackoverflow.com/questions/7383919/…). I keep having undefined value as I do '$.each(models, function(){ alert(models[0]['modelName']); })'
use json as datatype in ajax or use $.getJSON, also if issues add header application/json in php and mke sure nothing else is being echo'd

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.