Pablo
Pablo

Reputation: 1435

Display result using IF/ELSE in PHP

I have a sample code for my problem. what i want to do is if i search for "Helloworld" then i want to inform the user that there's no data matched based from their inputted data. Im thinking if can i use if else statement to do a validation if the data inputted didn't matched any rows and if the inputted data matched some rows. As i visualized the solution for this problem i think this method is the solution but i don't how can i do this. i think the solution is to put if else condition here's my code how i thought about it

if the result of search is not nothing then it will show the result then if nothing then the message will appear "no data matched"

<?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>

Upvotes: 0

Views: 1971

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

I see no point in displaying the entire table inside the form, you should display it somewhere outside of the form. Having said that, $result_validation variable is undefined, you need to use $search_result in your code.

And as per your question, use mysqli_result::$num_rows to check number of rows returned from the SELECT query.

if($search_result->num_rows){
    // display table
}else{
    echo 'no data matched';
}

Upvotes: 1

Related Questions