简体   繁体   English

如何在一个循环中正确显示所有英文定义而不用php分隔?

[英]How to properly display all the english definition in just one loop without separation in php?

The logical error display is that the english word that has more meaning are displayed like this: 逻辑错误显示是这样显示具有更多含义的英语单词:

English word: lie (1)
Definition: not honest

English word: lie (1)
Definition: deliberately say something untrue

English word: lie (2)
Definition: to stretch out on a surface that is slanted or horizontal

English word: lie (2)
Definition: to be positioned on and supported by a horizontal surface

I want the output to be displayed like this: 我希望输出显示如下:

English word: lie (1)
Definition: 1. not honest
            2. deliberately say something untrue

If it has the same word with different meaning this should be the output: 如果它具有相同的词,但含义不同,则应为输出:

English word: lie (1)
Definition: 1. not honest
            2. deliberately say something untrue

English word: lie (2)
Definition: 1. to stretch out on a surface that is slanted or horizontal
            2. to be positioned on and supported by a horizontal surface

Here's my code: 这是我的代码:

    <?php                                   

    $search = $_POST['word'];

    $query = "SELECT *" .
             " FROM english, english_meaning, maranao, filipino, translate". 
             " WHERE english.eng_id = english_meaning.eng_id and english.eng_id = translate.eng_id and maranao.mar_id = translate.mar_id and filipino.fil_id = translate.fil_id and english.english_word like '$search%'";

            $result = mysql_query($query) or die(mysql_error()); 

            $num_rows = mysql_num_rows($result);

            if($num_rows==0)
            {
                echo "No Results Found. Please try again";
            }


                while($row=mysql_fetch_array($result))
                {
                ?>
                    <div style = " background-color: #daeaff; border-radius: 10px; padding: 15px;">
                    <font size='3' face='Times New Roman'>

                        English word: <b><?php echo $row['english_word']; ?></b><br>
                        Definition:<b><div style="width:600px; border:0px solid orange; margin-left: 79px; word-wrap:break-word; margin-top: -18px;"><?php echo $row['definition'] ?></b></div>

                    </font>
                    </div>
                    <?php echo "<br>"; ?>

                <?php
                }
            }
    mysql_close($con);
?>

A couple of notes on the code: 关于代码的一些注意事项:

  • It is subject to SQL injection. 它受SQL注入的约束。 You should stop using the mysql_* functions, they have been deprecated, and replace them with msqli_* /PDO methods, and parameterized queries. 您应该停止使用已被弃用的mysql_*函数,并用msqli_* / PDO方法和参数化查询替换它们。

  • The HTML needs improvement too: you should not have cross-tags (eg: <b><div>...</b></div> ), and avoid obsolete tags that are no longer supported in HTML5 (eg: <font> ). HTML也需要改进:您不应该使用交叉标记(例如: <b><div>...</b></div> ),并避免HTML5中不再支持的过时标记(例如: <font> )。

You could achieve what you want by keeping track of the previous word, and then displaying the whole information or just a part of it. 您可以通过跟踪前一个单词,然后显示全部或部分信息,来实现所需的功能。 Something like this (in pseudo-code): 像这样的东西(用伪代码):

  1. Initialize previous_word variable to "". previous_word变量初始化为“”。
  2. While there are still rows (fetch): 虽然仍然有行(提取):
    1. If the English word is not the same as the previous_word . 如果英语单词与previous_word
      1. Print "English word: " + word. 打印“英文单词:” +单词。
    2. Print definition. 打印定义。
    3. Update previous_word with the value of the current word. 用当前单词的值更新previous_word

Modifying the code from your question, the result would look like this (only the loop part): 从您的问题修改代码,结果将如下所示(仅循环部分):

// AM - variable that will hold the previous word
$previous_word = "";
$row = 0;

while($row = mysql_fetch_array($result)) 
{
    // AM - Show the word only if it's a new one    
    if ($row["english_word"] != $previous_word) { 

        // AM - close the previous word definition
        if ($row > 0) { echo "</div><br>"; }

    ?>
    <div style = " background-color: #daeaff; border-radius: 10px; padding: 15px; font-family:'Times New Roman'; font-size:3;">
            English word: <b><?php echo $row['english_word']; ?></b><br>
            Filipino word: <b><?php echo $row['filipino_word']; ?></b><br>
            Maranao word: <b><i><?php echo $row['maranao_word']; ?></i></b><br>
            Definition:
<?php 
    } 
?>
        <div style="width:600px; border:0px solid orange; margin-left: 79px; word-wrap:break-word; margin-top: -18px;"><b><?php echo $row['definition'] ?></b></div>
<?php
    // AM - Update the previous word and the row count
    $previous_word = $row["english_word"];
    $row++;
}

// AM - close the last word definition
if ($row > 0) { echo "</div><br>"; }

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

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