count() rows in database using codeigniter


count() rows in database using codeigniter

这是模型(Excel.php):

function undangan($req_id)
    {
        
        $this->db->select("excel_id,request_id,to_name as nama,to_address as alamat_tujuan,to_zipcode as kode_pos, ifnull(tariff,0) as tarif ");
        $this->db->where('tariff = 0 and request_id =', $req_id);
        $query  =   $this->db->get('excel');
        return $query->result_array();
    }

这是控制器(Admin.php):

function undangan()
    {	
    	$this->load->model('excel');
        $this->load->model('request');	
    	$data['results']   =   $this->request->request();       
        foreach ( $data['results'] as $key)
        {
            $tarif=0;
           	$req_id = $key['req'];
            $data['tarif'][$req_id] =   $this->excel->total_tarif($req_id); 
            $data['undg'][$req_id]   =   $this->excel->undangan($req_id);
           	foreach ($data['undg'][$req_id] as $value)
            {
                $tarif += $value['tarif']; 
                $this->db->where('request_id', $value['request_id']);
                $num_rows = $this->db->count_all_results('excel');
                var_dump($num_rows);
            }
           
            $tarif = $data['tarif'][$req_id];
        }
           
        $this->load->view('admin_view',$data);
          
    } 

视图(admin_view.php):

<div class="container">
<h3>Customer Request</h3>
<?php
    foreach ($results as $value) {
  
    ?>
    <div class="panel panel-primary">
        <table class="table" align="center">
       
            <tr class="primary" align="center">
                <td><?php echo $value['nama']?></td>
                <td><?php echo $value['email']?></td>
                <td><?php echo $value['phone']; ?></td>
                <td><?php echo $value['alamat_pengirim']?></td>  
                <td id="total_<?php echo $value['req']?>">
                <span > <?php echo $tarif[$value['req']][0]['tarif']?>
                </td>
                <td>
                <button id="kirim_email" type="button" class="btn btn-success" onclick=update_status(<?php echo $value['req']?>) >
                 <span class="glyphicon glyphicon-envelope" aria-hidden="true" ></span> Kirim Email
                </button>
                </td>
                <td>
                <button  type="button" class="btn btn-success" onclick=tariff(<?php echo $value['req']?>)>
                <span class="glyphicon glyphicon-thumbs-up" aria-hidden="true" ></span> Auto Tarif
                </button>
                </td>
            </tr>
        </table>
    </div>  
    <table class="table table-striped table-bordered" align="center" width="400px" >
        <tr align="center" class="success">
            <th>
                Alamat
            </th>
            <th>
                Zip Code
            </th>
            
            <th>
                Tarif
            </th>
            
            <th>
                Action
            </th>
        </tr>
  
        <?php        
         foreach ($undg[$value['req']] as $row) {        
        ?>
        <tr>
            <td>
            <?php echo $row['alamat_tujuan']?>
            </td>
            <td>
            <input type="text" class="form-control col-md-2"  id="zip_<?php echo $row['excel_id']?>"  value="<?php echo $row['kode_pos']?>">
            </td>
            <td>
            <input type="text" class="form-control col-md-2"  id="<?php echo $row['excel_id']?>"  name="tariff" value="<?php echo $row['tarif']?>">
            </td>
            <td>
            <button  type="button" id="ref_butn" class="btn btn-md" onclick="update(<?php echo $row['excel_id']?> )">
            <span class="glyphicon glyphicon-floppy-save" aria-hidden="true" ></span> Save
            </button>
            </td>
        </tr>
    <?php    } ?>
    </table>
    <?php }; ?>
</div>
</div>
</div>

我想计算excel表中的行数,其中"request_id"=某个值。。我在控制器中对foreach内部进行了count_all_result,但它给了我:

int(3) int(3) int(3) int(3) int(3) int(3) int(2) int(2) int(3) int(3) int(3) int(2) int(2)

这不是实际的行总数。我应该在哪里计算all_result?帮我解决这个问题。。感谢

您可以在选择查询中使用count

$this->db->select("COUNT(request_id) as total, excel_id,request_id,to_name as nama,to_address as alamat_tujuan,to_zipcode as kode_pos, ifnull(tariff,0) as tarif ");
$this->db->where('tariff = 0 and request_id =', $req_id);

或使用

$total = $this->db->count();

试试这个代码:

$u->where('test', $test);
$u->where('test', $test);
$u->get();
$total = count($u->all);