2

因此,我的网站解析 JSON 数据,并将其分解为来自 NY Times API 的单独故事。

我正在尝试将这些单独的故事加载到我的 MySQL 数据库中。

下面的代码运行页面但出现错误:

A Database Error Occurred

You must use the "set" method to update an entry.

Filename: /Applications/MAMP/htdocs/topline/controllers/index.php

Line Number: 29

在下面取出我的 api 密钥。

模型

<?php

    class Api_model extends CI_Model {

        public function __construct() {

        } // End of construct

        public $offset;


        public function get_news() {


        $offset = 0;

        // create curl resource 
        $ch = curl_init(); 

        // set url 
        curl_setopt($ch, CURLOPT_URL, "http://api.nytimes.com/svc/mostpopular/v2/mostviewed/all-sections/1.json?offset=" . $offset . "&api-key=mykey"); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

        // $output contains the output string 
        $output = curl_exec($ch); 

        // close curl resource to free up system resources 
        curl_close($ch);      

        $Obj = json_decode($output);

        return $Obj;


        } // End of function

    } // End of class

?>

看法

<section>
    <?php

    $paginate = site_url('/pageup');

    //loop through json response items  
    foreach ($articles->results as $article){

        //error reporting turned off for this process because some objects have more than 1 photo
        error_reporting(0);

        echo "<div class='story'>";
            echo "<div class='photo'>";
                //pass image into an img tag
                echo "<img src='" . $article->media[0]->{"media-metadata"}[0]->url . "' />";
            echo "</div>";
            echo "<div class='story-text'>";
                //call the story headline
                echo "<h3>" . $article->title . "</h3>";
                //call the description
                echo "<p>" . $article->abstract . "</p>";
                //call the link to the full story
                echo "<p class='btn btn-primary'><a href='" . $article->url .  "' target='_blank'>Read More</a></p>";
            echo "</div>";
        echo "</div>";

        //setup database array
        $article_data = array(
            'id' => 1,
            'image' => $article->media[0]->{"media-metadata"}[0]->url,
            'body' => $article->abstract,
            'title' => $article->title,
            'link' => $article->url         
        );
    }

    //var_dump($articles->results[0]);

    ?>  

    <p><a href="<?php echo $paginate; ?>" class="btn btn-block">Load More Stories</a></p>

</section>

控制器

<?php

    class Index extends CI_Controller
    {
        public function __construct() {

            parent::__construct();

            session_start();

        } 

        public function index() {

            $data['title'] = "Topline Press";

            // Load the model
            $this->load->model('Api_model');

            // Call the method of the model
            $data['articles'] = $this->Api_model->get_news();

            // Load the views
            $this->load->view('header', $data);
            $this->load->view('start', $data);          
            $this->load->view('footer', $data);

            //push story data into database
            $this->db->insert('stories', $article_data);

        } 

    } // End of class

?>

在这方面相当新,所以我很感激任何帮助。提前致谢。

4

1 回答 1

2

您必须在插入数据库之前设置值:

$this->db->set('column_in_stories_table', $value_to_place_in_table); 
$this->db->insert('stories'); 

// Produces: INSERT INTO stories (column_in_stories_table) VALUES ('{$value_to_place_in_table}')

如果需要,您还可以将模型对象从控制器存储到数据库中:

/*
    class Api_model {
        $title = 'My Title';
        $content = 'My Content';
        $date = 'My Date';
    }
*/

$obj = new Api_model;

$this->db->set($obj);
$this->db->insert('stories');
于 2013-08-27T03:19:04.953 回答