codeigniter 为什么我不能在PHP的select中得到我的值

mkh04yzy  于 2022-12-06  发布在  PHP
关注(0)|答案(1)|浏览(82)

i'm practicing on how to use the select in the php, but why i can't display my value in my select box here in php? i'm using a codeigniter framework and here is my folder structure

  • controllers
  • Category.php
  • models
  • Model_category.php
  • views
  • category
  • index.php

the code in the index.php was

<div class="form-group">
        <label for="active">From Category:</label>
        <select class="form-control" id="active" name="active">
            <?php foreach ($category as $k => $v): ?>
                  <option value="<?php echo $v['id'] ?>"><?php echo $v['name'] ?></option>
            <?php endforeach ?>
        </select>
      </div>

and then i have this in controllers in Category.php

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

    $this->not_logged_in();
    
    $this->data['page_title'] = 'Category';
    $this->load->model('model_category');
}

public function index()
    {
        $this->render_template('category/index', $this->data);
        $category['getCategoryData'] = $this->model_category >getCategoryData();
        //print_r($category);die;
        //$this->load->view('category/index', $category);
    }

my table name is "category" and it's row is id,name,active
i tried this

$category = $this->model_category->getCategoryData();$this->load- >view('category/index', $category);

and it's working when i print using this

print_r($category);die;

output:

Array ( [getCategoryData] => Array ( [0] => Array ( [id] => 3 
[name] => Adds On [active] => 1 ) [1] => Array ( [id] => 2 [name] 
=> Meals [active] => 1 ) [2] => Array ( [id] => 1 [name] => 
Drinks [active] => 1 ) ) )

but i still can't display it in the select box, when i add the load view it will return like this DataTables warning: table id=manageTable - Cannot reinitialise DataTable.
my portion of code in ajax in the category.php:

$(document).ready(function() {
  $("#FileMainNav").addClass('active');
  $('#categoryMainNav').addClass('active');
  // initialize the datatable 
  manageTable = $('#manageTable').DataTable({
    'ajax': base_url + 'category/fetchCategoryData',
    'order': []
  });

  // submit the create from 
  $("#createForm").unbind('submit').on('submit', function() {
    var form = $(this);

    // remove the text-danger
    $(".text-danger").remove();

    $.ajax({
      url: form.attr('action'),
      type: form.attr('method'),
      data: form.serialize(), // /converting the form data into array and sending it to server
      dataType: 'json',
      success:function(response) {

        manageTable.ajax.reload(null, false); 

        if(response.success === true) {
          $("#messages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
            '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
            '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.messages+
          '</div>');
ttp71kqs

ttp71kqs1#

我想这个例子可以帮助你:(代码点火器版本3.1.13)
产品型号:

class Category_model extends CI_Model
{
  public function all()
   {
    $query = $this->db->get('category');
    return $query->result();
   }
 }

控制器:

class Category extends CI_Controller {

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

    public function index()
    {
            $data['category'] = $this->category_model->all();
            $this->load->view('category/index', $data);
    }

 }

查看方式:

<div class="form-group">
    <label for="active">From Category:</label>
    <select class="form-control" id="active" name="active">
        <?php foreach ($category as $items): ?>
              <option value="<?php echo $items->id ?>"><?php echo  $items->name ?></option>
        <?php endforeach ?>
    </select>
  </div>

最新消息:
要在Codeigniter上使用Datatables,您可以创建如下方法:Source

public function get_json($aTable = NULL, $aColumns = NULL)
{

    // Read dataTables POST variables
    $iDraw = $this->input->post('draw');
    $iColumns = $this->input->post('columns');
    $iOrder = $this->input->post('order');
    $iStart = $this->input->post('start');
    $iLength = $this->input->post('length');
    $iSearch = $this->input->post('search');

    // Total rows in the table
    $recordsTotal = $this->db->count_all($aTable);

    // Filtering
    // NOTE: This does not match the built-on DataTables filtering which does it
    // word by word on any field. It's possible to do here, but concerned about efficiency
    // on very large tables.
    $recordsFiltered = $recordsTotal;

    if (isset($iSearch) && $iSearch['value'] != '') {
        for ($i = 0; $i < count($aColumns); $i++) {
            $this->db->or_like($aColumns[$i], $iSearch['value']);
        }

        // Saves number of records that matches the query and filters
        $recordsFiltered = $this->db->count_all_results($aTable, false);
    }

    if (isset($iSearch) && $iSearch['value'] != '') {
        for ($i = 0; $i < count($aColumns); $i++) {
            $this->db->or_like($aColumns[$i], $iSearch['value']);
        }
    }

    // Write the SELECT portion of the query
    $this->db->select(implode(',', $aColumns));

    // Ordering
    if (isset($iOrder)) {
        for ($i = 0; $i < count($iOrder); $i++) {
            $this->db->order_by($aColumns[$iOrder[0]['column']], strtoupper($iOrder[0]['dir']));
        }
    } else {
        $this->db->order_by($aColumns[0], 'ASC');
    }

    // Paging
    if (isset($iStart) && $iLength != '-1') {
        $this->db->limit($iLength, $iStart);
    } else {
        $this->db->limit(25, 1);
    }

    // Select Data
    $Data = $this->db->get($aTable);

    // JSON enconding
    $json = json_encode(array(
            "draw" => isset($iDraw) ? $iDraw : 1,
            "recordsTotal" => $recordsTotal,
            "recordsFiltered" => $recordsFiltered,
            "data" => $Data->result())
    );

    return $json;
}

相关问题