0

有:

我想创建具有 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>

需要:

4

2 回答 2

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; 
}
于 2013-06-02T21:39:34.213 回答
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

于 2013-06-03T05:20:56.987 回答