2014-04-08 84 views
1

我有一個填充了MySQL查詢結果的表。我需要在表中插入一個額外的列作爲PHP函數,它根據查詢中字段的值生成字符串(我認爲這是如何解決問題的)。這可能嗎?我試圖將列設置爲一個變量,並在該函數中調用該變量(該代碼中未設置爲用戶定義的函數),但是我必須做錯了某些事情,因爲表格根本沒有填充。帶MySQL查詢的PHP函數表

$query1 = "SELECT `rank`.`term`, 
        `rank`.`acadplan`, 
        `rank`.`projlvl`, `rank`.`gpa`, 
        (SELECT COUNT(gpa) FROM `rank1` 
        WHERE `rank1`.`term` = '{$term}' 
         AND `rank1`.`acadplan`='{$acadplan}' 
         AND `rank1`.`projlvl`='{$projlvl}' 
         AND `rank1`.`gpa` > `rank`.`gpa`) + 1 AS 'ranking', 
       FROM `rank` 
        WHERE `rank`.`term` ='{$term}' 
         AND `rank`.`acadplan`='{$acadplan}' 
         AND `rank`.`projlvl`='{$projlvl}' 
       ORDER BY `rank`.`gpa` DESC"; 


$result = mysqli_query($query1); 
    echo "<table>"; 
     echo "<th>Term</th>"; 
     echo "<th>Plan</th>"; 
     echo "<th>Level</th>"; 
     echo "<th>GPA</th>"; 
     echo "<th>Rank</th>"; 
     echo "<th>NEWCOL</th>"; 
    echo "<col width = '125'>"; 
    echo "<col width = '125'>"; 
    echo "<col width = '125'>"; 
    echo "<col width = '125'>"; 
    echo "<col width = '125'>"; 
    echo "<col width = '250'>"; 


$num_rows = mysqli_num_rows($result); 

這是一個需要被執行的功能:

if ($num_rows <=24) { 
    switch ($num_rows) { 
     case ($rank >=1 and $rank <=3): 
      echo "1-3"; 
      break; 
     case ($rank >=4 and $rank <=6): 
      echo "4-6"; 
      break; 
     case ($rank >=7 and $rank <=9): 
      echo "7-9"; 
      break; 
     case ($rank >=10 and $rank <=12): 
      echo "10-12"; 
      break; 
     case ($rank >=13 and $rank <=15): 
      echo "13-15"; 
      break; 
     case ($rank >=16 and $rank <=18): 
      echo "16-18"; 
      break; 
     case ($rank >=19 and $rank <=21): 
      echo "19-21"; 
      break; 
     case ($rank >=22 and $rank <=24): 
      echo "22-24"; 
      break; 
     } 
    } elseif ($num_rows >=25 and $num_rows <=50) { 
     echo "greater than 24"; 
    } elseif ($num_rows >=51 and $num_rows <=100) { 
     echo "greater than 50"; 
    } elseif ($num_rows >=101 and $num_rows <=150) { 
     echo "greater than 100"; 
    } elseif ($num_rows >=151 and $num_rows <=260) { 
     echo "greater than 150"; 
    } 
} 

這是表:

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['term'] . "</td>"; 
    echo "<td>" . $row['acadplan'] . "</td>"; 
    echo "<td>" . $row['projlvl'] . "</td>"; 
    echo "<td>" . $row['gpa'] . "</td>"; 
    echo "<td>" . $row['ranking'] . "</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 
+0

'mysql_queryi($ query1);'是一個錯字嗎? –

+0

我認爲,這是SO-SO校正前的xD'替代(/^mysql_ /,/^mysqli_ /)SO片段)) –

+0

是的,一個錯誤,固定的。 – user3362278

回答

0

什麼像這樣的一個解決方案:

function someFunction($rank, $num_rows) { 
    if ($num_rows <=24) { 
     switch (true) { 
      case ($rank >= 1 AND $rank <=3): 
       return "1-3"; 
      case ($rank <=6): 
       return "4-6"; 
      case ($rank <=9): 
       return "7-9"; 
      case ($rank <=12): 
       return "10-12"; 
      case ($rank <=15): 
       return "13-15"; 
      case ($rank <=18): 
       return "16-18"; 
      case ($rank <=21): 
       return "19-21"; 
      case ($rank <=24): 
       return "22-24"; 
     } 
    } elseif ($num_rows >=25 and $num_rows <=50) { 
     return "greater than 24"; 
    } elseif ($num_rows >=51 and $num_rows <=100) { 
     return "greater than 50"; 
    } elseif ($num_rows >=101 and $num_rows <=150) { 
     return "greater than 100"; 
    } elseif ($num_rows >=151 and $num_rows <=260) { 
     return "greater than 150"; 
    } 
    return ""; 
} 

然後表格:

while($row = mysqli_fetch_array($result)) 
{ 
    echo "<tr>"; 
    echo "<td>" . $row['term'] . "</td>"; 
    echo "<td>" . $row['acadplan'] . "</td>"; 
    echo "<td>" . $row['projlvl'] . "</td>"; 
    echo "<td>" . $row['gpa'] . "</td>"; 
    echo "<td>" . $row['ranking'] . "</td>"; 
    echo "<td>" . someFunction($row['ranking'], $num_rows) . "</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 

編輯: 您可以使用PHP的HTML標籤這樣的:

<td><?php echo $row['ranking']; ?></td> 

我發現在我的意見,使用PHP值它preferrable方式。當然嚴格的商業邏輯應該儘可能地分開,而不是放在HTML標籤中。

+0

這正是我所需要的。一些谷歌搜索後,我想我明白什麼時候使用返回與回聲(完整的PHP新手)。謝謝。 – user3362278

+0

我建議你閱讀一下關於在你的HTML NOY中使用PHP反之亦然(當然在PHP文件中)。我給你一個線索在編輯的答案 – mareckmareck

+0

希望我能+1你的答案,非常感謝。 – user3362278