具有WHERE子句过滤的codeigniter模型函数结果数组

时间:2018-06-04 12:39:09

标签: php codeigniter activerecord mysqli

我的模型中已经有了一个工作功能

//GETTING TOTAL PURCHASE
function total_portal_user_sale($user_id)
{
    $return = 0;
    $transection  = $this->db->get('transection')->result_array();
    foreach ($transection as $row) {
        if ($row['buyer'] == $user_id) {
            $return += $row['credit'];
        }
    }
    return $return;
}

我正确地从我的数据库中获取数据,但我需要通过where子句过滤结果,例如此查询

$this->db->query("SELECT sum(credit) as total FROM transection 
where buyer = $user_id and status like '%paid%'");

这是相同的情况但是使用数据过滤,我不知道如何使用我的查询与活动记录语句

如果有人改写我的

function total_portal_user_sale($ user_id) 使用我的WHERE子句查询我会很感激...

这就是我如何将数据呈现到视图文件中的表

<div class="panel-body" id="demo_s">
<table id="demo-table" class="table table-striped" data-pagination="true" data-show-refresh="false" data-ignorecol="0,6" data-show-toggle="false" data-show-columns="false" data-search="true" data-striped="true" data-filter-control="true" data-show-export="true" >
    <thead>
        <tr>
            <th style="width:4ex"><input type="checkbox" id="users_idtog"/></th>
            <th><?php echo translate('no');?></th>
            <th><?php echo translate('image');?></th>
            <th><?php echo translate('name');?></th>
            <th><?php echo translate('email');?></th>
            <th><?php echo translate('phone');?></th>
            <th>Total<br>Sale</th>
            <th>This<br>Month<br>Sale</th>
            <th>Total Debit</th>
            <th>Total<br>Profit</th>
            <th><?php echo translate('creation');?></th>
            <th>Monthly<br>Sale<br>Target</th>
            <th>Coupon<br>Code</th>
            <th>Coupon<br>Expiry<br>Date</th>
            <th class="text-right"><?php echo translate('options');?></th>
        </tr>
    </thead>                
    <tbody >
    <?php
        $i = 0;
        foreach($all_users as $row){
            $i++;
    ?>                
    <tr>
                <td><input type="checkbox" class="users_id" name="users_id[]" value="<?php echo $row['user_id']; ?>" /></td>

        <td><?php echo $i; ?></td>
        <td>
            <img class="img-sm img-circle img-border"
                <?php if(file_exists('uploads/user_image/user_'.$row['user_id'].'.jpg')){ ?>
                    src="<?php echo base_url(); ?>uploads/user_image/user_<?php echo $row['user_id']; ?>.jpg"
                <?php } else if($row['fb_id'] !== ''){ ?>
                    src="https://graph.facebook.com/<?php echo $row['fb_id']; ?>/picture?type=large" data-im='fb'
                <?php } else if($row['g_id'] !== ''){ ?>
                    src="<?php echo $row['g_photo']; ?>"
                <?php } else { ?>
                    src="<?php echo base_url(); ?>uploads/user_image/default.png"
                <?php } ?>  />
        </td>
        <td><?php echo $row['username']; ?></td>
                                <td><a href="mailto:<?php echo $row['email']; ?>" target="_self" ><?php echo $row['email']; ?></td>
        <td><?php echo $row['phone']; ?></td>

       <td class="text-right"><?php echo $this->crud_model->total_purchase($row['user_id']); ?></td>
        <td class="text-right">
        <?php 
                $Days = explode("-",date("d-m-Y"));
                echo round($this->crud_model->sale_target($Days[0],$row['user_id']),2);
        ?>          
        </td>
        <td class="text-right"><?php echo $this->crud_model->total_profit($row['user_id']); ?></td>
        <?php 
        $ts = $this->crud_model->total_portal_user_sale($row['user_id']);
        $tc = $this->crud_model->total_portal_user_profit($row['user_id']);
        $tp = $ts - $tc
        ?>
        <td class="text-right">
            <?php echo $ts; ?>
        </td>

        <td class="text-right"><?php echo date('d M,Y',$row['creation_date']);?></td>
        <td><?php echo $row['monthly_sale_target']; ?></td>
        <td><?php echo $row['coupon_code']; ?></td>
        <td><?php echo $row['expiry_date']; ?></td>
        <td class="text-right">

            <a class="btn btn-purple btn-xs btn-labeled fa fa-tag" data-toggle="tooltip"
                onclick="ajax_modal('add_discount','<?php echo translate('give_target_discount'); ?>','<?php echo translate('adding_discount!'); ?>','add_discount','<?php echo $row['user_id']; ?>')" data-original-title="Edit" data-container="body">
                    <?php echo translate('give_discount');?>
            </a>

            <a class="btn btn-success btn-xs btn-labeled fa fa-wrench" data-toggle="tooltip" 

                    onclick="ajax_modal('edit','<?php echo translate('edit_user'); ?>','<?php echo translate('successfully_edited!'); ?>','user_edit','<?php echo $row['user_id']; ?>')" 

                        data-original-title="Edit" data-container="body">

                            <?php echo translate('edit');?>

                </a>

            <a onclick="delete_confirm('<?php echo $row['user_id']; ?>','<?php echo translate('really_want_to_delete_this?'); ?>')" class="btn btn-xs btn-danger btn-labeled fa fa-trash" data-toggle="tooltip" 
                data-original-title="Delete" data-container="body">
                    <?php echo translate('delete');?>
            </a>
        </td>
    </tr>
    <?php
        }
    ?>
    </tbody>
</table>
</div>

1 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

function total_portal_user_sale($user_id) 
{
   $return = 0;
   $this->db->select('*');
   //$this->db->select('sum(credit) as total,buyer');
   $this->db->from('transection');
   $this->db->where('buyer',$user_id);
   $this->db->like('status','paid');

   $query = $this->db->get();
   if ($query->num_rows() > 0 ) 
   {
      foreach ($query->result_array() as $row) 
      {
        if ($row['buyer'] == $user_id) 
        {
          $return += $row['credit'];
        }
      }
     return $return;
   }
}

您也可以将$this->db->select_max('credit')用于相同的

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html