#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0

    Mysqli insert parsed array containing original original tags


    I would really like for someone to take a little time and look over my code. I'm parsing some news content and I can insert the initial parse into my database which contains the news URL and the title. I'd like to expand it farther, to pass along each article link and parse the content of the article and include it in my database. The initial parsing works perfectly like this:

    PHP Code:
    include_once ('connect_to_mysql.php');
    include_once (
    'simple_html_dom.php');
    $html file_get_html('http://basket-planet.com/ru/');
    $main $html->find('div[class=mainBlock]'0);
      
    $items = array();
      foreach (
    $main->find('a') as $m){
        
    $items[] = '("'.mysql_real_escape_string($m->plaintext).'",
                    "'
    .mysql_real_escape_string($m->href).'")';
      }
    $reverse array_reverse($items);
    mysql_query ("INSERT IGNORE INTO basket_news (article, link) VALUES 
                 "
    .(implode(','$reverse)).""); 
    As you can see, I'm using PHP Simple HTML DOM Parser. To expand, I'm trying to use mysqli statement where I can bind the parameters so all the html tags get inserted into my database. I've done this before with XML parsing. Problem is I don't know how to bind the array, and see whether my code is correct, if it will work this way... Here's the entire code:


    PHP Code:
    $mysqli = new mysqli("localhost""root""""test");
    $mysqli->query("SET NAMES 'utf8'");
    include_once (
    'simple_html_dom.php');
    $html file_get_html('http://basket-planet.com/ru/');
    //find main news
    $main $html->find('div[class=mainBlock]'0);
    $items = array();
      foreach (
    $main->find('a') as $m){
        
    $h file_get_html('http://www.basket-planet.com'.$m->href.'');
        
    $article $h->find('div[class=newsItem]');
        
    //convert to string to be able to modify content
        
    $a str_get_html(implode("\n", (array)$article));
          if(isset(
    $a->find('img'))){
            foreach (
    $a->find('img') as $img){
              
    $img->outertext '';}} //get rid of images
          
    if(isset($a->find('a'))){
            foreach (
    $a->find('a') as $link){
              
    $link->href 'javascript:;';
              
    $link->target '';}} //get rid of any javascript
          
    if(isset($a->find('iframe'))){
            foreach (
    $a->find ('iframe') as $frame){
              
    $frame->outertext '';}} //get rid of iframes
         
    @$a->find('object'0)->outertext '';
         @
    $a->find('object'1)->outertext '';
         
    //modify some more to retrieve only text content
         //put entire content into a div (will if statements work here???)
         
    $text_content '<div>'.$a.'<br>'.
           if (!empty(
    $a->find('object'0)->data)){
             echo 
    '<a target="_blank" href="'.$a->find('object'0)->data.'">Play Video</a>&nbsp;&nbsp;';}
           if (!empty(
    $a->find('object'1)->data)){
             echo 
    '<a target="_blank" href="'.$a->find('object'1)->data.'">Play Video</a>&nbsp;&nbsp;';}
           
    //couple more checks to see if video links are present
        
    .'</div>';
    $items[] = '("'.$m->plaintext.'","'.$m->href.'","'.$text_content.'")';
    //reverse the array so the latest items have the last id
    $reverse array_reverse($items);
    $stmt $mysqli->prepare ("INSERT IGNORE INTO test_news (article, link, text_cont) VALUES (?,?,?)");
    $stmt->bind_param ???; //(implode(',', $reverse));
    $stmt->execute();
    $stmt->close(); 
    So the logic is for every href of an article found, I'm passing it to parse the content and I'm trying to add it to the array. I probably have a ton of errors but I can't test it yet because I don't know how to bind it to see if it works. And I'm also not sure if I can do the if statements inside $text_content div...meaning to display "Play Video" if they exist. So please, if someone can take time to work on this with me I would really appreciate it.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,378
    Rep Power
    594
    If you want someone to look over your code, you need to format it to make it readable. Read the sticky at the top of this forum that says

    READ THIS BEFORE POSTING
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    10
    Rep Power
    0
    Thanks for the advice. New to this forum...didn't know about wrapping php tags. Hope this is better
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,378
    Rep Power
    594
    If I understand what you want, there are examples (including wrappers) in the bind_param documentation.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo