PHP-MySQL使用Simple HTML Dom库将“Array”插入到foreach循环的表中

时间:2014-06-08 14:27:43

标签: php mysql simple-html-dom

我有一段类似于下面的代码:

    include 'simplehtmldom/simple_html_dom.php';
    ...
    ...
    foreach ($files as $file){
    $results= array();
        if(substr($file->getAttribute('href'),0,strlen($lookfor))==$lookfor){
            $URLs= $file->getAttribute('href');
            echo $URLs ."<br>";
            $html = file_get_html($URLs);
            foreach($html->find('div.postDisplay') as $post) {
                $item['date']     = $post->find('p.id.post-date', 0)->plaintext;
                $item['location']    = $post->find('p.id.post-location', 0)->plaintext;
                $title = $item['title']     = $post->find('h1.id.post-title', 0)->plaintext;
                $item['post'] = $post->find('div.post', 0)->plaintext;
                $results[] = $item;
            }
    print_r($results) ."</br>";
    ...
    ...
    ...
    $my_id ="1";
    $photos = "1";
    $insert_query = mysqli_query($db_connect, "INSERT INTO jackson.data (
        my_id, photos, results) VALUES (
        '$my_id', '$photos', '$results')");

代码在浏览器中回显$results值非常好;但是,当我将数据插入数据库时​​,results字段仅存储"Array"作为值。那么,我有什么遗失的东西吗?如何插入在我的浏览器而不是纯文本上回显的$results值的HTML格式?

2 个答案:

答案 0 :(得分:2)

您正在使用print_r输出带索引的数组,以及浏览器完美显示结果的原因。我认为您在插入查询中使用变量$ results,以及它失败的原因它包含一个数组。尝试这样的事情: 将表结构更改为

jackson.data (my_id, photos, title,date,location,post)

并将insert语句放入foreach循环并相应地插入值。

实施例

 foreach($html->find('div.postDisplay') as $post) {
                $item['date']     = $post->find('p.id.post-date', 0)->plaintext;
                $item['location']    = $post->find('p.id.post-location', 0)->plaintext;
                $title = $item['title']     = $post->find('h1.id.post-title', 0)->plaintext;
                $item['post'] = $post->find('div.post', 0)->plaintext;
                $query=mysqli_query($db_connect,"INSERT INTO jackson.data (
    my_id, photos, title,date,location,post) VALUES (
    '$my_id', '$photos', '$item['title'],$item['date'],.....)");
            }

对于html格式:

做这样的事情:

 echo "<html><body>";

 foreach($html->find('div.postDisplay') as $post) {
                    $item['date']     = $post->find('p.id.post-date', 0)->plaintext;
                    $item['location']    = $post->find('p.id.post-location', 0)->plaintext;
                    $title = $item['title']     = $post->find('h1.id.post-title', 0)->plaintext;
                    $item['post'] = $post->find('div.post', 0)->plaintext;
                    $query=mysqli_query($db_connect,"INSERT INTO jackson.data (
        my_id, photos, title,date,location,post) VALUES (
        '$my_id', '$photos', '$item['title'],$item['date'],.....)");

                 echo "<div class=\"my_post\"><h1>".$item['title']."</h1>"."<br />Published:". $item['date']."<br />".$item['location']."<br /><br />".$item['post']."</div>";
}
echo "</body></html>";

在你的CSS中你可以有这样的东西:

.my_post
{
  margin:0 auto;//centers the contents
   font-weight:bold;
   font:fontname;
   font-size:16px;
   color:brown;
   padding-top:15px;//Adjusts the gap between two posts;


}

答案 1 :(得分:1)

你可以使用

 "<pre>".print_r($result,true)."</pre>" 

存储在db中以显示类似于浏览器的html输出