html - Display result using IF/ELSE in PHP -
i have sample code problem. want if search "helloworld" want inform user there's no data matched based inputted data. im thinking if can use if else statement validation if data inputted didn't matched rows , if inputted data matched rows. visualized solution problem think method solution don't how can this. think solution put if else condition here's code how thought it
if result of search not nothing show result if nothing message appear "no data matched"
<?php if(isset($_post['search'])) { $valuetosearch = $_post['valuetosearch']; // search in table columns // using concat mysql function $query = "select * `users` concat(`id`, `fname`, `lname`, `age`) '%".$valuetosearch."%'"; $search_result = filtertable($query); } else { $query = "select * `users`"; $search_result = filtertable($query); } // function connect , execute 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 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 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>
i see no point in displaying entire table inside form, should display somewhere outside of form. having said that, $result_validation
variable undefined, need use $search_result
in code.
and per question, use mysqli_result::$num_rows
check number of rows returned select
query.
if($search_result->num_rows){ // display table }else{ echo 'no data matched'; }
Comments
Post a Comment