2014-11-15 67 views
0

我有一個表有一個名爲result的列,結果是一個銀行通過post方法發送給我的變量...並且值爲:1,2 ... so當銀行向我發送1時,我想向用戶展示其接受的內容,如果是2,則向用戶展示其被拒絕的內容...我需要一個如果其他公式的公式。更改表中顯示的結果

<?php 
$id_get= $_POST['id_get']; 
$trans_id = $_POST['trans_id']; 
$servername = "localhost"; 
$username = "blah"; 
$password = "blah"; 
$dbname = "blah"; 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
?> 
<?php 
mysqli_query($conn, "SET NAMES 'utf8'"); 
$result = mysqli_query($conn,"SELECT id_get, trans_id, result FROM users"); 
echo "<table border='1' width='800' align='center'> 
<tr> 
<th>ID</th> 
<th>transaction Result</th> 
</tr>"; 


while($row = mysqli_fetch_array($result)) 
{ 
echo "<tr align='center'>"; 
echo "<td width=10%>" . $row['id_get'] . "</td>"; 
echo "<td width=10%>" . $row['trans_id'] . "</td>"; 
echo "</tr>"; 
} 
echo "</table>";?><br> 
<div align="center"> 
<?php 

mysqli_close($conn); 
?> 

回答

0

可以檢查$row['result']價值while($row = mysqli_fetch_array($result))塊內,並顯示<td>相應

while($row = mysqli_fetch_array($result)) 
{ 
    echo "<tr align='center'>"; 
    echo "<td width=10%>" . $row['id_get'] . "</td>"; 
    echo "<td width=10%>" . $row['trans_id'] . "</td>"; 
    if ($row['result'] == '1') { 
     echo "<td width=10%>Accepted</td>"; 
    } 
    else if ($row['result'] == '2') { 
     echo "<td width=10%>Rejected</td>"; 
    } 
    else { 
     echo "<td width=10%>Unknown</td>"; 
    } 
    echo "</tr>"; 
} 

內容或者你也可以將您的查詢更改這個

$result = mysqli_query($conn,"SELECT id_get, trans_id, (CASE WHEN result = 1 THEN 'Accepted' WHEN result = 2 THEN 'Rejected' ELSE 'Unknown' END) AS result_description FROM users"); 

,並只顯示$row['result_description']

while($row = mysqli_fetch_array($result)) 
{ 
    echo "<tr align='center'>"; 
    echo "<td width=10%>" . $row['id_get'] . "</td>"; 
    echo "<td width=10%>" . $row['trans_id'] . "</td>"; 
    echo "<td width=10%>" . $row['result_description'] . "</td>"; 
    echo "</tr>"; 
} 
0

您可以使用case聲明:

SELECT id_get, trans_id, 
     (case when result = 1 then 'Accepted' 
      when result = 2 then 'Rejected' 
      else 'Unknown' 
     end) as ResultString 
FROM users; 

您還可以在數據庫中創建一個參考表,並使用join。這對於真正的應用程序是推薦的,因爲它確保相同的字符串用於任何查詢。

1

用例在PHP

$resultname= $row['trans_get']; 
switch($resultname){ 
    case("1"): 
     echo "Accepted"; 
    break; 

    case("2"): 
     echo "Rejected"; 
    break; 

    default: 
     echo "N/A"; 
    break; 
}  

因此,它應該是這樣的:

while($row = mysqli_fetch_array($result)) 
    { 

    $resultname= $row['trans_get']; 
    switch($resultname){ 
     case("1"): 
      echo "Accepted"; 
     break; 

     case("2"): 
      echo "Rejected"; 
     break; 
     default: 
      echo "N/A"; 
     break; 
    }  
    echo "<tr align='center'>"; 
    echo "<td width=10%>" . $row['id_get']. "</td>"; 
    echo "<td width=10%>" . $resultname . "</td>"; 
    echo "</tr>"; 
    }