2016-02-25 59 views
-1

我想要合并$output$output1然后填充HTML表格。如何合并2个数组并在表格中打印?

这是我的代码:

<?php 
$link = parse_ini_file(__DIR__ . '/config.ini', true); 
include("connect.php"); 



$output = ''; 
$cnn = simplexml_load_file($link['cnn']); 
$bbc = simplexml_load_file($link['bbc']); 

    foreach($cnn->channel->item as $item){ 



     $title = $item->title; 
     $description = $item->description; 
     $url = $item->link; 
     $pubDate = $item->pubDate; 
     $title1 = str_replace("'","\'", $title); 
     $description1 = str_replace("'","\'", $description); 



$output[]['title'] = $title; 
$output[]['description'] = $description; 
$output[]['url'] = $url; 
$output[]['p_date'] = $pubDate; 



      $sql = mysql_query("INSERT IGNORE INTO tbl_daily_news_headlines (
       title, 
       description, 
       url, 
       pub_date, 
       log_date) 
      VALUES (
       '$title1', 
       '$description1', 
       '$url', 
       '$pubDate', 
       now())") 
      or die(mysql_error()); 

     } 

       foreach ($bbc->channel->item as $bitem){ 

       $bbtitle = $bitem->title; 
       $bbdescription = $bitem->description; 
       $bburl = $bitem->link; 
       $bbpubDate = $bitem->pubDate; 
       $bbtitle1 = str_replace("'", "\'", $bbtitle); 
       $bbdescription1 = str_replace("'", "\'", $bbdescription); 

$output1[]['title'] = $bbtitle; 
$output1[]['description'] = $bbdescription; 
$output1[]['url'] = $bburl; 
$output1[]['p_date'] = $bbpubDate;  


    $sql = mysql_query("INSERT IGNORE INTO tbl_daily_news_headlines(
     title, 
     description, 
     url, 
     pub_date, 
     log_date) 
    VALUES( 
     '$bbtitle1', 
     '$bbdescription1', 
     '$bburl', 
     '$bbpubDate',now())") 
    or die(mysql_error()); 

$final_output = array_merge($output, $output1); 


      } 


?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0"> 
    <title>Feed Example</title> 
    <link rel="stylesheet" type="text/css" href="media/css/bootstrap.min.css"> 
    <link rel="stylesheet" type="text/css" href="media/css/dataTables.bootstrap.css"> 


    <script type="text/javascript" language="javascript" src="media/js/jquery-1.12.0.min.js"> 
    </script> 
    <script type="text/javascript" language="javascript" src="media/js/jquery.dataTables.js"> 
    </script> 
    <script type="text/javascript" language="javascript" src="media/js/dataTables.bootstrap.js"> 
    </script> 
    <script type="text/javascript" language="javascript" class="init"> 

$(document).ready(function() { 
    $('#example').DataTable(); 
}); 

    </script> 
</head> 
<body class="dt-example dt-example-bootstrap"> 
    <div class="container"> 
     <section> 
      <h1>FEED Test</h1> 
      <div class="info"> 
      </div> 
      <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
       <thead> 
        <tr> 
         <th>Title</th> 
         <th>Description</th> 
         <th>URL</th> 
         <th>Publish Date</th> 

        </tr> 
       </thead> 
       <tfoot> 
        <tr> 
         <th>Title</th> 
         <th>Description</th> 
         <th>URL</th> 
         <th>Publish Date</th> 
        </tr> 
       </tfoot> 
       <tbody> 
      <? foreach($final_output as $data1) 
      { 
      echo '<td>'.$data1['title'].$data1['description'].$data1['url'].$data1['p_date'].'</td>'; 

      } 


     ?> 
       </tbody> 
      </table> 

      </div> 
    </section> 
</body> 
</html> 

我试图array_merge()但它不工作。

我也试过:

$data = $output + $output1; 

如果我错了,我是不是放错了我的循环?

编辑: 所以之后我尝试array_merge($输出,$输出1);它只是输出数组的只是最后一个元素,第二个方法是相同的

我只是尝试做一些内部的foreach像

  <? foreach($output as $data) 
       { 
       foreach($output1 as $data1) 
        { 
        echo '<td>'.$data.$data1'</td>'; 
        } 
       } 
     ?> 

,但我得到500错误,所以任何想法?

编辑2: 感谢帕特里克我合并这2个输出,但仍然不能让印上表中,如果你现在的打印他们的水平(在1个泳道一切),如何让固定看我更新的代码?

enter image description here

+0

“它不是WO “什么方式不起作用?你期望的结果是什么?实际结果是什么? –

+0

检查更新后的文章 –

+0

就错误而言,您在'echo'行的'$ data1'后面缺少'.'。 –

回答

1

你之所以只得到一个元素是因为每次通过foreach循环覆盖以前的值。您需要每次在您的$output$output1阵列中创建一个新条目。

而不是

$output['title'] = $title; 
    $output['description'] = $description; 
    $output['url'] = $url; 
    $output['p_date'] = $pubDate; 

$output1['title'] = $bbtitle; 
    $output1['description'] = $bbdescription; 
    $output1['url'] = $bburl; 
    $output1['p_date'] = $bbpubDate;  

你应该有

$output[]['title'] = $title; 
    $output[]['description'] = $description; 
    $output[]['url'] = $url; 
    $output[]['p_date'] = $pubDate; 

$output1[]['title'] = $bbtitle; 
    $output1[]['description'] = $bbdescription; 
    $output1[]['url'] = $bburl; 
    $output1[]['p_date'] = $bbpubDate;  
+0

谢谢,你能检查更新的问题? –

+0

不要我改变脚本中的一些逻辑,并使其工作,谢谢。 –

0

的数据表,你希望它在某种JSON格式。以下是我会做它用,你有什么就有什么,赶紧(未经测试)

<?php 

$final_output = array_merge($output, $output1); 

和HTML/JavaScript的

<script type="text/javascript"> 
    var php_data = <?php echo json_encode($final_output); ?> 

    $("#example").DataTable({ 
     data: php_data 
    }); 

</script> 

<table id="example"> 
    <thead> 
     <tr> 
      <th>Title</th> 
      <th>Description</th> 
      <th>URL</th> 
      <th>Publish Date</th> 
     <tr> 
    </thead> 
</table> 

然而,一个更漂亮的方式做到这一点是通过AJAX。检查文档出来的数据表:https://www.datatables.net/examples/ajax/

和,如果你不使用AJAX你怎么可能格式化数据的示例:

https://www.datatables.net/examples/data_sources/js_array.html

+0

谢谢,我已经试过这个,但合并后,我只有最后一个元素的数组,因此,这是替代的原因寻找 –