HTML/PHP:如何突出显示在我生成的乘法表中搜索到的数字



HTML

<form method="GET">
<b><th>Enter rows: </th>
<input type="number" name="row"/>&nbsp&nbsp&nbsp
<th>Enter columns: </th>
<input type="number" name="column"/></b>
<br>
<br>
<input type="submit" name="generate" value="GENERATE">
<br><br>
</form>

这是PHP代码。我正在使用";对于循环";乘法表的结构,但我不知道如何突出显示乘法表中搜索到的数字。我还在搜寻。

<?php
if(isset($_GET['generate']))
{
echo "<b>";
echo "<table name='tab' border='1'>";
$rows = $_GET['row'];
$columns = $_GET['column'];
for ($row =1; $row <= $rows; $row++)
{
echo "<tr>";
for ($column = 1; $column <= $columns; $column++)
{
echo "<td>" . $column * $row ."</td>";
}
echo "</tr>";
}
echo "</table>";
echo "</b><br><br>";
echo "<form method='GET'>";
echo "<div class='mid'><b><th>Search: </th>";
echo "<input type='number' name='searchbar'/></div>";
echo "<br><br>";
echo "<input type='submit' name='search' value='SEARCH'>";
echo "<br><br>";
echo "</form>";
}
?>

您可以尝试使用以下代码:

<?php
$searchbar = isset($_POST["searchbar"]) ? intval($_POST["searchbar"]) : "0";
$rows = isset($_GET["row"]) ? intval($_GET["row"]) : "0";
$columns = isset($_GET["column"]) ? intval($_GET["column"]) : "0";
if(isset($_GET['generate']))
{
echo "<table name='tab' border='1'>";
for ($row =1; $row <= $rows; $row++)
{
echo "<tr>";
for ($column = 1; $column <= $columns; $column++)
{
$highlight = ($searchbar == $column * $row);
echo "<td ".($highlight ? 'style="background: red;"' : "")."><b>" . $column * $row ."</b></td>";
}
echo "</tr>";
}
echo "</table>";
echo "<br><br>";
echo "<form method='POST'>";
echo "<div class='mid'><b>Search: </b>";
echo "<input type='number' name='searchbar' value='$searchbar'/></div>";
echo "<br><br>";
echo "<input type='submit' name='search' value='SEARCH'>";
echo "<br><br>";
echo "</form>";
}
die();
?>

首先,如果您使用GET作为标记表单的属性,在提交过程中,您将丢失generaterowcolumn参数。如果要使用GET方法,则必须在表单中为要保留的每个参数填充3个隐藏的输入类型(例如:<input type="hidden" name="column" value="8">(。

所以你也可以试试这个代码:

<?php
$searchbar = isset($_GET["searchbar"]) ? intval($_GET["searchbar"]) : "0";
$rows = isset($_GET["row"]) ? intval($_GET["row"]) : "0";
$columns = isset($_GET["column"]) ? intval($_GET["column"]) : "0";
if(isset($_GET['generate']))
{
echo "<table name='tab' border='1'>";
for ($row = 1; $row <= $rows; $row++)
{
echo "<tr>";
for ($column = 1; $column <= $columns; $column++)
{
$highlight = ($searchbar == $column * $row);
echo "<td ".($highlight ? 'style="background: red;"' : "")."><b>" . $column * $row ."</b></td>";
}
echo "</tr>";
}
echo "</table>";
echo "<br><br>";
echo "<form method='GET'>";
echo "<input type='hidden' name='generate' value='1'/>";
echo "<input type='hidden' name='row' value='$rows'/>";
echo "<input type='hidden' name='column' value='$columns'/>";
echo "<div class='mid'><b>Search: </b>";
echo "<input type='number' name='searchbar' value='$searchbar'/></div>";
echo "<br><br>";
echo "<input type='submit' name='search' value='SEARCH'>";
echo "<br><br>";
echo "</form>";
}
?>

使用POST方法,url保持不变,并且可以在提交后传输searchbar参数。在循环中,您可以使用生成的数字检查searchbar值,并突出显示正确的数字。

最新更新