xml insert into database |
kenapa cangkal melakukan ini? karena kita ketahui data RSS selalu update sehingga parsing XML hari kameren berbeda dengan data parsing hari ini.
nah data yang ingin kita simpan ke dalam database tetap sama fiednya yaitu title, link, pubDate dan description. cangkal pada tips ini menggunakan database MySql, ayo sob kita create table t_xml pada database coba. ini scriptnya
CREATE TABLE IF NOT EXISTS `t_xml` ( `id_xml` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(200) NOT NULL, `link` varchar(200) NOT NULL, `pubDate` datetime NOT NULL, `description` text NOT NULL, PRIMARY KEY (`id_xml`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=325 ;
setelah konfigurasi database selesai, saatnya kita koding PHP menggunakan codeigniter, hal yang akan disiapkan pada CI seperti struktur berikut
- aplication
- config
- database.php
- controller
- Xml.php
- model
- Xml_model.php
kita ikuti konfigurasi satu demi satu struktur yang diatas, untuk konfigurasi database.php seperti dibawah ini
$active_group = 'default'; $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'coba', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
kemudian kita ke bagian controller Xml.php, kita buat function insertdetik seperti list script di bawah ini
public function detikinsert () { $feed_url="http://rss.detik.com/index.php/inet"; $content = file_get_contents($feed_url); $x = new SimpleXmlElement(html_entity_decode($content)); $dataArr = array(); foreach($x->channel->item as $row){ $title = $row->title; $link = $row->link; $pubDate = date("Y-m-d H:i", strtotime($row->pubDate)); $description = $row->description; $dataArr[] = "('$title', '$link', '$pubDate', '$description')"; } // echo $pubDate; $this->load->model('Xml_model'); $this->Xml_model->insert($dataArr); $this->session->set_flashdata('message', 'Create Record Success'); redirect(site_url('xml/detik')); }
setelah itu kita masuk ke bagian model Xml_model.php buat function insert seperti script ini, coding ini menggunakan implode agar dapat memisah data array $dataArr
yang berjumlah 10 row.
// insert data function insert($dataArr) { $query_str = 'insert into t_xml (title,link, pubDate, description) values '.implode(',', $dataArr).''; $query = $this->db->query($query_str); }
ok sob, sekarang jalankan di url browser kamu http://localhost:808/enjoyit/index.php/xml/insertdetik ,kemudian cek data di database mysql menggunakan phpmyadmin. apakah data tersimpan seperti gambar di bawah ini sob? berarti sobat sudah berhasil melakukan tips ini
insert xml to database |
regards,
cangkal
Your Affiliate Profit Machine is waiting -
BalasHapusPlus, making money with it is as easy as 1--2--3!
This is how it works...
STEP 1. Choose affiliate products you want to push
STEP 2. Add some push button traffic (it ONLY takes 2 minutes)
STEP 3. Watch the affiliate products system grow your list and sell your affiliate products all for you!
Do you want to start making money?
Click here to launch the system
Your Affiliate Money Making Machine is ready -
BalasHapusPlus, getting it set up is as easy as 1, 2, 3!
Follow the steps below to make money...
STEP 1. Tell the system what affiliate products the system will promote
STEP 2. Add some push button traffic (it LITERALLY takes JUST 2 minutes)
STEP 3. Watch the system grow your list and upsell your affiliate products all by itself!
Are you ready???
Click here to check it out
Did you hear there is a 12 word phrase you can speak to your man... that will induce intense feelings of love and impulsive appeal for you buried inside his heart?
BalasHapusBecause deep inside these 12 words is a "secret signal" that fuels a man's impulse to love, please and care for you with all his heart...
====> 12 Words That Fuel A Man's Love Instinct
This impulse is so built-in to a man's brain that it will drive him to work better than before to take care of you.
Matter-of-fact, fueling this influential impulse is absolutely mandatory to having the best ever relationship with your man that as soon as you send your man one of these "Secret Signals"...
...You'll soon find him open his soul and heart to you in such a way he haven't expressed before and he'll recognize you as the only woman in the galaxy who has ever truly appealed to him.