Rumah > pembangunan bahagian belakang > tutorial php > 怎么合并多个if条件

怎么合并多个if条件

WBOY
Lepaskan: 2016-06-13 10:23:39
asal
1429 orang telah melayarinya

如何合并多个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 "我是来添加景点美食绿色的";        }
Salin selepas log masuk


------解决方案--------------------
你的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 "更新成功!";
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan