Javascript 在php中,如何在一个循环中正确显示所有英文定义而不分离?
逻辑错误显示为具有更多含义的英语单词显示如下:Javascript 在php中,如何在一个循环中正确显示所有英文定义而不分离?,javascript,php,sql,Javascript,Php,Sql,逻辑错误显示为具有更多含义的英语单词显示如下: 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
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
我希望输出显示如下:
English word: lie (1)
Definition: 1. not honest
2. deliberately say something untrue
如果同一个词有不同的含义,则应为输出:
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
这是我的密码:
<?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);
?>
定义:
关于代码的几个注释:
- 它受制于SQL注入。您应该停止使用
函数,因为它们已被弃用,并用mysql\u*
/PDO方法和参数化查询替换它们msqli\u*
- HTML也需要改进:您不应该有交叉标记(例如:
),并避免HTML5不再支持的过时标记(例如:…
)
上一个单词变量初始化为“”
修改问题中的代码,结果如下所示(仅循环部分):
//AM-将保存前一个单词的变量
$previous_word=“”;
$row=0;
while($row=mysql\u fetch\u数组($result))
{
//AM-仅当单词是新单词时才显示
如果($row[“english_word”!=$previous_word){
//AM-关闭上一个单词定义
如果($row>0){echo“
”;}
?>
英语单词:
菲律宾语:
马拉诺语:
定义:
从sql语句中添加限制1它将只显示一个定义。如何计算括号中的数字,例如:lie(1)
?我自己刚刚添加了它,这将有助于查看更多的列名(甚至可能是表结构).你如何记录一个词的某些定义是相互关联的,而另一些是相互独立的?(你如何知道lie(1)有两个定义,而lie(2)有两个不同的定义?)@用户3808774
应该在每个单词后面,对吗?我会用它更新答案,如果它是不同的东西,那么给我留下一条评论,我会检查它。想法是把它放在结尾
之后。页脚也会受到影响。页脚是如何受到影响的?上面的代码中没有页脚。页脚的边距每次搜索单词时,页脚都会受到影响。我修复了代码中的一个错误(if($row>0){echo“
”;}
)。关于页脚的错误,可能是因为页脚前面有太多的关闭(
)(但我看不到代码就无法知道)。
// 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>"; }