简体   繁体   English

如何在PHP中使用If / else语句来过滤mysql结果

[英]How to Use the If/else statement in PHP to filter mysql result

In the beginning, each person have 2 languages of messages ( chimessage & engmessage )saved to the database. 首先,每个人都有两种语言的消息( chimessageengmessage )保存到数据库中。

Each of them have selected a preferred language (either chi or eng). 他们每个人都选择了首选的语言(中文或英文)。 In the display page, i'm trying to show each person's message with their preferred language. 在显示页面中,我试图用他们喜欢的语言显示每个人的消息。 eg, If person A prefer eng, the page will only show engmessage. 例如,如果某人A偏爱eng,则该页面将仅显示engmessage。

PS the display page will include both engmessage and chimessage for different people PS显示页面将包括针对不同人群的热情和钟声

I am wondering how can i do that, im thinking using the if/else statement. 我想知道如何使用if / else语句来做到这一点。

PS I know iam using deprecated mysql,still trying to learn php, plz bear with me PS我知道我使用过时的mysql,仍然尝试学习php,请与我

 <?php

 include('connect-db.php');

 $result = mysql_query("SELECT * FROM table
  WHERE language ='Chi' OR language ='Eng'");


 while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {

 echo "<tr>";

 echo '<td width="200px">' . $row['people'] . '</td>';
 echo '<td width="200px">' . $row['language'] . '</td>';
 echo '<td>' . $row['chimessage'] ."<br />";
 echo '<td>' . $row['engmessage'] ."<br />";

 }

 echo "</tr>";
 echo "</table>";
 ?>

Expected result 预期结果

 People      Language       Msg
 A            Chi           物理治療
 B            Eng           Physiotherapy

Change your code like this 像这样更改代码

 <?php

 include('connect-db.php');

 $result = mysql_query("SELECT * FROM table
  WHERE language ='Chi' OR language ='Eng'");


echo "<table>";

 while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {

 echo "<tr>";
 echo '<td width="100px">' . $row['id'] . '</td>';
 echo '<td width="200px">' . $row['people'] . '</td>';

 if($row['language'] == 'Chi')
 {
    echo '<td>' . $row['chimessage'] ."</td>";
 }
 else if($row['language'] == 'Eng')
 {
    echo '<td>' . $row['engmessage'] ."</td>";
 }
 echo "</tr>";

 }


 echo "</table>";
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM