$type=$_POST['type'];//直接看类型获取对应数据 无需登录switch ($type){case 1: //sql读数据1 $acase 2: //sql读数据2 $a}if($a){} //读出数据有就返回 没有就返回错误 die;if($user){} //验证是否登录//登陆后才能依类型获取的数据switch ($type){case 3: xxxxxxx $bcase 4: xxxxxxx $b}if($b){} //读出数据有就返回 没有就返回错误 die;switch ($type){case 5: xxxxxxx $ccase 6: xxxxxxx $c}if($c){} //读出数据有就返回 没有就返回“此项为空” 这里不算错
$type=$_POST['type']; //$type 只会是单值switch ($type) { case 1: //生成 sql 指令1 case 2: //条件生成 sql 指令2 //sql读数据 $a if($a){} //读出数据有就返回 没有就返回错误 die; break; case 3: xxxxxxx $b case 4: xxxxxxx $b if($b){} //读出数据有就返回 没有就返回错误 die; break; case 5: xxxxxxx $c case 6: xxxxxxx $c if($c){} //读出数据有就返回 没有就返回“此项为空” 这里不算错}
$type=$_POST['type']; //$type 只会是单值switch ($type) { case 1: //生成 sql 指令1 case 2: //条件生成 sql 指令2 //sql读数据 $a if($a){} //读出数据有就返回 没有就返回错误 die; break; case 3: xxxxxxx $b case 4: xxxxxxx $b if($b){} //读出数据有就返回 没有就返回错误 die; break; case 5: xxxxxxx $c case 6: xxxxxxx $c if($c){} //读出数据有就返回 没有就返回“此项为空” 这里不算错}
1 2可以
但1 2 无需登录
在
if($a){}
break;
之后
加了if($user)
判断登录
然后case 3
case 4
if($b){1}else{2}
结果一直是2
那就请你说明规则和贴出真实的代码
switch ($type) { case 1: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); case 2: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 3: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 4: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($b){ $rep['code'] = 1; $rep['valid'] = $b; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 5: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 6: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($c){ $rep['code'] = 1; $rep['valid'] = $c; reponse($rep);} else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}}
function reponse($rep){ echo json_encode($rep); die;}
switch ($type) { case 1: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); case 2: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 3: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 4: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($b){ $rep['code'] = 1; $rep['valid'] = $b; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 5: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 6: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($c){ $rep['code'] = 1; $rep['valid'] = $c; reponse($rep);} else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}}
function reponse($rep){ echo json_encode($rep); die;}
$b = mysql_fetch_assoc(mysql_query($sql ));
那就请你说明规则和贴出真实的代码
type 的 1、2、3、4...都代表什么意思?
case 3 和 case 4 的 sql 指令是一样的吗?
switch ($type) { case 1: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); case 2: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 3: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 4: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($b){ $rep['code'] = 1; $rep['valid'] = $b; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 5: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 6: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($c){ $rep['code'] = 1; $rep['valid'] = $c; reponse($rep);} else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}}
function reponse($rep){ echo json_encode($rep); die;}
$b = mysql_fetch_assoc(mysql_query($sql ));
type 的 1、2、3、4...都代表什么意思?
case 3 和 case 4 的 sql 指令是一样的吗?
那应该这样写
switch ($type) { case 1: $sql = SQL语句1 break; case 2: $sql = SQL语句2 break; case 3: $sql = SQL语句3 break; case 4: $sql = SQL语句4 break; case 5: $sql = SQL语句5 break; case 6: $sql = SQL语句6 break;}$a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}
$fun = switch_.$_GET['type'];$fun();function switch_1(){ a(true);}function switch_2(){ a(false);}function a($condition){ if(!$condition){ die('error'); } echo __Method__;}function switch_3(){ b(true);}function switch_4(){ b(false);}function b($login){ if(!$login){ die('error'); } echo __Method__;}function switch_5(){ c(true);}function switch_6(){ c(false);}function c($condition){ if(!$condition){ die('error'); } echo __Method__;}
这样是可以 不过有个小问题
就是成功都是返回数据
但失败要返回的错误提示是不一样的 还有要判断为空但是不算错的情况
所以要完全实现就要做多次判断 我想这样肯定不是最好的
我做了些调整 整体运作没问题了 不过应该也还有可以优化的地方
顺便我case实际有16个 写那么多function也挺麻烦了 不过这种方式学到了
感谢二位