我如何简化这个PHP代码? switch()函数太多了

时间:2012-10-19 03:37:45

标签: php

我在jQuery Mobile中制作了listview,并使用PHP来提供其内容。它工作正常,但代码太长,大部分代码彼此非常相似。有没有办法简化代码?请看一下代码,然后我将解释我真正需要做的事情:

<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>

结果是:

enter image description here

通过使用while ($row = mysql_fetch_array($result)){,我可以逐个获取sql表的每一行。但我也需要检查每个单元格(列)的值。

5 个答案:

答案 0 :(得分:1)

将代码分组为2参数函数:

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

调用为:

 <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);
        }

答案 1 :(得分:1)

这通过在行中的所有列上应用函数(先跳过)然后将它们与" . "串在一起来概括它。

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>";
}

答案 2 :(得分:0)

试试这个

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

答案 3 :(得分:0)

这是最终结果:

<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>

答案 4 :(得分:-1)

    <?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;
                    }
}
    ?>