untuk langkah-langkahnya, silahkan anda buat dua table dengan struktur table sebagai berikut:
table : member
table : hobby
Kemudian, silahkan isi data ke table hobby!, berikut adalah contoh isi table hobby:
Setelah itu, silahkan anda buat model dengan nama Member.php. berikut kodenya:
| PHP | | copy code | | ? |
| 01 | <?php |
| 02 | //lokasi file protected/models |
| 03 | class Member extends CActiveRecord { |
| 04 | |
| 05 | public static function model($className = __CLASS__) { |
| 06 | return parent::model($className); |
| 07 | } |
| 08 | |
| 09 | public function tableName() { |
| 10 | return 'member'; |
| 11 | } |
| 12 | |
| 13 | public function rules() { |
| 14 | |
| 15 | return array( array('nama, alamat, hobby', 'required'), ); |
| 16 | } |
| 17 | |
| 18 | public function attributeLabels() { |
| 19 | return array('id' => 'ID', 'nama' => 'Nama', 'alamat' => 'Alamat', 'hobby' => 'Hobby', ); |
| 20 | } |
| 21 | |
| 22 | } |
| 23 | ?> |
| PHP | | copy code | | ? |
| 01 | <?php |
| 02 | |
| 03 | class Hobby extends CActiveRecord { |
| 04 | |
| 05 | public static function model($className = __CLASS__) { |
| 06 | return parent::model($className); |
| 07 | } |
| 08 | |
| 09 | public function tableName() { |
| 10 | return 'hobby'; |
| 11 | } |
| 12 | |
| 13 | public function rules() { |
| 14 | |
| 15 | return array( array('hobby', 'required'), ); |
| 16 | } |
| 17 | |
| 18 | public function attributeLabels() { |
| 19 | return array('id' => 'ID', 'hobby' => 'Hobby', ); |
| 20 | } |
| 21 | |
| 22 | } |
| 23 | ?> |
| PHP | | copy code | | ? |
| 01 | <?php |
| 02 | |
| 03 | //lokasi file protected/controllers |
| 04 | class TipmanymanyController extends Controller{ |
| 05 | |
| 06 | //untuk menentukan layout default pada controller |
| 07 | public $layout='Null'; |
| 08 | |
| 09 | public function actionCreatemember(){ |
| 10 | |
| 11 | if($_POST){ |
| 12 | |
| 13 | //ambil data hobby |
| 14 | $hobby=$_POST['hobby']; |
| 15 | |
| 16 | //ambil data hobby dengan operator ternary; |
| 17 | $idhobby=empty($hobby) ? array(0) : $hobby; |
| 18 | |
| 19 | //declarasi model member :) |
| 20 | $ModelMember = new Member; |
| 21 | |
| 22 | //set ke field nama |
| 23 | $ModelMember->nama = $_POST['nama']; |
| 24 | |
| 25 | //set ke field alamat |
| 26 | $ModelMember->alamat= $_POST['alamat']; |
| 27 | |
| 28 | //set ke field hobby. (implode digunakan untuk merubah array jadi string dengan separator comma) |
| 29 | //dan disinilah proses data array disimpan didalam satu field di tabel member yaitu hobby |
| 30 | $ModelMember->hobby =implode(", ", $idhobby); |
| 31 | if($ModelMember->save()){ |
| 32 | $this->redirect('index'); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | //untuk mendapatkan data hobby dari table hobby |
| 37 | $dataHobby = Hobby::model()->findAll(); |
| 38 | $this->render('createmember', |
| 39 | array( |
| 40 | 'hobbies'=>$dataHobby, |
| 41 | ) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | //function untuk menampilkan data member beserta hobbynya |
| 46 | public function actionIndex(){ |
| 47 | |
| 48 | $DataMember = Member::model()->findAll(); |
| 49 | $this->render('index', |
| 50 | array( |
| 51 | 'Members'=>$DataMember, |
| 52 | ) |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | ?> |
createmember.php
| PHP | | copy code | | ? |
| 01 | <form action="" method="post"> |
| 02 | <table border="0"> |
| 03 | <tr> |
| 04 | <td>Nama</td> |
| 05 | <td>:</td> |
| 06 | <td><input type="text" name="nama" /></td> |
| 07 | </tr> |
| 08 | <tr> |
| 09 | <td>Alamat</td> |
| 10 | <td>:</td> |
| 11 | <td><textarea name="alamat" cols="50" rows="3"><garismiringtextarea></td> |
| 12 | </tr> |
| 13 | <tr> |
| 14 | <td>Hobby</td> |
| 15 | <td>:</td> |
| 16 | <td> |
| 17 | <!--ini digunakan untuk menampilkan checkbox dengan mengambil data dari tabel hobby--> |
| 18 | <?php |
| 19 | foreach($hobbies as $dhobby):?> |
| 20 | <input type="checkbox" name="hobby[]" value="<?php echo $dhobby->id;?>" /><?php echo $dhobby->hobby;?> |
| 21 | <?php endforeach;?> |
| 22 | </td> |
| 23 | </tr> |
| 24 | <tr> |
| 25 | <td> </td> |
| 26 | <td> </td> |
| 27 | <td><input type="submit" value="Simpan" /></td> |
| 28 | </tr> |
| 29 | </table> |
| 30 | </form> |
index.php
| PHP | | copy code | | ? |
| 01 | <style> |
| 02 | table { |
| 03 | border-collapse: collapse; |
| 04 | } |
| 05 | |
| 06 | </style> |
| 07 | <a href="<?php echo $this -> createUrl('tipmanymany/createmember');?>">Tambah member baru</a> |
| 08 | <table border="1" width="500px"> |
| 09 | <tr> |
| 10 | <th>Nama</th> |
| 11 | <th>Alamat</th> |
| 12 | <th>Hobby</th> |
| 13 | </tr> |
| 14 | <?php |
| 15 | foreach($Members as $member): |
| 16 | /*SELECT data dari table hobby dengan menggunakan WHERE IN :)*/ |
| 17 | $idhobby = $member -> hobby; |
| 18 | $criteria = new CDbCriteria( array( |
| 19 | //spesifik field |
| 20 | 'select' => 'hobby', |
| 21 | //where in kondisi |
| 22 | 'condition' => "id IN ($idhobby)", |
| 23 | //order by |
| 24 | 'order' => 'id DESC', )); |
| 25 | //end |
| 26 | |
| 27 | ?> |
| 28 | <tr> |
| 29 | <td valign="top"><?php echo $member -> nama;?></td> |
| 30 | <td valign="top"><?php echo $member -> alamat;?></td> |
| 31 | <!--disinilah proses untuk menampilkan data hobby--> |
| 32 | <td valign="top"><?php |
| 33 | $dataHobby = Hobby::model() -> findAll($criteria); |
| 34 | $i = 1; |
| 35 | foreach ($dataHobby as $hobby) : |
| 36 | echo $i . '. ' . $hobby -> hobby . '<br>'; |
| 37 | $i++; |
| 38 | endforeach; |
| 39 | ?></td> |
| 40 | <!--end hobby--> |
| 41 | </tr> |
| 42 | <?php endforeach;?> |
| 43 | </table> |
dan berikut ini adalah contoh isi table membernya setelah disimpan:
Demikianlah tutorial kali ini semoga bermanfaat bagi anda semua..
Download script lengkapnya:
Models/Member.php
Models/Hobby.php
Controllers/TipmanymanyController.php
Views/tipmanymany/createmember.php
Views/tipmanymany/index.php
Sumber:http://www.shariveweb.com/2013/10/09/input-data-array-ke-satu-field-untuk-select-where-in-study-kasus.html
0 Response to "Input data array ke satu field untuk select where in (study kasus) "
Post a Comment