练习19
来源:互联网 发布:广西来宾广电网络招聘 编辑:程序博客网 时间:2024/05/16 11:33
user.class.php
<?php
/*一个功能,可以分解成很多小的功能,可以一直细分下去,只到没有过分重复的代码。重构,就是把一个小的功能,,拎出来,形成一个独立的服务。 这个函数虽然只有两行,但也确实是提供了一个服务。原则: 重构一方面是重组代码;另一重要方面是设计的的重构*/
class User{ protected $arr;//进行封装不让外部使用,除子类和父类 protected $con; /** *数据库操作错误打印。。。 */ function sql_err($a){ if(!$a){ echo mysql_error(); } } function __construct(){ $this->con =mysql_connect("localhost","root","123456"); if(!$this->con){ echo "11"; } mysql_query("set character set 'utf-8'"); mysql_query("set names utf8"); mysql_select_db("my_user_db"); $this->arr = $this->get_data();//构造函数,在类一实例化的时候就会执行 } function __destruct(){ mysql_close($this->con); } public function isNotExist(){ echo "Does not exist<br/>"; echo "<a href=\"http://localhost/Reconstruction/Reconstruction002/all.php\">重新输入</a>"; }
/** *提供数据源 *把数据库数据变成数组 * @return array */ protected function get_data(){ $b = mysql_query("select * from user"); $this->sql_err($b); $ar = array(); while($c = mysql_fetch_array($b)){ array_push($ar,$c); } return $ar; } /** * 返回经过修改后的数据 *把数组储存至数据库 *@param array $arr数组 * @return array */ protected function save_data($array){ $del = "truncate table user"; $b = mysql_query($del); $this->sql_err($b); foreach($array as $c=>$v){ $name = $v['name']; $age = $v['age']; $sex = $v['sex']; $insert_new = "insert into user (name,age,sex) values ('{$name}','{$age}','{$sex}')"; $b = mysql_query($insert_new); $this->sql_err($b); } } /** * 提供一方法,以表格形式输出数据 * @param array找到的数组 * @return string */ public function joinHtml($array){ if(count($array)==0){ User::isNotExist(); } $d = 0; $b ="<html><body><form><table border=\"1\" cellspacing=\"0\" cellpadding=\"10\">"; foreach($array as $t){ $d = $d+1; $a = "<tr> <td>".$d."</td> <td>".$t['name']."</td> <td>".$t['age']."</td> <td>".$t['sex']."</td> </tr>"; $b = $b.$a; } $b = $b . "</table></form></html>"; return $b; } /** * 返回所有数据 * @return array */ public function get_all(){ $temp= array(); foreach($this->arr as $c=>$v){ array_push($temp,$this->arr[$c]); } return $temp; }
/** * 返回包含指定年龄的数组 * @param int $age年龄 * @return array */ public function get_by_age($age){ $temp = array(); foreach($this->arr as $c=>$v){ if($this->arr[$c]['age']==$age){ array_push($temp,$this->arr[$c]); } } return $temp; } /** * 返回包含指定年龄间的数组 * @param int $age1小年龄 $age2大年龄 * @return array */ public function get_between_ages($age1,$age2){ $temp = array(); foreach($this->arr as $c=>$v){ if($this->arr[$c]['age']>=$age1&&$this->arr[$c]['age']<=$age2){ array_push($temp,$this->arr[$c]); } } return $temp; } /** * 返回包含指定姓名的数组 * @param string $name 姓名 * @return array */ public function get_by_name($name){ $temp = array(); foreach($this->arr as $c=>$v){ if($this->arr[$c]['name']==$name){ array_push($temp,$this->arr[$c]); } } return $temp; } /** * 返回包含指定年龄和性别的数组 * @param string $sex性别 $age年龄 * @param int $age年龄 * @return array */ public function get_both_age_sex($age,$sex){ $temp = array(); foreach($this->arr as $c=>$v){ if($this->arr[$c]['age']==$age&&$this->arr[$c]['sex']==$sex){ array_push($temp,$this->arr[$c]); } } return $temp; } /** * 返回包含指定性别的数据的年龄的总和 * @param string $sex性别 * @return array */ public function get_by_sexs($sex){ $b=0; $temp = array(); foreach($arr as $c=>$v){ if($arr[$c]['sex']==$sex){ $d=$arr[$c]['age']; $b=$b+$d; } } return $b; }
/** * 返回包含指定性别的数组 * @param string $sex性别 * @return array */ public function get_by_sex($sex){ $temp = array(); foreach($this->arr as $c=>$v){ if($this->arr[$c]['sex']==$sex){ array_push($temp,$this->arr[$c]); } } return $temp; }
/** * 返回包含指定姓氏的数组 * @param string $firstname姓氏 * @return array */ public function get_by_firstname($firstname){ $temp = array(); foreach($this->arr as $c=>$v){ $e=$this->arr[$c]['name']; $f=substr($e,0,2); if($f==$firstname){ array_push($temp,$this->arr[$c]); } } return $temp; }
/** * 返回姓氏最多的的数组 * @return array */ public function get_mostname(){ $temp = array(); $arra=array(); $array=array(); $zf=""; foreach($this->arr as $c=>$v){ $e=$this->arr[$c]['name']; $f=substr($e,0,2); $b=$f.$b; } foreach($this->arr as $c=>$v){ $e=$this->arr[$c]['name']; $f=substr($e,0,2); $m=substr_count($b,$f); array_push($arra,$m); }
for($m=0;$m<count($arra);$m++){ for($i=count($arra);$i>0;$i--){ for($j=0;$j<=count($arra);$j++){ if($arra[$i]>$arra[$j]){ unset($arra[$j]); } } } } array_pop($arra); foreach($arra as $c=>$v){ array_push($array,$c); } //var_dump($arra); foreach($array as $c=>$v){ array_push($temp,$this->arr[$c]); } return $temp; } /** * 返回包含指定字符的数组 * @param string $word字符 * @return array */ public function get_by_word($word){ $temp = array(); foreach($this->arr as $c=>$v){ $e=$this->arr[$c]['name']; for($i=0;$i<strlen($e);$i++){ $f=substr($e,$i,2); if($f==$word){ array_push($temp,$this->arr[$c]); } } } return $temp;
}
/** * 返回包含指定性别和在某年份以后的数组 * @param string $sex 性别 * @param string $year年份 * @return array */ public function get_between_sex_year($sex,$year){ $temp = array(); foreach($this->arr as $c=>$v){ $age=$this->arr[$c]['age']; if($this->arr[$c]['sex']==$sex){ $number=idate("Y")-$year; if($number>$age){ array_push($temp,$this->arr[$c]); } } } return $temp; } /** * 返回性别为男后加先生,性别为女后加小姐的数组 * @return array */ public function get_both_miss_mr(){ $temp = array(); foreach($this->arr as $c=>$v){ $d=$d+1; if($this->arr[$c]['sex']=="男"){ $this->arr[$c]['name']=$this->arr[$c]['name']."先生"; array_push($temp,$this->arr[$c]); }elseif($this->arr[$c]['sex']=="女"){ $this->arr[$c]['name']=$this->arr[$c]['name']."小姐"; array_push($temp,$this->arr[$c]); } } return $temp; } /** * 返回按年龄排序的数组 * @return array */ public function get_order_age(){ $temp = array(); $array=array(); foreach($this->arr as $c=>$v){ $a=$this->arr[$c]['age']; array_push($array,$a); } asort($array); foreach($array as $c=>$v){ array_push($temp,$this->arr[$c]); } return $temp; } /** * 返回按姓氏挨在一起排序的数组 * @return array */ public function get_order_firstname(){ $temp = array(); $array=array(); foreach($this->arr as $c=>$v){ $a=$this->arr[$c]['name']; array_push($array,$a); } asort($array); foreach($array as $c=>$v){ array_push($temp,$this->arr[$c]); } return $temp; }}
?>
就改动了getdata和savedata函数
- 练习19
- 练习19
- 练习19
- 练习19
- 【SICP练习】19 练习1.25
- leetCode练习(19)
- 19 联动菜单练习
- Python练习实例19
- 2017.11.19C练习
- Python 练习实例19
- 指针练习-19
- 练习
- “++”“--”练习
- 练习
- 练习
- 练习
- 练习
- 练习
- linux下产生core文件以及不产生core文件的条件
- linux 线程池
- python logging现学现用 – TimedRotatingFileHandler使用方法
- HTML学习总结1
- 关于Java 23种设计模式的有趣见解 .
- 练习19
- HDU 1159 Common Subsequence(还是DP)
- 智能指针
- 《算法导论》 8.4 桶排序
- Web Service学习笔记
- 消息中间件 Apache Qpid 简介,第 1 部分
- lcc编译器
- struts控制器核心RequestProcessor
- asp.net 常见问题收集整理(不断收集中)