Tolong pilih kategori sesuai, jenis posting (diskusi atau bukan) dan sertakan tag/topik yang sesuai seperti komputer, java, php, mysql, dll. Promosi atau posting tidak pada tempatnya akan kami hapus!
- Bagi Anda yang ingin mendaftar, baca link berikut:
http://diskusiweb.com/discussion/50491/how-to-registrasi-diskusiweb-com-baca-ini-terlebih-dahulu
- Cara menyisipkan kode program supaya tampil rapi dan terformat dengan baik di diskusiweb.com: http://www.diskusiweb.com/discussion/50415/cara-menyisipkan-kode-program-di-diskusiweb-com
- Cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

masalah loop dengan dynamic list

aku ingin hasil output nya seperti ini : http://imgur.com/U0zxCgI

itu aku ambil dari : http://akomodasi.co.id/

itu inputan single : 1, double : 2
aku coba begini :

<?php
    $Sgl = 1;
    $Dbl = 2;
    $Trp = 0;
    $array = array('Single'=>$Sgl, 'Double'=>$Dbl*2, 'Triple'=>$Trp*3);
?>

<table border="1">
    <tr>
        <td>No</td>
        <td>Room Type</td>
        <td>Room Grade</td>
        <td>Gender</td>
        <td>Family Name</td>
        <td>First Name</td>
    </tr>
    <?php
        $no = 1;
        foreach($array as $key=>$val){
       
            for($i=0; $i< $val; $i++) {
                $room_type = $key;
    ?>
                <tr>
                    <td><?php echo $no; ?></td>
                    <td><?php echo $room_type; ?></td>
                    <td>Standart Room</td>
                    <td>
                        <select>
                          <option selected>Mr.</option>
                          <option>Ms</option>
                          <option>Mrs</option>
                          <option>Mstr</option>
                        </select>
                    </td>
                    <td>
                        <input type="text" class="" placeholder="Family Name">
                    </td>
                    <td>
                        <input type="text" class="" placeholder="First Name">
                    </td>
                </tr>
    <?php
           
            }
            $no++;
        }
    ?>

</table>

hasilnya : http://imgur.com/LtNUXkB

hampir sama sih, cuman kurangnya di penomorannya

gimana caranya agar output nya sama persis seperti gambar?

mohon pencerahannya
Tagged:

Comments

  • edited January 2016
    itu mah bukan cuma masalah penomoran
    tapi juga masalah grouping

    <table border="1">
    <?php
    $arr = array(
    array('Jawa Barat','Bandung'),
    array('Jawa Barat','Bogor'),
    array('Jawa Tengah','Semarang'),
    array('Jawa Tengah','Solo'),
    array('Jawa Tengah','Tegal'),
    array('Jawa Timur','Surabaya'),
    array('Jawa Timur','Sidoarjo')
    );

    $prop = '';
    $c = 1;
    foreach($arr as $v)
      echo
        '<tr><td>' .
        ( $prop!=$v[0] ? ($c++).'</td><td>'.($prop=$v[0]) : '</td><td>' ) .
        '</td><td>'.$v[1].'</td></tr>';
    ?>
    </table>
  • edited January 2016
    kalo ndak paham penulisan kaya begitu ...

    <table border="1">
    <?php
    $arr = array(
    array('Jawa Barat','Bandung'),
    array('Jawa Barat','Bogor'),
    array('Jawa Tengah','Semarang'),
    array('Jawa Tengah','Solo'),
    array('Jawa Tengah','Tegal'),
    array('Jawa Timur','Surabaya'),
    array('Jawa Timur','Sidoarjo')
    );

    $prop = '';
    $c = 1;
    foreach($arr as $v) {
      echo '<tr><td>';
      if($prop!=$v[0]) {
        $prop=$v[0];
        echo $c.'</td><td>'.$prop;
        $c++;
      }
      else {
        echo '</td><td>';
      }
      echo '</td><td>'.$v[1].'</td></tr>';
    }
    ?>
    </table>
  • itu beda dengan kasus aku :-?
  • yg bilang siapa ?

    yg dipahami itu logika nya
  • ok, aku coba pahami
  • edited January 2016
    klo begini bisa gk ya??
     <?php
    $no = 1;
    foreach($array as $key=>$val){
     
    for($i=0; $i< $val; $i++) {
    $room_type = $key;
    if($i==0) { echo "<tr><td rowspan=".$val."><".$no."></td><td rowspan=".$val.">".$room_type."</td><td rowspan=".$val.">Standart Room</td>"; }
    else { echo "<tr>"; }
     
    ?>
    <td>
    <select>
    <option selected>Mr.</option>
    <option>Ms</option>
    <option>Mrs</option>
    <option>Mstr</option>
    </select>
    </td>
    <td>
    <input type="text" class="" placeholder="Family Name">
    </td>
    <td>
    <input type="text" class="" placeholder="First Name">
    </td>
    </tr>
    <?php
     
    }
    $no++;
    }
    ?>
  • bukan begitu bro
    coba lihat gambarnya lagi atau coba akses di web nya
  • Kagak ngerti maksoed loe...

    emng data array nya gimana?
  • klo aku coba begini :

    <?php
        $Sgl = 1;
        $Dbl = 2;
        $Twn = 0;
        $Trp = 0;
        $Quad = 0;
    ?>

    <table border="1">
        <tr>
            <td>No</td>
            <td>Room Type</td>
            <td>Room Grade</td>
            <td>Gender</td>
            <td>Family Name</td>
            <td>First Name</td>
        </tr>
        <?php      
            $total = ($Sgl * $Sgl) + ($Dbl * $Dbl) + ($Twn * $Twn) + ($Trp * $Trp) + ($Quad * $Quad);
            for($i=1; $i <= $total; $i++) {
                $room = 'Standart Room';
                if($i == $Sgl && $Sgl == 1){
                    $no = $i;
                    $roomType = 'Single';
                }
                elseif ( ($i%$Dbl) == 0) {
                    if($i == $Dbl){
                        $no = $i;
                        $roomType = 'Double';
                    }
                    else{
                        $no = $i - 1;
                        $roomType = 'Double';
                    }
                }
                else{
                    $no = $room = '';
                    $roomType = '';
                }
        ?>
        <tr>
            <td><?php echo $no;?></td>
            <td><?php echo $roomType;?></td>
            <td><?php echo $room;?></td>
            <td>
                <select>
                  <option selected>Mr.</option>
                  <option>Ms</option>
                  <option>Mrs</option>
                  <option>Mstr</option>
                </select>
            </td>
            <td>
                <input type="text" class="" placeholder="Family Name">
            </td>
            <td>
                <input type="text" class="" placeholder="First Name">
            </td>
        </tr>
        <?php
                $no++;
            }
        ?>

    </table>

    dengan value seperti itu, udah jalan
    cuman belum sempurna

    klo aku ganti variabelnya, tampilannya gak sesuai dengan inputan yg dimasukan

    itu inputannya dinamis
  • edited January 2016
    misal klo inputannya :  $Sgl = 0, $Dbl = 0, $Twn = 0, $Trp = 1, $Quad = 1; hasilnya : http://imgur.com/Th3nQ6g

    contoh lain misal inputannya : $Sgl = 2, $Dbl = 0, $Twn = 2, $Trp = 0, $Quad = 0; hasilnya : http://imgur.com/Q2mUPLa

    contoh lain lagi misal inputannya : $Sgl = 0, $Dbl = 3, $Twn = 0, $Trp = 0, $Quad = 0; hasilnya : http://imgur.com/VEiaj7x
  • edited January 2016
    punya gw jalan gk ya...
    8->
    <table border="1">
    <tr>
    <td>No</td>
    <td>Room Type</td>
    <td>Room Grade</td>
    <td>Gender</td>
    <td>Family Name</td>
    <td>First Name</td>
    </tr>
    <?php
    $type["sgl"] = $_POST["sql"]);
    $type["dbl"] = $_POST["dbl"]);
    $type["trp"] = $_POST["trp"]);
    $type["twn"] = $_POST["twn"]);
    $type["quad"] = $_POST["quad"]);
     
     
    $no=1;
    foreach ($type as $k => $v){
    switch ($k){
    case 'sql' : $loop = 1; $room_type="Single" ;break;
    case 'dbl' : $loop = 2; $room_type="Double" ;break;
    case 'twn' : $loop = 2; $room_type="Twin" ;break;
    case 'trp' : $loop = 3; $room_type="Triple" ;break;
    case 'quad': $loop = 4; $room_type="Quad" ;break;
    default : $loop = 0;break;
    }
     
    for($i=0; $i < $v; $i++) { //loop berdasarkan jumlah inputan user
    for($j=0; $j < $loop; $j++) { //loop berdasarkan room type
    if($j==0) { echo "<tr><td rowspan=".$loop.">".$no."</td><td rowspan=".$loop.">".$room_type.
    "</td><td rowspan=".$loop.">Standart Room</td>"; }
    else { echo "<tr>"; }
     
    ?>
    <td>
    <select>
    <option selected>Mr.</option>
    <option>Ms</option>
    <option>Mrs</option>
    <option>Mstr</option>
    </select>
    </td>
    <td>
    <input type="text" class="" placeholder="Family Name">
    </td>
    <td>
    <input type="text" class="" placeholder="First Name">
    </td>
    </tr>
    <?php
    }
    $no++;
    }
    }
    ?>
    </table>

Sign In or Register to comment.