2013-08-06 26 views
-2

我试图从2个sql语句中拉出两个结果并打印到HTML表格中。但是有些结果如何不正确,请帮我解决。在HTML表格中打印多个结果集

以下是第二个结果不是打印正确的结果 - 重复的结果。

<?php 
global $wpdb; 
$result1=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%java%'); 
$result2=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%oracle%'); 
?> 
<table id="table_id"> 
    <thead> 
     <tr> 
      <th>JAVA</th> 
      <th>ORACLE</th> 
     </tr> 
    </thead> 
<tbody> 
<?php foreach($result1 as $rows1) { ?> 
<?php foreach($result2 as $rows2) { ?> 
      <tr> 
       <td> <?php echo $rows1->post_name ; ?> </td> 
       <td> <?php echo $rows2->post_name ; ?> </td>    
</tr> 
    <?php 
    } 
?> 
<?php 
    } 
?> 

+1

为什么你在第一个里面写第二个foreach? – 2013-08-06 10:11:05

+1

在发布这个问题之前,你甚至还读过自己的代码吗? –

+0

看起来尽可能的糟糕。为什么你需要2个查询完全相同的模式?也许你想使用OR子句。然而,你应该调试你的代码甚至是语法错误,因为你有,所以不需要嵌套foreaches的逻辑错误 –

回答

2
 global $wpdb; 
     $result1=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%java%'"); 

     $result2=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%oracle%'"); 

获取到对象之后,我们把所需的值在两个阵列,使我们可以创建一个多维数组。

 $result_one = array(); 
     $result_two = array(); 

    //putting values in array 

     foreach ($result1 as $result) { 
     $result_one[] = $result->post_name; 
     } 

     foreach ($result2 as $result) { 
     $result_two[] = $result->post_name; 
     } 

    //Creating a multi-dimensional array so that we can print two columns easily 

     $results = array(
     $result_one, 
     $result_two, 
     ); 

    //Initialising the variables to be used as counters and array indexes. 

    $i = 0; $j = 0; $k = 0; $p = 1; 

    //getting length of array 

    $array_length = count($result_one); 

现在启动两个循环以从创建的多维数组中获取数据。 在这种情况下:要创建表正确,我们需要在这个订单价值:

$结果[0] [0],$结果[1] [0],$结果[0] [1] $ results [1] [1], $ results [0] [2],$ results [1] [2]等等。

所以我们必须相应地创建循环。

while ($i < 2) { 
     while ($j < $array_length) { 

    if (fmod($p,2)) echo '<tr>'; // So that <tr> will be printed only after two <td> 

     echo "<td>". $results[$i][$j]. "</td>"; 

     if($i == 0) { $i = 1; } else { $i =0; } // Toggling values of $i from 0 to 1. 
     $k++; 
     if(fmod($k, 2) == 0){ $j ++; } // Increasing $j after 2 steps. 

     $p++; 

     if (fmod($p,2)) echo '</tr>'; 

     } 

    $i ++; 

    } 

如果您需要三列:

重复步骤一个二,三,这样就可以创建一个这样的数组:

$results = array(
      $result_one, 
      $result_two, 
      $result_three, 
      );  

更改第一while循环

while ($i < 3) 

并将以下语句更改为:

if (fmod($p,3)) echo '<tr>'; 
if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; } 
if(fmod($k, 3) == 0){ $j ++; } 
if (fmod($p,3)) echo '</tr>'; 

我希望您现在能够更好地理解,并且可以自己做出所需的更改。

这看起来很复杂。但这应该工作。请尝试。 任何人都可以随意编辑while循环,使其更简单。

+0

如何在html表中调用这个,因为我需要第二列的第二个结果集。感谢 – Hansa

+0

更新了答案。请检查。 –

+0

谢谢Sandesh,我已经测试过,但它在一列中打印,我需要第一个像条件一列,第二个像第二列。 – Hansa

0

我错过了第三个结果array.please纠正我。

 $results = array(
     $result_one, 
     $result_two, 
     $result_three, 
     ); 

    $i = 0; $j = 0; $m = 0; $k = 0; $p = 1; 

    $array_length = count($result_one); 
?> 
<table id="table_id"> 
    <thead> 
     <tr> 
      <th>ORACLE</th> 
      <th>JAVA</th> 
      <th>SAP</th> 
     </tr> 
    </thead> 
<tbody> 
<?php while ($i < 3) { 
     while ($j < $array_length) { 
    if (fmod($p,3)) echo '<tr>'; 

     echo "<td>" . $results[$i][$j] . "</td>"; 

     if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; } 
     $k++; 
     if(fmod($k, 3) == 0){ $j ++; } 

     $p++; 

     if (fmod($p,3)) echo '</tr>'; 

} 
    $i ++; 

    } 
    ?> 
+0

http://pastebin.com/QXzaVLRA。还要确保你有正确创建的$ result_three数组 –