Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display data via $.get function using jQuery?
    primarykey
    data
    text
    <p>I am having this issue I have been struggling with for sometime and now I need help: I have the following array </p> <pre><code>array(2) { [0]=&gt; object(stdClass)#4 (4) { ["id"]=&gt; string(1) "1" ["idMake"]=&gt; string(1) "1" ["modelName"]=&gt; string(6) "Legend" ["modelYear"]=&gt; string(4) "1986" } [1]=&gt; object(stdClass)#5 (4) { ["id"]=&gt; string(1) "2" ["idMake"]=&gt; string(1) "1" ["modelName"]=&gt; string(3) "MDX" ["modelYear"]=&gt; string(4) "2000" } } </code></pre> <p>How can I use it the <code>$.get()</code> function via jQuery in order to have something like this: </p> <pre><code>id Model Year 1 Legend 1986 2 MDX 2000 </code></pre> <p>I have already tried the following:</p> <p>a process.php file: </p> <pre><code>&lt;?php require 'DataLayer.class.php'; $dl = new DataLayer(); //get car make models $models = $dl-&gt;getCarModels($id); if(isset($models)){ echo json_encode(json_encode($models)); } else{ echo 'failed'; } ?&gt; </code></pre> <p>the getCarModels function:</p> <pre><code>public function getCarModels($id){ $stmt = $this-&gt;pdo-&gt;prepare("SELECT * FROM model WHERE idMake=? ORDER BY modelName"); $stmt-&gt;execute(array($id)); return $stmt-&gt;fetchAll(PDO::FETCH_OBJ); } </code></pre> <p>Javascript function called upon clicking on some links</p> <pre><code>function getCarModels(id, make){ $.get(process.php, function(data){ var models = $.parseJSON(data); for(var model in models.model.modelName){ // I got stuck here } }); } </code></pre> <p>Hope this makes any sense to someone. Thanks.</p> <p>Just find out some similar issue at this link: <a href="https://stackoverflow.com/questions/7383919/php-multidimensional-array-into-jquery">php multidimensional array into jQuery</a> I am giving a try. </p> <hr> <p>Like I said from the start, my issue is quite similar to the one posted at this link <a href="https://stackoverflow.com/questions/7383919/php-multidimensional-array-into-jquery">php multidimensional array into jQuery</a>. 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. </p> <p>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.</p> <p>Thanks again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload