-2

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:

enter image description here

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.

4

5 回答 5

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>";
}
于 2012-10-19T03:50:02.297 回答
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);
        }
于 2012-10-19T03:42:55.663 回答
0

尝试这个

   if(in_array("Behnam",$row) {
        echo " B . ";
    }
    else if(in_array("Train",$row) {
       echo " T . ";
    }
    else {
        echo " N . ";
    }
于 2012-10-19T05:01:40.993 回答
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>
于 2012-10-28T04:17:25.167 回答
-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;
                    }
}
    ?>
于 2012-10-19T03:41:13.827 回答