双tr数组

时间:2013-06-02 21:01:50

标签: php html mysql arrays

HAVE:

我想创建表,其中1行(tr)像树的标题,其他行是cicle数组,其中父id =第一行(标题);但是查询的结果构建了cicle header1-content1 header1-contet2(并且将是header1-content1-content2-content3)

文件中的PHP代码:

$query = "SELECT cafedra.*, facultet.f_name FROM cafedra,facultet WHERE facultet_id = facultet.id";
$row = $db->select($query);
$cafedra = "";
foreach($row as $r) {    
$result = "<tr class='info'>
               <td colspan='5'>$r[f_name]</td>
           </tr>
           <tr>
               <td class='span1' > $r[code] </td>
               <td style='text-align:left;'> $r[c_name]</td>
               <td><a id='view-$r[id]' href='/facultet/$r[id]' class='btn btn-mini btn-success'><i class='icon-desktop icon-large'></i></a></td>
               <td><button id='edit-$r[id]' class='btn btn-mini btn-warning' ><i class='icon-pencil icon-large'></i></button></td>
               <td><button id='delete-$r[id]' class='btn btn-mini btn-danger' ><i class='icon-trash icon-large'></i></button></td>
           </tr>";
$cafedra .= $result;   
 }

文件中的HTML代码:

<br/>
<h1>  </h1>
<br/><br/>
<div class="row-fluid">
<div id="facultet" class="span9 offset1">
   <table class="table table-hover">
    <tbody>

<?php echo $cafedra; ?>

   </tbody>
  </table>
</div>
</div>

需要:

2 个答案:

答案 0 :(得分:0)

有几种方法可以实现这一目标,但最简单的方法之一就是订购数据。

首先,您必须按照自己喜欢的方式订购数据:

SELECT cafedra.*, facultet.f_name FROM cafedra,facultet WHERE facultet_id = facultet.id ORDER BY facultet.f_name ASC ;

然后在PHP中排序:

$data = array() ;

foreach($row as $key => $value){
  $f_name = $value['f_name'] ;
  if (!isset($data[$f_name]){
    $data[$f_name] = array() ;
  }
  $data[$f_name][] = $value ;
}

那个输出:

foreach($data as $header => $rows){
  $result = "<tr class='info'>
               <td colspan='5'>$r[f_name]</td>
           </tr> " ;
  foreach($rows as $row){
    $result .= "
           <tr>
               <td class='span1' > $r[code] </td>
               <td style='text-align:left;'> $r[c_name]</td>
               <td><a id='view-$r[id]' href='/facultet/$r[id]' class='btn btn-mini btn-success'><i class='icon-desktop icon-large'></i></a></td>
               <td><button id='edit-$r[id]' class='btn btn-mini btn-warning' ><i class='icon-pencil icon-large'></i></button></td>
               <td><button id='delete-$r[id]' class='btn btn-mini btn-danger' ><i class='icon-trash icon-large'></i></button></td>
           </tr>";
  }
$cafedra .= $result; 
}

答案 1 :(得分:0)

谢谢,解决了:

<?php

$cafedra = "";
$query = "SELECT facultet.* FROM  facultet ORDER BY facultet.id ASC";
$row = $db->select($query);
foreach($row as $r){
  $result = "<tr class='info'>
               <td colspan='5'>$r[f_name]</td>
           </tr>" ;

  $query2 = "SELECT cafedra.* FROM cafedra WHERE facultet_id = $r[id]";
  $row2 = $db->select($query2);
  foreach($row2 as $r2){
    $result .= "
           <tr>
               <td class='span1' > $r2[code] </td>
               <td style='text-align:left;'> $r2[c_name]</td>
               <td><a id='view-$r2[id]' href='/facultet/$r[id]' class='btn btn-mini btn-success'><i class='icon-desktop icon-large'></i></a></td>
               <td><button id='edit-$r2[id]' class='btn btn-mini btn-warning' ><i class='icon-pencil icon-large'></i></button></td>
               <td><button id='delete-$r2[id]' class='btn btn-mini btn-danger' ><i class='icon-trash icon-large'></i></button></td>
           </tr>";
  }
$cafedra .= $result; 
}

?>

<br/>
<h1>  </h1>
<br/><br/>
<div class="row-fluid">
<div id="facultet" class="span9 offset1">
   <table class="table table-hover">
    <tbody>

<?php echo $cafedra; ?>

   </tbody>
  </table>
</div>
</div>

_http://i.stack.imgur.com/paV5j.png