重构函数,数据库

user.clss.php

<?php

/*一个功能,可以分解成很多小的功能,可以一直细分下去,只到没有过分重复的代码。
重构,就是把一个小的功能,,拎出来,形成一个独立的服务。 
这个函数虽然只有两行,但也确实是提供了一个服务。
原则: 重构一方面是重组代码;另一重要方面是设计的的重构
*/

class User{
	
	protected $arr;//进行封装不让外部使用,除子类和父类
	
	function __construct(){
		$this->arr = $this->get_data();//构造函数,在类一实例化的时候就会执行
	}
	
	
	
	public function isNotExist(){
		echo "Does not exist<br/>";
		echo "<a href=\"http://localhost/Reconstruction/Reconstruction002/all.php\">重新输入</a>";
	}

	/**
	*提供数据源
	*把文件内容变成数组
	*@param string wj
	* @return array
	*/
	protected function get_data(){
		$con =mysql_connect("localhost","root","123456");
		if(!$con){
			die('sorry,not connect'.mysql_error());
		}
		mysql_query("set character set 'utf-8'");
		mysql_query("set names utf8");
		mysql_select_db("my_user_db", $con);
		$b = mysql_query("select * from user");
		$ar = array();
		while($c=mysql_fetch_array($b)){
			array_push($ar,$c);
		}
		return $ar;
	}
	/**
	* 返回经过修改后的数据
	*把数组变成文件
	*@param string $wj文件名
	*@param array $arr数组
	 * @return array 
	 */
	/* protected function save_data($arr){
		$file = fopen("data.txt","w+");
		
		foreach($arr as $c=>$v){
			$d=$c+1;
			fwrite($file,$d."#".$v['name']."#".$v['age']."#".$v['sex']);
		}
		fclose($file);
	} */
	
	/**
	 * 提供一方法,以表格形式输出数据
	 * @param array找到的数组
	 * @return string 
	 */
	public function joinHtml($array){
		if(count($array)==0){
			User::isNotExist();
		}
		$d = 0;
		$b ="<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></body>";
		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($v['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($v['age']>=$age1&&$v['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($v['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($v['age']==$age&&$v['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($this->arr as $c=>$v){
		
			if($v['sex']==$sex){
				$d=$v['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($v['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=$v['name'];
			$f=substr($e,0,3);
			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=$v['name'];
			$f=substr($e,0,2);
			$b=$f.$b;
		}
		foreach($this->arr as $c=>$v){
			$e=$v['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=$v['name'];
			for($i=0;$i<strlen($e);$i++){
				$f=substr($e,$i,3);
				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=$v['age'];
			if($v['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($v['sex']=="男"){
				$v['name']=$v['name']."先生";
				array_push($temp,$this->arr[$c]);
			}elseif($v['sex']=="女"){
				
				$v['name']=$v['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=$v['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=$v['name'];
			array_push($array,$a);
		}
		asort($array);
		foreach($array as $c=>$v){
			array_push($temp,$this->arr[$c]);
		}
		
		return $temp;
		
	}
}





?>


 

newuser.class.php

<?php
/*一个功能,可以分解成很多小的功能,可以一直细分下去,只到没有过分重复的代码。
重构,就是把一个小的功能,,拎出来,形成一个独立的服务。 
这个函数虽然只有两行,但也确实是提供了一个服务。
原则: 重构一方面是重组代码;另一重要方面
是设计的的重构
*/

//require_once("User.class.php");

class NewUser extends User{
	/**
	*返回包含指定年龄间和性别的数组
	* @param string $sex
	* @param int $age1 $age2
	* return array
	*/
	public function get_both_sex_ages($sex, $age1,$age2){
		$temp = array();
		$ages = $this->get_between_ages($age1,$age2);
		foreach($ages as $c=>$v){
				if($v['sex']==$sex){
					array_push($temp,$v);
				}
			
		}
		return $temp;
	}
	//因为$arr不是protected封装的,害我多花了n长时间。注意。。
	/**
	 * 返回包含指定字符和性别的数组
	 * @param string $sex ن€مˆ
	 * @param string $wordنŒ‡مšم­—ه،
	 * @return array 
	 */
	public function get_both_word_sexs($sex,$word){
		$temp = array();
		$wds = $this->get_by_word($word);
		foreach($wds as $v){
			if($v['sex']==$sex){
				array_push($temp,$v);
			}
		}
		return $temp;
	}
	/**
	*返会增加一个数组后的整个数组
	* @param string name„ age„ sex
	* @return array
	*/
	/* public function add_record($name,$age,$sex){
		$id = count($this->arr)+1;
		$temp = array("id"=>$id,"name"=>$name,"age"=>$age,"sex"=>$sex."\r\n");
		array_push($this->arr,$temp);
		$this->save_data($this->arr);
		return $this->arr;
	} */
	
	/**
	*  返回修改数组后的新数组
	* return array
	*/
	public function update_record(){
		$this->conn();
		$name = $_GET['name'];
		$age = $_GET['age'];
		$sex = $_GET['sex'];
		$id = $_GET['id'];
		
		
		$upd = "UPDATE user SET name='{$name}',age='{$age}', sex='{$sex}' WHERE id='{$id}'";
		
		
		
		mysql_query($upd);
		
		$a = $this->read_array();
		return $a;
		
	}
	
	/**
	* 返回删除一个数组后的新数组
	*/
	public function delete_record(){
		$this->conn();
		$id = $_GET['id'];
		
		$del = "DELETE FROM user WHERE id='{$id}'";
		mysql_query($del);
		
		$a = $this->read_array();
		return $a;
	}  
	
	/**
	* 返回插入一个数组后的新数组
	* return array
	*/
	public function insert_record(){	
		$this->conn();
		$name = $_GET['name'];
		$age = $_GET['age'];
		$sex = $_GET['sex'];
		
		$ins = "insert into user (name,age,sex) values ('{$name}','{$age}','{$sex}')";
		mysql_query($ins);
		
		$a = $this->read_array();
		return $a;
		
	}
	
	/**
	*连接数据库
	*/
	public function conn(){
		$con =mysql_connect("localhost","root","123456");
		mysql_query("set character set 'utf-8'");
		mysql_query("set names utf8"); 
		if(!$con){
			die('sorry,not connect'.mysql_error());
		}
		mysql_select_db("my_user_db", $con);
	}
	
	/**
	*数据库取数据
	*/
	public function read_array(){
		
		$b = mysql_query("select * from user");
		$ar = array();
		while($c=mysql_fetch_array($b)){
			array_push($ar,$c);
		}
		//mysql_close($con);
		return $ar;
	}
	
	
	

}



?>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值