我有问题。是否有可能将我的xml文件放入数据库并从中访问它们。我有这个函数从xml文件中获取内容:
function getFeed()
{
$content = file_get_contents('http://feeds.reuters.com/reuters/technologyNews');
$x = new SimpleXmlElement($content);
foreach($x->channel->item as $entry) {
$feeds = array('title' => (string)$entry->title
);
$this->session->set_userdata($feeds);
$this->load->model('membership_model');
$this->membership_model->feeds();
}
将模型插入数据库的模型,然后将标题作为链接格式。但我想这不是一个好主意,因为它不起作用。
function feeds()
{
$this->db->empty_table('feeds');
$this->db->set('title',$this->session->userdata('title'));
$this->db->insert('feeds');
$get_feed = $this->db->get('feeds');
foreach ($get_feed->result() as $row){
echo "<a href='$row->title'>".$row->title."</a><br/>";
}
答案 0 :(得分:0)
您可以使用insert batch
首先准备数组以进行批量插入。
function getFeed()
{
$content = file_get_contents('http://feeds.reuters.com/reuters/technologyNews');
$x = new SimpleXmlElement($content);
foreach($x->channel->item as $entry) {
$feeds[] = array(
'title' => (string)$entry->title,
'url'=> (string)$entry->url, // ad more fields if needed
);
$this->load->model('membership_model');
$this->membership_model->feeds($feeds);
}
//load view with data. No need to get same data from db again.
$this->load->view("feed_list",array("feeds"=>$feeds));
}
模特:
function feeds($feeds_data)
{
$this->db->empty_table('feeds');
$this->db->insert_batch('feeds', $feeds_data);
}
在视图中
foreach($feeds as $f)
{
echo "<a href='".$f["url"]."'>".$f["title"]."</a><br/>";
}