坑爹的php代码
来源:互联网 发布:淘宝数据怎么查 编辑:程序博客网 时间:2024/05/22 01:31
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;}
写烂代码实际上影响极其恶劣,是那个第一个打破窗户的人,不要说“我只多写了一个空格”,实际上你打碎了整栋楼的窗户。
0 0
- 坑爹的php代码
- PHP的搜索代码
- PHP常用的代码
- OOP的PHP代码
- 调试php的代码
- PHP代码的加密
- 使用的php代码
- PHP代码的优化
- 【PHP】代码的优化
- 实用的php代码
- 救命的PHP代码
- php的代码优化
- PHP 编写更高性能的PHP代码
- php 实现php代码的加密解密
- 【PHP】对于PHP某些代码的总结
- PHP 代码延时执行后面的代码
- PHP 调用C的代码
- 一个php代码的日历表
- 【教程】Windows获取开发人员许可证(Windows 应用商店应用)
- 使用Server.MapPath()获取当前路径
- hdu 1890 Robotic Sort(伸展树)
- 冬日,有暖阳在心中
- linux:实用的工具.....持续更新中
- 坑爹的php代码
- jQuery取得select选择的文本与值
- 1818挂Q网 www.1818guaq.tk 免费24小时挂Q
- 【转】Java与C++的区别
- 美丽心灵
- delphi 设置自启动
- shell编程笔记-基本元素
- 苏煜块状链表代码。。
- 反向建索引