Maison >développement back-end >tutoriel php > 怎么合并多个if条件

怎么合并多个if条件

WBOY
WBOYoriginal
2016-06-13 13:30:461263parcourir

如何合并多个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 "更新成功!";
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn