首頁 >後端開發 >php教程 >怎么合并多个if条件

怎么合并多个if条件

WBOY
WBOY原創
2016-06-13 10:23:391427瀏覽

如何合并多个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 "更新成功!";
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn