-1

I scraped date from another website and then insert all fetching values into my table sracp-data. My Value insert successfully, but only last value insert.

My Fetching values look like:

enter image description here

My Fetching Cood:I Used for scraping data with SIMPLE HTML DOM

            include './simple_html_dom.php';       //Inlcude HTML DOM
            $html = file_get_html('http://tacticalwalls.com/shop/');  //Scrapping site/url
              //**ul[class=products] here is specific DIV values** 
            foreach ($html->find('ul[class=products]') as $items) {
                foreach ($items->find('a') as $anchor) {          
                    $item_a = $anchor->href;
                }
                foreach ($items->find('img') as $img){
                    $item_img = $img->src;
                }

                 $db_conn = mysql_connect('localhost', 'root', '') or die('error');
                    mysql_select_db('db_scrap', $db_conn) or die(mysql_error());

                    $sql = "INSERT INTO scrap_data(url, imges) VALUES ('".$item_a."', '".$item_img."')";

                    mysql_query($sql) or die(mysql_error()); 
                 }

My Problem is, How can I Insert all fetching value into table specific column. Like < a > Tag insert into the url column and < img > tag value insert into the images colunmn?

rjony
  • 331
  • 3
  • 7

1 Answers1

0

You are always overwriting your $item_a and $item_img in your foreach.

You need something like

$items_a = array();
foreach($items->find('a') as $anchor) {
    array_push($items_a, $anchor->href)
}

Same goes for images. At the end, simply implode the array

$item_a = implode(';', $items_a);

and you are done.

        include './simple_html_dom.php';       //Inlcude HTML DOM
        $html = file_get_html('http://tacticalwalls.com/shop/');  //Scrapping site/url
          //**ul[class=products] here is specific DIV values** 
        foreach ($html->find('ul[class=products]') as $items) {
            $items_a = array();
            foreach ($items->find('a') as $anchor) {          
                array_push($items_a, $anchor->href);
            }
            $items_img = array();
            foreach ($items->find('img') as $img){
                array_push($items_img, $img->src);
            }
            $item_a = implode(';', $items_a);
            $item_img = implode(';', $items_img);
            $db_conn = mysql_connect('localhost', 'root', '') or die('error');
            mysql_select_db('db_scrap', $db_conn) or die(mysql_error());

            $sql = "INSERT INTO scrap_data(url, imges) VALUES ('".$item_a."', '".$item_img."')";

            mysql_query($sql) or die(mysql_error()); 
        }
flowna
  • 11
  • 1