Heim  >  Artikel  >  Backend-Entwicklung  >  怎么合并多个if条件

怎么合并多个if条件

WBOY
WBOYOriginal
2016-06-13 10:23:391409Durchsuche

如何合并多个if条件
如题:代码效率不高,冗余太多,在线等。

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->if($start_id == $first_id && $arrive_id == '') {                    echo "我是第一个城市城市";                    //去查询第一个城市是否添加了交通工具                    $start_id = '';                    $arrive_id = $first_id;                    $res = $this->model->get_traffic($tripid, $type, $start_id, $arrive_id);                    if($res) {                        $affected_rows = $this->model->update_traffic($traffic, $tripid, $type, $start_id, $arrive_id);                        if($affected_rows > 0) {                            echo "更新成功!";                        }                    }else{                        $trafficid = $this->model->add_traffic($traffic, $tripid, $start_id, $arrive_id, $type, $dateline);                        if($trafficid > 0) {                            echo '添加成功';                        }                    }                    exit;                }                if($start_id == $last_id && $arrive_id == '') {                    echo "我是最后一个城市";                    $start_id = $last_id;                    $arrive_id = '';                    $res = $this->model->get_traffic($tripid, $type, $start_id, $arrive_id);                                        if($res) {                        $affected_rows = $this->model->update_traffic($traffic, $tripid, $type, $start_id, $arrive_id);                        if($affected_rows > 0) {                            echo "更新成功!";                        }                    }else{                        $trafficid = $this->model->add_traffic($traffic, $tripid, $start_id, $arrive_id, $type, $dateline);                        if($trafficid > 0) {                            echo '添加成功';                        }                    }                    exit;                }                if($start_id == '' && $arrive_id == '') {                    echo "需选择2个城市才能添加交通工具";                }else{                    echo "选择正确可以入库";                    $res = $this->model->get_traffic($tripid, $type, $start_id, $arrive_id);                                        if($res) {                        $affected_rows = $this->model->update_traffic($traffic, $tripid, $type, $start_id, $arrive_id);                        if($affected_rows > 0) {                            echo "更新成功!";                        }                    }else{                        $trafficid = $this->model->add_traffic($traffic, $tripid, $start_id, $arrive_id, $type, $dateline);                        if($trafficid > 0) {                            echo '添加成功';                        }                    }                }            }            $this->display = true;            $this->view->assign('cities', $cities);            $this->view->assign('traffic', $_config['traffic']);            $this->view->assign('tripid', $tripid);            $this->view->assign('cityid', $cityid);            $this->view->assign('type', $type);        }else if($type == 2) {            echo "我是来添加景点美食绿色的";        }


------解决方案--------------------
你的if else {}括号匹配不?我怎么觉得不对啊。。。
------解决方案--------------------
可以用switch 试试!
------解决方案--------------------
$res = $this->model->get_traffic($tripid, $type, $start_id, $arrive_id);
if($res) {
$affected_rows = $this->model->update_traffic($traffic, $tripid, $type, $start_id, $arrive_id);
if($affected_rows > 0) {
echo "更新成功!";
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn