在CodeIgniter中隐藏$ this-> table-> generate()中生成的表格列

时间:2016-09-09 02:28:40

标签: php mysql codeigniter

现在已经在codeigniter工作了一段时间。

我正在从数据库生成数据,并使用$this->table->generate(records);

在表格中组织视图数据

它会生成应该是什么,但如果我想隐藏在查询中选择的id该怎么办?因为您在$this->db->select();中选择的任何内容也会显示在表格中。

如何隐藏id或阻止它在视图中的表格中显示?

提前致谢。

我的模特是:

function clist($search) {
        $config["per_page"] = 10;
        $where = "fn LIKE '%%".$search."%%' OR mn LIKE '%%".$search."%%' OR ln LIKE '%%".$search."%%' OR suffix LIKE '%%".$search."%%' OR address_common LIKE '%%".$search."%%' OR address_brgy LIKE '%%".$search."%%' OR address_city_prov LIKE '%%".$search."%%' OR con_num LIKE '%%".$search."%%' OR date(date_created) LIKE '%%".$search."%%'";

        $this->db->select('id, CONCAT(fn," ",mn," ",ln," ",suffix) AS name,CONCAT(address_common,", ",address_brgy,", ",address_city_prov) AS address,con_num', FALSE);
        $this->db->select("DATE_FORMAT(date_created, '%M %d, %Y - %T') AS date_created", FALSE);

        if (!empty($search)) {
            $this->db->where($where);
        }
        $this->db->order_by('id', 'ASC');
        $rows = $this->db->get('tbl_cust_info', $config['per_page'], $this->uri->segment(3))->result_array();

        $index = $this->uri->segment(3) + 1;

        foreach ($rows as $count => $row) {
            array_unshift($rows[$count], $index.'.');
            $index = $index + 1;

            $rows[$count]['name'] = anchor(base_url().'raffle/clist_details/?i='.$row['id'],$row['name']);
        } return $rows;
    }

我的控制器是:

public function clist() {
        $data["title"] = "Customers' List";

        $this->load->library('table');

        $data["records"] = $this->raffle_model->clist($search);

        $data['thead'] = $this->table->set_heading('#','Name<br /><small>(First Name Middle Name Last Name Suffix )</small>','Address <br /><small>(House #, Bldg. #, Compound, Street, Avenue etc., Barangay, City / Province)</small>','Contact No<br />&nbsp','Date Created<br />&nbsp');
        $data['ttamplate'] =  $this->table->set_template(
            array(
                    'table_open' => 
                        '
                            <div class="table-responsive">
                                <table class="table table-hover table-bordered table-striped table-condensed">
                                    <caption>
                                        <h3><strong>
                                            Customers
                                        </strong></h3>
                                        <small>Below are the list of customers.</small>
                                    </caption>
                        '
                ));

        $data['pagelinks'] = $this->pagination->create_links();

        $this->load->view("templates/header", $data);
        $this->load->view("templates/nav", $data);
        $this->load->view("pages/cust_list", $data);
        $this->load->view("templates/footer", $data);
    }

我的观点是:

<div class="row">
    <?php
        if(empty($records)){
        echo '<br /><br /><br /><div class="alert alert-danger" role="alert" style="text-align: center;"><strong>< < < No Customer found. > > ></strong></div>';
      }else{
          $thead;
          $ttemplate;
          echo $this->table->generate($records);
        }
      ?>
  </div>

2 个答案:

答案 0 :(得分:1)

假设您的所有数据都来自$ records数组,它就是这样的

$this->table->generate(array) // all the array columns will be strucured as table cells.
$records = [
    array("name"=>'sid',age=>21), 
    array("name"=>'sid',age=>21), 
    array("name"=>'sid',age=>21), 
];

您不想显示年龄栏。你可以创建像

这样的新数组
  $displayableRecords = array();
  foreach($records as $record)
    $displayableRecords = $record['name'];

并传递新数组以生成表。

答案 1 :(得分:1)

终于明白了,谢谢@siddhesh。

使用以下代码解决我的问题。 array让我很难过。 希望有人会发现这也很有用。

<div class="row">
    <?php
        if(empty($records)){
        echo '<br /><br /><br /><div class="alert alert-danger" role="alert" style="text-align: center;"><strong>< < < No Customer found. > > ></strong></div>';
      }else{
          foreach ($records as $record) { 
            $displayable_records = 
              array(
                '0' => $record['0'],
                'name' => $record['name'],
                'address' => $record['address'],
                'con_num' => $record['con_num'],
                'date_created' => $record['date_created']
              );
              $this->table->add_row($displayable_records);
          }
          echo $this->table->generate();
        }
      ?>
  </div>