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]
[/spoiler]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=model]
[/spoiler]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=view]
[/spoiler]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>
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]
[/hidden]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>
mohon bantuannya.






Reply With Quote

Bookmarks