坑爹的php代码

refine代码不仅仅是写代码,整体业务逻辑简化,结构上的简化能带来10倍的代码简化,能让client做的事情千万别让serve做。

譬如下面的代码,column实际上是category(分类)的含义,实际上客户端能将分类和子分类取出来,就能简化N多服务器sql了:

<?php
// 服务器端处理
function get_column_video2($array)
{
    extract($array);
    $column_id = isset($column_id) ? (int) $column_id : 0;
    $start = isset($start) ? (int) $start : 0;
    $pagesize = isset($pagesize) ? (int) $pagesize : 10;
    $status = isset($status) ? (int) $status : '';
    $uid = isset($uid) ? (int) $uid : '';
    $date = isset($date) ? $date : '';
    $search_text = isset($search_text) ? $search_text : '';
    $updatemin = isset($updatemin) ? (int) $updatemin : '';
    $updatemax = isset($updatemax) ? (int) $updatemax : '';
    $sort = isset($sort) ? $sort : '';
    $order = isset($order) ? $order == 'asc' ? 'asc' : 'desc'  : 'desc';

    $where_str = '';
    if ($status) {
        $where_str .= " and status = '$status'";
    } else {
        $where_str .= " and status >= 7 ";
    }

    if ($uid) {
        $where_str .= " and creator_id = '$uid'";
    }

    //某一天的数据
    if ($date) {
        $timestamp = strtotime($date);
        $endtime = 24 * 3600 + $timestamp;
        $where_str .= " and ( v.create_time >= $timestamp and  v.create_time < $endtime) ";
    } elseif ($updatemin || $updatemax) {
        if ($updatemin && $updatemax) {
            $where_str .= " and ( v.update_time >= $updatemin and  v.update_time < $updatemax) ";
        } elseif ($updatemin) {
            $where_str .= " and v.update_time >= $updatemin ";
        } else {
            $where_str .= " and v.update_time <= $updatemax ";
        }
    }

    $where_like = '';
    if ($search_text) {
        $search_text = mysql_real_escape_string($search_text);
        $where_like .= "or title like '%" . $search_text . "%' ";
        $where_like .= "or synopsis like '%" . $search_text . "%' ";
        $where_like .= "or key_words like '%" . $search_text . "%' ";
        $where_str .= 'and (' . trim($where_like, 'or') . ')';
    }

    if (in_array($sort, array('create_time', 'update_time', 'airtime'))) {
        $orderBy = " order by v.$sort $order ";
    } else {
        $orderBy = ' order by v.id ' . $order;
    }

    $limit = "limit $start , $pagesize";
    $website_id = $this->session->userdata('website_id');
    if($website_id){
        $website_videos_sql = "(select v.* from vms_video_metadata as v inner join vms_column";
        $website_videos_sql .= " as c on (v.column_id = c.id and c.website_id = '$website_id' ))";
    }

    if ($column_id) {
        if ($column_id !== -1) {
            $column = $this->db->get_where('column', array('id' => $column_id))->row();
            if (isset($column->column_level) && !$column->column_level) {
                $sql = "select v.*,u.user_name,c1.column_level , c1.column_name as column_name3 ,c2.column_name as column_name2,c3.column_name as column_name1
                    from $website_videos_sql as v inner join vms_column as c on (c.root_id = '$column_id' and c.id = v.column_id )
                    left join vms_column as c1 on (c1.id = v.column_id) left join vms_column as c2 on (c2.id = c1.parent_id)
                    left join vms_column as c3 on (c3.id = c2.parent_id)
                    left join vms_user as u on (u.id = v.creator_id)  where  v.is_removed = 0 $where_str $orderBy  $limit ";

                $count = $this->db->query("select count(*) as count  from $website_videos_sql as v
                    inner join vms_column as c on (c.root_id = '$column_id' and c.id = v.column_id )
                    where  v.is_removed = 0 $where_str ")->row()->count;
            } else {
                $sql = "select  v.*,u.user_name,c1.column_level , c1.column_name as column_name3 ,c2.column_name as column_name2,c3.column_name as column_name1  from $website_videos_sql as v
                    inner join vms_column as c on (c.id = v.column_id) left join vms_column as c1 on (c1.id = v.column_id)
                    left join vms_column as c2 on (c2.id = c1.parent_id)  left join vms_column as c3 on (c3.id = c2.parent_id)
                    left join vms_user as u on (u.id = v.creator_id)
                    where  (c.id = '$column_id' or c.parent_id = '$column_id') and  v.is_removed = 0 $where_str $orderBy $limit ";

                $count = $this->db->query("select count(*) as count ".
                    "from $website_videos_sql as v inner join vms_column as c on (c.id = v.column_id) ".
                    "where (c.id = '$column_id' or c.parent_id = '$column_id') and v.is_removed = 0 $where_str "
                )->row()->count;
            }
        } else {
            $sql = "select  v.*,u.user_name   from $website_videos_sql as v
                left join vms_user as u on (u.id = v.creator_id)
                where v.column_id = '0' and v.is_removed = 0 $where_str $orderBy  $limit ";
            $count = $this->db->query("select count(*) as count ".
                "from $website_videos_sql as v where v.column_id = '0' and".
                " v.is_removed = 0 $where_str "
            )->row()->count;
        }
    } else {
        $sql = "select  v.*,u.user_name,c1.column_level , c1.column_name as column_name3 ,c2.column_name as column_name2,c3.column_name as column_name1 ".
            "from $website_videos_sql as v left join vms_column as c1 on (c1.id = v.column_id) left join ".
            "vms_column as c2 on (c2.id = c1.parent_id)   left join vms_column as c3 on (c3.id = c2.parent_id) left join vms_user as u on (u.id = v.creator_id) ".
            "where  v.is_removed = 0 $where_str $orderBy  $limit ";

        $count = $this->db->query("select  count(*) as count from $website_videos_sql ".
            "as v left join vms_column as c on (c.id = v.column_id)  where  v.is_removed = 0 $where_str "
        )->row()->count;
    }

    if ($rs = $this->db->query($sql)->result_array()) {
        $data['rs'] = $rs;
        $data['count'] = $count;
    }
    return isset($data) ? $data : array(
        'rs' => '',
        'count' => 0
    );
}

改为:

<?php
// 改变结构,客户端负责部分逻辑
function get_video_list($category_id, $page_start, $page_size, $sort, $order) {
    if (!in_array($order, array('asc', 'desc'))) {
        $res = Error_Utils::parse(Error_Utils::PARAMETER_INVLIAD_ORDER);
        return $res;
    }

    if (!$category_id || (count(explode(',', $category_id)) == 0)) {
        $res = Error_Utils::parse(Error_Utils::PARAMETER_INVLIAD_CATEGORY_IDS);
        return $res;
    }

    $order_by = " order by v.id $order";
    if ($sort && in_array($sort, array('create_time', 'update_time', 'airtime'))) {
        $order_by = " order by v.$sort $order";
    }

    $v_is_not_remove = "v.is_removed = ".NOT_REMOVE;
    $status_str = "status >= ".NOT_RELEASE;
    $limit = "limit $page_start , $page_size";

    $from_where = " from vms_video_metadata as v, vms_user as u";
    $from_where .= " where u.id = v.creator_id and v.column_id in ($category_id)"
    $from_where .= " and $v_is_not_remove and $status_str";
    $from_where .= " $order_by $limit";

    $video_list_sql = "select v.*, u.user_name $from_where";
    $video_count_sql = "select count(*) as count $from_where";

    $data = array(
        'res' => array(),
        'total' => 0,
        'count' => 0
    );

    $video_list = $this->db->query($video_list_sql)->result_array();
    $video_count = $this->db->query($video_count_sql)->row()->count;
    if (!$video_list) {
        $res = Error_Utils::parse(Error_Utils::SUCCESS, "", $data);
        return $res;
    }

    $data['res'] = $video_list;
    $data['total'] = $video_count;
    $data['count'] = count($data['res']);
    foreach ($data['res'] as $key => $value) {
        $data['res'][$key]['thumbnail_val'] = $data['res'][$key]['thumbnail_url'];
        $data['res'][$key]['thumbnail_url'] = PIC_V($data['res'][$key]['thumbnail_url']);
    }

    $res = Error_Utils::parse(Error_Utils::SUCCESS, "", $data);
    return $res;
}

写烂代码实际上影响极其恶劣,是那个第一个打破窗户的人,不要说“我只多写了一个空格”,实际上你打碎了整栋楼的窗户。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

winlinvip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值