user1794603
user1794603

Reputation:

Foreach invalid argument

I am creating a search function that will allow a user to search for a house in my database by postcode initially. The function can be seen below, when the function is executed and finds a true statement I get no errors however when I execute the search and I get a no fields been returned I am left with this error:

   No Records Found
   Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/undergradpad/search.php on line 26 

I want it to display No Records Found however I don't know how I should correct the above error.

search.php:

<table width="500" border="1" cellpadding="5">
<tr>
<th width="16" scope="row">id</th>
<td width="95">bedrooms</td>
<td width="140">description</td>
<td width="104">roadname</td>
 <td width="71">postcode</td>
</tr>


<?php
    require("classes/class.House.inc");

    $obj = new House();
    $obj->search($_POST['term']);
    foreach($obj->data as $val){
    extract($val);
?>

<tr>
<td scope="row"><?php echo $id; ?></td>
<td><?php echo $bedrooms; ?></td>
<td><?php echo $description; ?></td>
<td><?php echo $roadname; ?></td>
<td><?php echo $postcode; ?></td>
</tr>
<?php
}
?>
</table>

classes/class.House.inc:

 <?php 
 include("connect/class.Database.inc");

 class House extends Database {


    public function read(){

            $query = "SELECT * FROM houses";

            $result = $this->mysqli->query($query);

            $num_result = $result->num_rows;    
            if($num_result > 0){
                while($rows =$result->fetch_assoc()){               
                    $this->data[]=$rows;
                    //print_r($rows);
                }           
                return $this->data;
            }
    }

    public function search ($term){

                    $query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
                    $result = $this->mysqli->query($query);


                    $num_result = $result->num_rows;

                    if($num_result > 0){
                        while($rows =$result->fetch_assoc()){               
                            $this->data[]=$rows;
                            //print_r($rows);
                        }           
                        return $this->data;
                    } else{
                     echo 'No Records Found';    
                        }
    }
 }
 ?>

Upvotes: 0

Views: 300

Answers (3)

Vikas Umrao
Vikas Umrao

Reputation: 2615

if(is_array($obj->data)){
foreach code
}
else{
no record
}

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116110

$obj is a House object. It has no $data property, even if you use it. The search method sets this property, but only if records are found. If no records are found, the method echoes a value.

I would change it like this: Instead of echoing an error, make the method return false:

public function search ($term){

    $query = "SELECT * FROM houses WHERE postcode like '%".$this->mysqli->real_escape_string($term)."%'";
    $result = $this->mysqli->query($query);

    $data = false;

    $num_result = $result->num_rows;

    while($row =$result->fetch_assoc()){               
        $data[]=$row;
    }           
    return $data;
}

Now, the function return an array of false if there is no data. You can now use it like this:

$obj = new House();
if ($data = $obj->search($_POST['term']))
{
  foreach($obj->data as $val){
    extract($val);
  }
}

The changes I made: - No longer set data as a property, since you also return it. You can still do that, if you like, but I think it's confusing to do both. - Return false if there's no data. - Change the variable rows to row, since it only contains one row.

Upvotes: 0

Staff
Staff

Reputation: 933

in this variable ($obj->data) you just get null data.

First check if not empty and than use foreach and don't have error if yout method don't return null data

just check if (!empty($obj->data) { foreach code }

Upvotes: 1

Related Questions