使用codeigniter在表单上显示每个数据

时间:2013-11-13 05:21:26

标签: php mysql codeigniter

我是CodeIgniter的新手并且正在探索它。我很难做到这一点。到目前为止,这是我尝试过的。

Controller.php这样

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customer_controller extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('customer_model');
    }
    public function index()
    {
        $data['title']= 'Customer';
        $data['records'] = $this->customer_model->getAll();
        $data['groups'] = $this->customer_model->getAllCustomerGroups();
        $data['groupcodes'] = $this->customer_model->getAllCustomerGroups();
        $this->load->view('include/header',$data);
        $this->load->view('include/navbar',$data);
        $this->load->view('customer_view', $data);
        $this->load->view('include/sidebar',$data);
        $this->load->view('include/footer',$data);

    }
}

Model.php

 function getAll() {
            $query = $this->db->get('customercard');
            return $query->row_array();       
        }

View.php

              <form class="form-horizontal" role="form">
          <div class="form-group">
            <label for="inputCustomerCode3" class="col-sm-2 control-label">Code</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputCustomerCode3" readonly>
            </div>

            <label for="inputWebsite3" class="col-sm-2 control-label">Website</label>
            <div class="col-sm-4">
              <input type="url" class="form-control" id="inputWebsite3" placeholder="Website">
            </div>
          </div>
          <div class="form-group">
            <label for="inputCustomerName3" class="col-sm-2 control-label">Name</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputCustomerName3" placeholder="Customer Name">
            </div>

            <label for="inputContact3" class="col-sm-2 control-label">Contact</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputContact3" placeholder="Contact">
            </div>
          </div>
          <div class="form-group">
            <label for="inputCustomerGroup3" class="col-sm-2 control-label">Group</label>


            <label for="inputIncomeAccount3" class="col-sm-2 control-label">Income Account</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputIncomeAccount3" placeholder="Income Account">
            </div>
          </div>
          <div class="form-group">
            <label for="inputCustomerType3" class="col-sm-2 control-label">Type</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputCustomerType3" placeholder="Customer Type">
            </div>
            <label for="inputSalesPerson3" class="col-sm-2 control-label">Sales Person</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputSalesPerson3" placeholder="Sales Person">
            </div>
          </div>
          <div class="form-group">
            <label for="inputCustomerAddress3" class="col-sm-2 control-label">Address</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputCustomerAddress3" placeholder="Customer Address">
            </div>

            <label for="inputCreditLimit3" class="col-sm-2 control-label">Credit Limit</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputCreditLimit3" placeholder="Credit Limit">
            </div>
          </div>
          <div class="form-group">
            <label for="inputCustomerAddressAlt3" class="col-sm-2 control-label">AddressAlt</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputCustomerAddressAlt3" placeholder="Customer AddressAlt">
            </div>

            <label for="inputTin3" class="col-sm-2 control-label">Tin</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputTin3" placeholder="Tin">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPhone13" class="col-sm-2 control-label">Phone1</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputPhone13" placeholder="Phone1">
            </div>

            <label for="inputTaxCode3" class="col-sm-2 control-label">Tax Code</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputTaxCode3" placeholder="Tax Code">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPhone23" class="col-sm-2 control-label">Phone2</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputPhone23" placeholder="Phone2">
            </div>

            <label for="inputTerms3" class="col-sm-2 control-label">Terms</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputTerms3" placeholder="Terms">
            </div>
          </div>
          <div class="form-group">
            <label for="inputFax3" class="col-sm-2 control-label">Fax</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputFax3" placeholder="Fax">
            </div>

            <label for="inputPriceCode3" class="col-sm-2 control-label">Price Code</label>
            <div class="col-sm-4">
              <input type="text" class="form-control" id="inputPriceCode3" placeholder="Price Code">
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
            <div class="col-sm-4">
              <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
            </div>
          </div>

          <div class="form-group">
            <div class="col-sm-offset-6 col-sm-10">
                <button type="button" class="btn btn-primary">
                  <span class="glyphicon glyphicon-backward"></span>
                </button>
                <button type="button" class="btn btn-primary">
                  <span class="glyphicon glyphicon-arrow-left"></span> 
                </button>
                <button type="button" class="btn btn-primary">
                  <span class="glyphicon glyphicon-arrow-right"></span>  
                </button>
                <button type="button" class="btn btn-primary">
                  <span class="glyphicon glyphicon-forward"></span> 
                </button>
            </div>
          </div>

          <div class="form-group">
            <div class="col-sm-offset-4 col-sm-10">
                <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Add</button>
                <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span> Update</button>
                <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
                <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-remove"></span> Delete</button>
                <button type="reset" class="btn btn-primary"><span class="glyphicon glyphicon-refresh"></span> Cancel</button>
            </div>
          </div>
        </form>

我想要做的是将数据库中的数据显示到我的表单中。我选择了

  $query = $this->db->get('customercard');

产生

  SELECT * FROM customercard

但是我想在表单上显示每条记录,然后当你注意到我有导航按钮时,第一个,上一个,下一个和最后一个。我想使用这些按钮遍历记录。我不知道如何在视图中显示它。所以我要求你给我一些教程来开始或帮助我使用代码,这样我就可以继续我正在做的事情。帮助真的很感激。先谢谢你们!

1 个答案:

答案 0 :(得分:1)

将模型更改为

function getAll() {
            $query = $this->db->get('customercard');
            return $query->result_array();       
        }

另请注意,当您将$data['records']传递给视图时,您只需在视图中$records即可访问该视频。

在您看来,只需循环播放。

<?php
foreach ($recordsas $value)
  {
  echo "$value <br>";
  }
?>

-----------您可以尝试以下方法。 这是根据您的需求获取下一条记录的简单方法。

MODEL

public function fetch_customers($limit, $start) {
    $this->db->select('customercode,customername,customergroup,customertype,customeraddress,website');
    $this->db->limit($limit);
    $this->db->where('customercode >',$start);
    $this->db->order_by('customercode');
    $query = $this->db->get('customercard');
    if ($query->num_rows() > 0) {
        $row = $query->row();
        $data[] = $row;
        return $data;
    } else {
        return false;
     }
}

CONTROLLER

public function index(){
    $shown_id = 0;
    if(isset($this->uri->segment(3)){
        $shown_id = $this->uri->segment(3);
    }
    $this->load->model('customers_model');
    $data['info'] = $this->customers_model->fetch_customers(1,$shown_id);
    $this->load->view('customer_info',$data);
}

查看

$customer_id = $info->customercode;
$customer_name = $info->customername;
//Use like above to get the customer info and show as you like

但是,当您进行“下一步”按钮时,您需要将$ customer_id传递回控制器。 您也可以使用超链接执行此操作。

<a href="<?php echo base_url();?>controller_name/index/<?php echo $customer_id;?>"

或使用onclickwindow.location

的按钮