2013-08-27 35 views
2

因此,我的站點解析了JSON數據,並將其從紐約時報API中分離出來。將json數據推送到codeigniter中的數據庫中

我試圖加載這些單獨的故事到我的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 

?> 

在這個相當新的,所以我感謝所有幫助。提前致謝。

回答

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'); 
+0

是的 - 似乎它現在工作。謝謝你的解釋。 – user2673468

+0

@ user2673468如果答案有幫助並解決了您的問題,您應該點擊旁邊的綠色複選標記將其標記爲「已接受」,這讓社區知道您的問題現在已解決 –

相關問題