使用codeigniter计算数据库中的行数

时间:2016-05-11 07:50:28

标签: php mysql 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>
&#13;
&#13;
&#13;

我想计算来自表格excel的行,其中&#39; request_id&#39; =东西.. 我在控制器中做了foreach里面的count_all_result,但它给了我:

&#13;
&#13;
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)
&#13;
&#13;
&#13;

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

2 个答案:

答案 0 :(得分:0)

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

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);

答案 1 :(得分:0)

试试这段代码:

daten  = read.csv(file="test.csv",head=TRUE,sep=",")
trash <- grep("Blank", daten[,3])
daten <- daten[-c(trash),]
iAv_temp <- grep("Intensity average" , daten[,2])
iAv <- daten[c(iAv_temp),]
iAv_name <- iAv[,3]
iAv <- iAv[,4]
iC_temp <- grep("Concentration average" , daten[,2])
iC <- daten[c(iC_temp),]
iC_name <-iC[,3]
iC <- iC[,4]
i=1
x=6
j=1
leng=length(iC)
leng_t=leng/6
iC_MW <- c(1:leng_t)

while (j<=leng_t)
{ 
  iC_name_short<-iC_name[i]  
  temp <- iC[i:x]
  iC_MW[j]<-median(temp)
  i<-i+6
  x<-x+6
  j<-j+1
}