How to merge multiple if conditions

Source: Internet
Author: User
How to merge multiple if conditions such as: low code efficiency, too much redundancy, online, etc. PHPcodeif ($ start_id $ first_id & amp; $ arrive_id) {echo & quot; I am the first city & quot ;; query whether multiple if conditions are merged in the first city.
Example: the code is inefficient, redundant, and online.
PHP code
  If ($ start_id = $ first_id & $ arrive_id = '') {echo" I am the first City city "; // check whether the first city has added the vehicle $ 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 "updated successfully! ";}} Else {$ trafficid = $ this-> model-> add_traffic ($ traffic, $ tripid, $ start_id, $ arrive_id, $ type, $ dateline ); if ($ trafficid> 0) {echo 'added successfully';} exit;} if ($ start_id = $ last_id & $ arrive_id = '') {echo "I am the last city"; $ 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 "updated successfully! ";}} Else {$ trafficid = $ this-> model-> add_traffic ($ traffic, $ tripid, $ start_id, $ arrive_id, $ type, $ dateline ); if ($ trafficid> 0) {echo 'added successfully ';}} exit;} if ($ start_id = ''& $ arrive_id = '') {echo "you need to select two cities to add transportation vehicles";} else {echo "you can select the correct city to import traffic to the database"; $ res = $ this-> model-> get_traffic ($ tripid, $ type, $ start_id, $ arrive_id); if ($ res) {$ affected_rows = $ this-> model-> update_traffic ($ traffic, $ tripid, $ typ E, $ start_id, $ arrive_id); if ($ affected_rows> 0) {echo "update successful! ";}} Else {$ trafficid = $ this-> model-> add_traffic ($ traffic, $ tripid, $ start_id, $ arrive_id, $ type, $ dateline ); if ($ trafficid> 0) {echo 'added successfully ';}}}$ 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 "I am here to add scenic green food ";}


------ Solution --------------------
Does your if else {} parentheses match? What do I think is wrong...
------ Solution --------------------
You can try it with switch!
------ Solution --------------------
$ 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 "updated! ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.