Langsung ke konten utama

Codeigniter menyimpan data parsing XML ke database

xml insert into database
pada tips sebelumnya kita hanya parsing data XML RSS kemudian langsung menampikan data apa yang diparsing pada waktu itu juga, nah sekarang cangkal mencoba sebelum data ditampikan, terlebih dahulu menyimpan data ke database, sehingga data XML yang diparsing sebelumnya masih tersimpan di 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


Komentar

  1. Your Affiliate Profit Machine is waiting -

    Plus, 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

    BalasHapus
  2. Your Affiliate Money Making Machine is ready -

    Plus, 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

    BalasHapus
  3. 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?

    Because 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.

    BalasHapus

Posting Komentar

penulis senang dengan adanya pembaca yang meninggalkan jejak. :)

Postingan populer dari blog ini

codeigniter mengambil data JSON dari URL

Sekarang pertukaran data baik itu dekstop, mobile, web sangat mudah dilakukan dengan adanya JSON. Banyak API yang menyediakan dengan format JSON, seperti api.tiket.com, api.cekresi.com bahkan perusahaan besar semacam facebook, google, twitter juga menggunakan API. Cangkal kali ini mencoba sharing bagaimana mengambil data JSON dari URL (website) menggunakan framework codeigniter. URL yang digunakan yaitu http://jsonplaceholder.typicode.com/posts/ . URL ini memang sengaja dibuat untuk digunakan sebagai testing. Data JSON yang diberi oleh URL tadi bentuknya seperti ini [{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }, { "userId": 1, "id": 2,

ip whatsapp dari dns cache mikrotik

selamat pagi sob, kita ketahui setiap administrator memanajemen rule alias lalu lintas mikrotik dengan berbagai cara untuk memenuhi kebutuhan instansi / perusahaan. pada postingan ini ilustrasinya adalah sebagai berikut sebuah perusahaan ingin menjalin komunikasi lewat media whatsapp, akan tetapi akses intenet harus ditutup. Oleh karena itu seorang administrator networking membuat rule sesuai permintaan perusahaan tersebut. Adress Lists Whatsapp

postfixadmin your email address or password is not correct. can't open file letsencrypt privkey.pem

[Wed Feb 02 08:53:49.429318 2022] [php7:notice] [pid 1445731] [client 180.252.186.123:64290] Failed to read password from /usr/bin/doveadm pw -r 5 ... stderr: doveconf: Fatal: Error in configuration file /etc/dovecot/conf.d/10-ssl.conf line 16: ssl_key: Can't open file /etc/letsencrypt/live/mail.binasaranasukses.com/privkey.pem: Permission denied\n, password: , referer: https://postfixadmin.example.com/login.php [Wed Feb 02 08:53:49.429526 2022] [php7:notice] [pid 1445731] [client 180.252.186.123:64290] Error while trying to call pacrypt(), referer: https://postfixadmin.example.com/login.php [Wed Feb 02 08:53:49.429590 2022] [php7:notice] [pid 1445731] [client 180.252.186.123:64290] Exception: /usr/bin/doveadm pw -r 5 failed, see error log for details in /var/www/postfixadmin/functions.inc.php:1060\nStack trace:\n#0 /var/www/postfixadmin/functions.inc.php(1275): _pacrypt_dovecot()\n#1 /var/www/postfixadmin/model/Login.php(36): pacrypt()\n#2 /var/www/postfixadmin/public/login.php