使用PHP表省区和分区

时间:2015-11-25 02:55:30

标签: php html mysql

我的数据库中有省区和分区表,我想在html表中打印输出如下:

Output i want

但我明白了:

I get this

这是我的代码:

columns: [{
  field: "respuestasPosibles",
  template: function(e){
    if ( (e.tipoPregunta === 'E') && $.isArray(e.respuestasPosibles) ){
      var st = '';
      $.each(e.respuestasPosibles, function(i, a){
        st += a.respuestaPosible + '(' + a.idRespuestaPosible + ')<br>';
      });
      return st;
    }
    else{
      // The cell will be empty
      return '-';
    }
  }, ....
]

请帮助我,

非常感谢。

2 个答案:

答案 0 :(得分:0)

您需要关闭然后以foreach ($QueryDistricts as $QueryDistrict )的每个循环打开一个新行。试试这样的事情 -

<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
  <td> <?php echo $orderProv; ?></td>
  <td> <?php echo $QueryProvince->nameProv; ?></td>
  <?php 
  //$QueryDistricts = Query District;

  $i = 0; // simple counter

  foreach ($QueryDistricts as $QueryDistrict ) {

      if($i>0){ // if not the 1st row, echo new row start
  ?>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>    
  <?php 
      }
  ?>
  <td> <?php echo $QueryDistrict ->nameDist; ?></td>
</tr>
  <?php
   $i++;
  }
}
?>

答案 1 :(得分:0)

表格结果:

enter image description here

html源代码: enter image description here

完整代码:

<table id="example1" class="table table-bordered table-condensed table-hover">
    <thead>
        <tr>
            <th>No.</th>
            <th width="20%">Provinsi</th>
            <th>Kabupaten/Kotamadya</th>
            <th>Kecamatan</th>
            <th>Pendamping</th>
            <th>Telepon</th>
            <th>Email</th>
            <th>Status Laporan</th>
        </tr>
    </thead>
    <tbody>
    <?php
        $thecolor = array ("olive", "navy", "aqua", "maroon", "green", "yellow", "orange", "purple", "light-blue", "red");

        //$infLokasis = Query;
        $nomorProv = 1;

            $thecolorProv = $thecolor;
            $colorProv = 0;
            foreach ($infLokasis as $infLokasi) {
                # code... cetak nama provinsi
                if ($colorProv >= 10) {
                    # code... warna set
                    $colorProv = 0;
                }


    ?>
        <tr>
          <td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $nomorProv.". ";?></strong></td>
          <td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $infLokasi->lokasi_nama;?></strong></td>
    <?php 

          $i = 0; // simple counter

            //$infKabkotas = Query;
            $nomorKabkota = 1;
            $thecolorKabkota = $thecolor;
            $colorKabkota = 0;

            foreach ($infKabkotas as $infKabkota) {
                # code... cetak nama kabupaten
                if ($colorKabkota >= 10) {
                    # code... warna set
                    $colorKabkota = 0;
                }

                if($i>0){ // if not the 1st row, echo new row start
    ?>
        <tr>
          <td></td>
          <td></td>    
    <?php 
                }
    ?>
          <td><strong class="text-<?php echo $thecolorKabkota[$colorKabkota];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$infKabkota->lokasi_nama; ?></span></td>
    <?php
        $j = 0;

        $infKecamatans = Query;
        $nomorKecamatan = 1;
        $thecolorKecamatan = $thecolor;
        $colorKecamatan = 0;

        foreach ($infKecamatans as $infKecamatan) {
            if ($colorKecamatan >= 10) {
                # code... warna set
                $colorKecamatan = 0;
            }         

            if($j>0){ // if not the 1st row, echo new row start

    ?>
        <tr>
          <td></td>
          <td></td>    
          <td></td>                                                  
    <?php 
            }
    ?>
          <td><strong class="text-<?php echo $thecolorKecamatan[$colorKecamatan];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$nomorKecamatan.". ".$infKecamatan->lokasi_nama; ?></strong></td>
    <?php
        $k = 0;
        $idDaerah = substr($infKecamatan->lokasi_kode, 0,10);

        $infPendampings = $db->get_results("SELECT id_p, nama_p, telepon_p, email_p FROM pendamping WHERE id_d LIKE '$idDaerah%' ");
        $nomorPendamping = 1;
        $thecolorPendamping = $thecolor;
        $colorPendamping = 0;


        if ($infPendampings) {

            foreach ($infPendampings as $infPendamping) {

                if($k>0){ // if not the 1st row, echo new row start
    ?>
        <tr>
          <td></td>
          <td></td>    
          <td></td>
          <td></td>    
    <?php 
                }  
    ?>

          <td>
                <?php echo "<span class=\"text-".$thecolorPendamping[$colorPendamping]." small\">".$nomorPendamping.". "; ?></span>
                <?php echo "<a href=\"vpendamping.php?str=".encryptor('encrypt', $infPendamping->id_p)."\" class=\"text-".$thecolorPendamping[$colorPendamping]." small\"  target=\"_blank\">".namaGelar($infPendamping->nama_p); ?>
          </td>
          <td class="small"><?php echo $infPendamping->telepon_p; ?></td>
          <td class="small"><?php echo $infPendamping->email_p; ?></td>
          <td class="small"><?php echo "<i class=\"fa fa-fw fa-square-o text-red\"></i> Kosong"; ?></td>
    <?php 
            $nomorPendamping++;
            $colorPendamping++;
            $k++;
            }

        }
        else {

            # code... row kosong
            #if i romove 4 line code below... Provinsi name and Kabupaten/Kotamadya name should appear
            echo "<td></td>\n";
            echo "<td></td>\n";    
            echo "<td></td>\n";
            echo "<td></td>\n";                                                    
        }

    ?>
    </tr> 
    <?php    

        $nomorKecamatan++;
        $colorKecamatan++;
        $j++;
        }
    ?>
    <?php    
                $i++;
                $colorKabkota++;
                $nomorKabkota++;
            } // End infKabkota

            $nomorProv++;
            $colorProv++;
        }
    ?>
    </tbody>
</table>