简体   繁体   中英

How I can simplify this PHP code? too many switch() function

I made listview in jQuery Mobile and I used PHP to feed its contents. It works properly, but the code is too long and most part of it are very similar to each other. Is there any way to simplify the code? Please have a look at the code, then I'll explain what I really need to do:

<ol data-role="listview">
            <?php
            while ($row = mysql_fetch_array($result)){
                echo "<li><a href=\"#\">";
                // first column check
                switch ($row[1]) {
                    case "Behnam":
                        echo " B . ";
                        break;
                    case "Tarin":
                        echo " T . ";
                        break;
                }
                // second column check
                switch ($row[2]) {
                    case "Behnam":
                        echo " B . ";
                        break;
                    case "Tarin":
                        echo " T . ";
                        break;
                    default:
                        echo " N . ";
                }
                // third column check
                switch ($row[3]) {
                    case "Behnam":
                        echo " B . ";
                        break;
                    case "Tarin":
                        echo " T . ";
                        break;
                    default:
                        echo " N . ";
                }
                // fourth column check
                switch ($row[4]) {
                    case "Behnam":
                        echo " B . ";
                        break;
                    case "Tarin":
                        echo " T . ";
                        break;
                    default:
                        echo " N . ";
                }
                // fifth column check
                switch ($row[5]) {
                    case "Behnam":
                        echo " B . ";
                        break;
                    case "Tarin":
                        echo " T . ";
                        break;
                    default:
                        echo " N . ";
                }
                // sixth column check
                switch ($row[6]) {
                    case "Behnam":
                        echo " B ";
                        break;
                    case "Tarin":
                        echo " T ";
                        break;
                    default:
                        echo " N ";
                }
                echo "</li></a>";
            }
            ?>
            </a></li>
        </ol>

and the result is:

在此处输入图片说明

By using while ($row = mysql_fetch_array($result)){ I can fetch each row of the sql table one by one. But I also need to check the value of each cell(column) as well.

Group the code in a 2 parameter function:

  function checkRowValue($row, $checkDefault) {
      switch ($row) {
          case "Behnam":
              echo " B . ";
              break;
          case "Tarin":
              echo " T . ";
              break;
          default:
              if ($checkDefault)
                  echo " N . "; 
      }
  } 

Invoke as:

 <ol data-role="listview">
        <?php
        while ($row = mysql_fetch_array($result)){
            echo "<li><a href=\"#\">";
            // first column check
            for ($i = 0; $i < 7; $i++)
                checkRowValue($row[i], $i > 0);
        }

This generalizes it by applying a function over all the columns in your row (skipping first) and then string them together with " . " .

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo "<li><a href=\"#\">";
    echo join(' . ', array_map(function($v) {
        if ($v == 'Behnam') {
            return 'B';
        elseif ($v == 'Tarin') {
            return 'T';
        else {
            return 'N';
        }
    }, array_slice($row, 1));
    echo "</li></a>";
}

Try This

   if(in_array("Behnam",$row) {
        echo " B . ";
    }
    else if(in_array("Train",$row) {
       echo " T . ";
    }
    else {
        echo " N . ";
    }

This is the final result:

<ul id="competition_list" data-role="listview" data-inset="true" data-theme="a">
            <?php
            // using the returned value from database to feed the list.
            // showing the competiton rounds' outcome.
            while ($row = mysql_fetch_array($result)){
                    echo "<li><a href=\"#\">";
                    echo $row[0] . ".";  //this is the id column in the table and will give me the number of the row
                    for($i = 1; $i <= 5; $i++){
                        switch ($row[$i]) {
                        case "Behnam":
                            echo "&nbsp;&nbsp; B &nbsp;&nbsp;";
                            break;
                        case "Tarin":
                            echo "&nbsp;&nbsp; T &nbsp;&nbsp;";
                            break;
                        }
                    } // end for()
                    echo "</a></li>";
            } // end while()
            ?>
        </ul>
    <?php
                while ($row = mysql_fetch_array($result)){
                    echo "<li><a href=\"#\">";
                    // first column check
            foreach($row as r)
            {


                    switch (r) {
                        case "Behnam":
                            echo " B . ";
                            break;
                        case "Tarin":
                            echo " T . ";
                            break;
                    }
}
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM