如何将这个php普通代码转换为codeigniter mvc

时间:2015-02-25 10:41:21

标签: php codeigniter

我需要帮助才能将此代码转换为codeigniter MVC 我有2个文件PHP 查看的index.php和模型的cari_barang.php

我有文件控制器的问题,我不知道如何用ajax获得价值。

这是我的PHP代码index.php

<html>
<head>
    <script type="text/javascript" src="jquery-1.11.2.js"></script> 
    <script type='text/javascript' src='jquery.autocomplete.js'></script>
    <script type="text/javascript">
$(document).ready(function() {  
    $("#barang").keyup(function() {
        var kode    = $('#barang').val();
        $.ajax({
            type    : "POST",
            data    : "kode="+kode,
            url     : "cari_barang.php",
            dataType: "json",
            success: function(data){
                $("#namabarang").val(data.namabarang); 
                $("#hargabeli").val(data.hargabeli); 
            }
        });
    });

});
</script>
</head>
<body>
  <div class="demo" style="width: 450px;">
  <div>
  <p>Kode Barang : <input type="text" id="barang" value="0001"></p>
  <p>Nama Barang : <input type="text" id="namabarang" size="50" disabled></p>
  <p>Harga Beli : <input type="text" id="hargabeli" size="15" align="right" disabled></p>
  </div>
  </div> 
</body>
</html>

cari_barang.php的这段代码

<?php

mysql_connect("localhost","root","");
mysql_select_db("barang");

$kode   = $_POST['kode'];

$sql    = mysql_query("select * from tbbarang where kodebarang='$kode'");
$row    = mysql_num_rows($sql);

if($row>0){
    $r = mysql_fetch_array($sql);
    $data['namabarang'] = $r['namabarang'];
    $data['hargabeli'] = $r['hargabeli'];
    echo json_encode($data);

}else{
    $data['namabarang'] = '';
    $data['hargabeli'] = '';
    echo json_encode($data);
}
?>

之前感谢

2 个答案:

答案 0 :(得分:2)

查看codeigniter网站http://www.codeigniter.com/userguide3/,希望您能找到您的解决方案。 如果你将上面的代码转换为codeigniter,那么它只是一个codeigniter的视图文件。

答案 1 :(得分:0)

感谢大家的回答,我的问题通过此代码解决了

型号:

public function get($kode)
{

    $this->db->where('kodebarang',$kode);
    $query = $this->db->get('tbbarang');
    if ($query->num_rows() > 0)
    {
       foreach ($query->result() as $row)
       {
          $data['namabarang'] = $row->namabarang;
          $data['hargabeli'] = $row->hargabeli;
          echo json_encode($data);
       }
    } else {
        $data['namabarang'] = '';
        $data['hargabeli'] = '';
        echo json_encode($data);
    }
}

控制器:

public function view()
{
    $this->load->model('m_barang');
    $kode = $this->input->post('kode');
    //@$kode = $_POST['kode'];
    $data = $this->m_barang->get($kode);
}

查看:

<html>
<head>

 <script type='text/javascript' src='<?php echo base_url("jquery-1.11.2.js");?>'></script>
 <script type='text/javascript' src='<?php echo base_url("jquery.autocomplete.js");?>'></script>
<!-- <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<link rel="stylesheet" href="main.css" type="text/css" /> -->
<script type="text/javascript">
$(document).ready(function() {  
    $("#barang").keyup(function() {
        var kode    = $('#barang').val();
        $.ajax({
            type    : "POST",
            data    : "kode="+kode,
            url     : "<?=base_url('barang/view')?>",
            dataType: "json",
            success: function(data){
                $("#namabarang").val(data.namabarang); //$r['namabarang']
                $("#hargabeli").val(data.hargabeli); //$r['hargabeli']
            }
        });
    });

});
</script>
</head>
<body>
  <div class="demo" style="width: 450px;">
  <div>
  <p>Kode Barang : <input type="text" id="barang" value=""></p>
  <p>Nama Barang : <input type="text" id="namabarang" size="50" disabled></p>
  <p>Harga Beli : <input type="text" id="hargabeli" size="15" align="right" disabled></p>
  </div>
  </div> 
  <p class="footer" ><?php echo base_url();?></p>
  <p class="footer" ><?php echo site_url();?></p>
  <p class="footer" ><?php echo base_url('barang/view');?></p>
</body>
</html>