Menampilkan field tabel secara dinamis
Welcome guest, is this your first visit? Create Account now to join.
  • Login:

Welcome to the CHIP Forum.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

+ Reply to Thread
Results 1 to 2 of 2

Thread: Menampilkan field tabel secara dinamis
  
Bookmark and Share

  1. #1
    Join Date
    Feb 2009
    Location
    Bandung
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    Rep Power
    11

    Menampilkan field tabel secara dinamis



    Gan gw kan buat SQL eksekutor di CI, tapi ada kendala yaitu gw belum bisa tampilkan field tabel secara dinamis sesuai query yg diinputkan contoh punya gw sudah didefine duluan field name table nya jadi yg keluar harus 3 field klo tidak error. Berikut source codenya
    [spoiler=controller]
    PHP Code:
    <?php

    class Controller_query extends CI_Controller {

        function 
    __construct() {
            
    parent::__construct();
            
    $this->load->model('model_query');
        }

        function 
    index() {
            
    $eksekusi $this->input->post('eksekusi');
            if (
    $eksekusi == '') {
                
    $eksekusi 'select * from mahasiswa';
            }
            
    //$this->model_query->GetAll($eksekusi);
            
    $data['eksekusi'] = $this->model_query->GetAll($eksekusi);
            
    $this->load->view('view_query'$data);
        }

    }

    ?>
    [/spoiler]
    [spoiler=model]
    PHP Code:
    <?php

    class Model_query extends CI_Model {

        function 
    __construct() {
            
    parent::__construct();
        }

        function 
    GetAll($eksekusi) {
            
    $query $this->db->query($eksekusi);
            if (
    $query->num_rows() > 0) {
                return 
    $query->result();
            } else {
                return array();
            }
        }

    }

    ?>
    [/spoiler]
    [spoiler=view]
    HTML Code:
    <html>
        <title>
            Query SQL
        </title>
    <h1>MySQL eksekutor</h1>
    
    <h3>Input Mahasiswa</h3>
    <table>
        <form action="<?php echo site_url() ?>/controller_query/index" method="POST">
            <tr>            
                <td>Perintah MySQL:</td>
            </tr>
            <tr><td><textarea name="eksekusi" cols="35" rows="5"> </textarea></td></tr>
            <tr><td><input type="submit" value="Coba"/></td></tr>
        </form>
    </table>
    
    <table id="zebra-table" cellspacing="1" width="1000">
        <tr>
            <td>Hasil Eksekusi:</td>
        </tr> 
        <tr><th>NO</th><th>NIM</th><th>NAMA</th></tr>
             <?php
                    foreach ($eksekusi as $row) 
                    {
                         echo "<tr>";
                ?>
                        <td><?php echo $row->no; ?></td>
                        <td><?php echo $row->nim; ?></td>
                        <td><?php echo $row->nama; ?></td>
                        
             <?php  echo "</tr>";   
             }
                    
             ?>
    </table>
    <div id="tnt_pagination">
    </div>
    </html>
    [/spoiler]
    gw coba mengubah php di bawah ini ke OOP PHP yg menggunakan CI di atas, nah source code yg di bawah ini sudah dinamis field name tabelnya.
    [hidden=MySQL eksekutor]
    PHP Code:
    <html>
        <!-- Created on: 7/6/2007 -->
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title></title>
            <meta name="description" content="">
            <meta name="keywords" content="">
            <meta name="author" content="Unregistered User">
            <meta name="generator" content="AceHTML 6 Pro">
        </head>
        <body>
            <?
            $host = "localhost";
            $user = "root";
            $pass = "";
            ?>
            <form action="query.php"  method="post">
                Pilih databsae untuk query: <br>
                <select name="data" size="1">
                    <?
                    mysql_connect($host, $user, $pass);
                    $tabel = mysql_list_dbs();
                    for ($i = 0; $i < mysql_num_rows($tabel); $i++) {
                        echo "<option>" . mysql_table_name($tabel, $i++);
                    }
                    ?>
                </select>
                Perintah MySQL: <br>
                <textarea name="eksekusi" cols="35" rows="5"> </textarea>
                <input type="submit" value="coba">



                </select>

            </form> 
            <?
            if (isset($_POST['eksekusi'])) {

                $host = "localhost";
                $user = "root";
                $pass = "";
                $eksekusi = $_POST['eksekusi'];
                mysql_connect($host, $user, $pass);
                mysql_select_db($_POST['data']);
    //$eksekusi=stripslashes($eksekusi);
                $hasil = mysql_query($eksekusi);
                echo "<B> Hasil Eksekusi:</b> $eksekusi <br>";
                if ($hasil == 0) {
                    echo "Jancok!" . mysql_errno() . ":" . mysql_error() . "<br>";
                } else {
                    echo "<table border=1> <thead> <tr>";
                    for ($i = 0; $i < mysql_num_fields($hasil); $i++) {
                        echo "<th>" . mysql_field_name($hasil, $i) . "</th>";
                    }
                    echo "</tr> </thead> <tbody>";
                    for ($i = 0; $i < mysql_num_rows($hasil); $i++) {
                        echo "<tr>";
                        $baris_arai = mysql_fetch_row($hasil);
                        for ($j = 0; $j < mysql_num_fields($hasil); $j++) {
                            echo "<td>" . $baris_arai[$j] . "</td>";
                        }
                        echo "</tr>";
                    }
                }

                echo "<tbody> </table>";
                echo "<form action=query.php method=post> <input type=submit value=ulang> </form>";
            }
            ?>
        </body>
    </html>
    [/hidden]
    mohon bantuannya.
    Z4ck

  2. #2
    Join Date
    Jan 2005
    Posts
    908
    Thanks
    0
    Thanked 9 Times in 8 Posts
    Rep Power
    21

    Re: Menampilkan field tabel secara dinamis



    @TS,
    cek user guide CI untuk database dan DBUtil
    semua ada kok,

    cheers,
    Last edited by positive+; 26-12-2011 at 10:16.
    " A man's felicity consists not in the outward and visible blessing of fortune, but in the inward and unseen perfections and riches of the mind "

    thomas carlyle

    where i used to go when surfing :
    site1 site2 site3 ...



 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts