簡體   English   中英

如何簡化此 PHP 代碼? switch() 函數太多

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

我在 jQuery Mobile 中創建了列表視圖,並使用 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>

結果是:

在此處輸入圖片說明

通過使用while ($row = mysql_fetch_array($result)){我可以一一獲取sql表的每一行。 但我還需要檢查每個單元格(列)的值。

將代碼分組為一個 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);
        }

這通過在行中的所有列上應用一個函數(首先跳過)然后將它們用" . "串在一起來概括它。

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

嘗試這個

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

這是最終結果:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM