0

Containers and there is a Variable "sresult" which is somehow undefined.But i don't understand why , i thought it's possible to pass a Variable to another Div-Container.

This is my fist Div-Container :

<div id = "header">
    <h1>User-Search</h1>
    <?php if(!empty($user)): ?>
    <?php

    echo'<form action="search.php" method="POST">
    <input type="text" placeholder="User Search" name="search_user">
    <input type="submit">
    </form>';

    if(!empty ($_POST['search_user'])):

    $search_user = "SELECT username FROM users WHERE username like :search_user";
    $edited_search_user = "%".$_POST['search_user']."%";
    $suser = $conn->prepare($search_user);
    $suser->bindParam(':search_user', $edited_search_user);
    $suser->execute();
    $sresult = $suser->fetchAll(); 

    ?>
         <?php endif; ?>
        <?php endif; ?>
    </div>

And this is my Second one :

<div id = "textarea">
    <?php
    if(count($sresult)> 0)
    {
        foreach ($sresult as $srow)
        {
            echo'<table>';
            echo'<th>Username : </th>';
            echo'<td>'.$srow["username"].'</td';
            echo'</table>';
        }    

    }
        else {
            echo 'No User found !';
             }

    ?>

    </div>

i tried to put everything together in one Div-Container but even there it didn't worked.

1 Answers1

0

Variable $sresult is defined only when you submit your form (because of !empty ($_POST['search_user'])), so you should check if $sresult exists before doing something with it. This can be done with empty():

<div id = "textarea">
<?php
if(!empty($sresult) > 0)
{
    foreach ($sresult as $srow)
    {
        echo'<table>';
        echo'<th>Username : </th>';
        echo'<td>'.$srow["username"].'</td';
        echo'</table>';
    }    

}
else {
    echo 'No User found !';
}?>
</div>

Function count will not work as it just checks size / number of elements in a variable and doesn't check if it exists. That's why - use empty.

u_mulder
  • 54,101
  • 5
  • 48
  • 64