简体   繁体   中英

Display JSON format data

When a retrieve the according data from the database then encode it as json then pass it to the view:

    $json_data = json_encode($x);
    $search_results['search_results'] = $json_data;
    $this->load->view('findquestions',$search_results);

If I display this in the view we get:

[{"question_title":"java"},{"question_title":"big java book"}]

How can the question_titles so "java" be extracted and presented as a url link or inside a table

To process the results in PHP

In your controller:

$search_results['search_results'] = $x; //store the array
$this->load->view('findquestions',$search_results); //pass the results as PHP array to view

In your view:

foreach($search_results as $result){
   echo $result['question_title'];
}

Since you're passing the json-string directly to the view and then process it (for display) through PHP, you should be able to use PHP's json_decode() to convert it into an object (or array) to use it in a more-friendly manner:

// use json_decode with `true` to get an associative array
$results = json_decode($search_results['search_results'], true);

You can now access the data either directly or within a loop:

// direct access
$title1 = $results[0]['question_title'];

// in a loop
foreach ($results as $result) {
    echo 'Title: ' . $result['question_title'];
}

For reference, using the sample-data supplied in the question, a print_r() of the data:

print_r($results);

Array (
    [0] => Array (
            [question_title] => java
        )
    [1] => Array (
            [question_title] => big java book
        )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM