如何将 php 脚本转换为代码点火器 MVC


How to convert php script to codeigniter MVC?

我有PHP脚本和工作:

<?php
    $con=mysqli_connect("localhost","root","","dbm");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result3 = mysqli_query($con,"select t.tgl_transaksi, p.kode_produk, tp.hp_tujuan,   d.nama as nama2, sr.ket as ket2  from dbm.transaksi_psa tp
    left join dbm.transaksi t
    on tp.id_transaksi=t.id_transaksi
    left join dbm.produk p
    on tp.kode_produk = p.kode_produk
    left join dbm.set_report sr on t.status=sr.jenis
    left join dbm.distributor d on (tp.id_distributor=d.id_distributor and tp.com=d.com)
    where date(t.tgl_transaksi)=date(now())
    order by t.tgl_transaksi DESC");
    echo "
    <div class='table-responsive'>
        <table class='table table-hover' id='t01'>
            <tr>
                <th>Tanggal</th>
                <th>Produk</th>
                <th>Tujuan</th>
                <th>Suplier</th>
               <th>Status</th>
           </tr>";
    while ($row = mysqli_fetch_array($result3))
    {
        echo "<tr>";
        echo "<td>" . $row['tgl_transaksi'] . "</td>";
        echo "<td>" . $row['kode_produk'] . "</td>";
        echo "<td>" . $row['hp_tujuan'] . "</td>";
        echo "<td>" . $row['nama2'] . "</td>";
        echo "<td>" . $row['ket2'] . "</td>";
        echo "</tr>";
    }
    echo "</table></div> <br><br><br>";
    mysqli_close($con);
?>

我尝试更改为代码点火器 MVC - OOP:模型:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Ikhtisar_model extends CI_Model{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    //read the ikhtisar list from db
    public function get_all()
    {
        $this->db->select('t.tgl_transaksi AS tgl, p.kode_produk AS kode, tp.hp_tujuan AS nohp, d.nama AS nama2, sr.ket AS ket2', FALSE)
        ->from('transaksi_pulsa tp')
        //$this->db->where('date(t.tgl_transaksi)', 'date(now())');
        ->join('transaksi t','tp.id_transaksi=t.id_transaksi', 'left')
        ->join('produk p','tp.kode_produk = p.kode_produk', 'left')
        ->join('set_report sr','t.status=sr.jenis', 'left')
        ->join('distributor d','tp.id_distributor=d.id_distributor and tp.com=d.com', 'left')
        ->order_by("t.tgl_transaksi", "DESC");
        $ikhtisar = $this->db->get();
        //->result_array();
        //return $ikhtisar;     
     }
}

控制器:

<?php
    if ( ! defined('BASEPATH')) exit('Akses di blok. Cek kembali link yang Anda tuju!');
    class ikhtisar extends CI_Controller {
        public function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->database();
        }
        public function index()
        {
            //load the ikhtisar_model
            $this->load->model('ikhtisar_model');  
            //call the model function to get the ikhtisar data
            $ikhresult = $this->ikhtisar_model->get_all();           
            $data['ikhlist'] = $ikhresult;
            //load the ikhtisar_view
            $this->load->view('templates/header', $data);
            $this->load->view('ikhtisar_view',$data);
            $this->load->view('templates/footer', $data);
        }
    }

视图:

<!-- BEGIN CONTENT -->
    <div class="page-content-wrapper">
        <div class="page-content">
            <div class="row">
                <div class="col-md-12 col-sm-12">
                    <table class="table table-striped table-hover">
                        <thead>
                        <tr>
                            <th>Tanggal</th>
                            <th>Produk</th>
                            <th>Tujuan</th>
                            <th>Suplier</th>
                            <th>Status</th>
                        </tr>
                        </thead>
                        <tbody>
                        <? foreach($ikhtisar->result_array() as $ikh): ?>
                            <tr>
                                <td><?=$ikhlist['tgl']?></td>
                                <td><?=$ikhlist['kode']?></td>
                                <td><?=$ikhlist['nohp']?></td>
                                <td><?=$ikhlist['nama2']?></td>
                                <td><?=$ikhlist['ket2']?></td>
                            </tr>
                        <? endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>  
</div>  

但是,我得到的是,数据根本不显示。页眉(页脚)显示正确。

首先,您必须从模型中返回数据 类 Ikhtisar_model 扩展CI_Model{

 public function get_all()
 {
   .........................
   $ikhtisar = $this->db->get();
   return $ikhtisar;
 }
}

其次,您必须将数据从Contorller传递到您的视图。你已经做到了。

  public function index()
 {
      //load the ikhtisar_model
      $this->load->model('ikhtisar_model');  
      //call the model function to get the ikhtisar data
      $ikhresult = $this->ikhtisar_model->get_all();           
      $data['ikhlist'] = $ikhresult;//you got it and you will get your variable as $ikhlist at your view
      //load the ikhtisar_view
      $this->load->view('templates/header', $data);
      $this->load->view('ikhtisar_view',$data);
      $this->load->view('templates/footer', $data);
 }

现在显示在您的视图中。你写了 foreach 循环 worng。应该是这样的

        <? foreach($ikhlist->result_array() as $ikh): ?>
          <tr>
            <td><?=$ikh['tgl']?></td>
            <td><?=$ikh['kode']?></td>
            <td><?=$ikh['nohp']?></td>
            <td><?=$ikh['nama2']?></td>
            <td><?=$ikh['ket2']?></td>
          </tr>
          <? endforeach; ?>

最好使用完整的 PHP 标签而不是短标签。

<?php而不是<?

<?php echo而不是<?=

谢谢你帮助我,Shaiful Islam先生。我得到的只是这个错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: ikhtisar
Filename: ikhtisar/ikhtisar_view.php
Line Number: 19

Tanggal Kode    No HP   Nama2   Ket2
( ! ) Fatal error: Call to a member function result_array() on a non-object in /var/www/html/cinew/application/views/ikhtisar/ikhtisar_view.php on line 19
Call Stack
#   Time    Memory  Function    Location
1   0.0001  131424  {main}( )   ../index.php:0
2   0.0003  132712  require_once( '/var/www/html/cinew/system/core/CodeIgniter.php' )   ../index.php:202
3   0.0063  340720  call_user_func_array ( )    ../CodeIgniter.php:359
4   0.0063  340852  ikhtisar->index( )  ../CodeIgniter.php:359
5   0.3295  372728  CI_Loader->view( )  ../ikhtisar.php:20
6   0.3295  373004  CI_Loader->_ci_load( )  ../Loader.php:419
7   0.3296  390908  include( '/var/www/html/cinew/application/views/ikhtisar/ikhtisar_view.php' )   ../Loader.php:833

文件上的第 19 行:

<?php foreach($ikhtisar->result_array() as $ikh): ?>

PS:很抱歉使用"发布您的答案"。这真的不是我自己问题的解决方案答案。我不能在注释中使用代码。