作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个解决我的问题的示例代码。我想做的是,如果我搜索“Helloworld”,那么我想通知用户没有根据他们输入的数据匹配的数据。我在想如果输入的数据与任何行都不匹配并且输入的数据与某些行匹配,我是否可以使用 if else 语句进行验证。当我想象这个问题的解决方案时,我认为这个方法就是解决方案,但我不知道如何做到这一点。我认为解决方案是把 if else 条件放在这里是我的代码我是如何思考的
如果搜索结果不是空的,那么它会显示结果,如果什么都没有,那么消息将显示“没有数据匹配”
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `users` WHERE CONCAT(`id`, `fname`, `lname`, `age`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `users`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "test_db");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP HTML TABLE DATA SEARCH</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="php_html_table_data_filter.php" method="post">
<input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
<input type="submit" name="search" value="Filter"><br><br>
<?php
if($result_validation != ''){
?>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['age'];?></td>
</tr>
<?php endwhile;?>
</table>
<?php
}else{
echo "no data matched";
}
?>
</form>
</body>
</html>
最佳答案
我认为在表单内显示整个表格没有意义,您应该将其显示在表单外的某个位置。话虽如此,$result_validation
变量未定义,您需要在代码中使用 $search_result
。
根据您的问题,使用 mysqli_result::$num_rows
检查从 SELECT
查询返回的行数。
if($search_result->num_rows){
// display table
}else{
echo 'no data matched';
}
关于php - 在 PHP 中使用 IF/ELSE 显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139236/
我是一名优秀的程序员,十分优秀!