Chris
Chris

Reputation: 1654

Invalid argument supplied for foreach()

I'm trying to get all results from my database. I have an OOP structure set up and created a couple functions in my DB class which get all results from the database.

public function getAll($table, $order) {
     return $this->actionAll('SELECT *', $table, $order);
}

public function actionAll($action, $table, $order) {
        $sql = "{$action} FROM {$table} ORDER BY {$order} DESC";

        if(!$this->queryAll($sql)) {
            return $this;
        }
    }

public function queryAll($sql) {
        $this->_error = false;

        if($this->_query = $this->_pdo->prepare($sql)) {
            if($this->_query->execute()) {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }
    }

In my Page class I'm calling the functions from my DB class and counting the results.

public function get() {
        $data = $this->_db->getAll('pages', 'created');

        if($data->count()) {
            $this->_data = $data->first();
            return true;
        }
    }

Next, in my list PHP file I'm calling the get function from my Page class and creating an foreach loop to loop through the results.

<?php $page = new Page();
$pages = $page->get();

     if(empty($pages)): ?>
            <p>No pages at the moment</p>
        <?php else: ?>
              <?php foreach($pages as $item): ?>
                    <tr>
                        <td><?php echo e($item->data()->label); ?></td>
                        <td><?php echo e($item->data()->title); ?></td>
                        <td><a href="<?php echo BASE_URL; ?>/page.php?page=<?php echo e($item->data()->slug); ?>"><?php echo e($item->data()->slug); ?></a></td>
                        <td><a href="<?php echo BASE_URL; ?>/admin/edit.php?id=<?php echo e($item->data()->id); ?>">Edit</a></td>
                        <td><a href="<?php echo BASE_URL; ?>/admin/delete.php?id=<?php echo e($item->data()->id); ?>">Delete</a></td>
                    </tr>
              <?php endforeach; 
 endif; ?>

The problem is that I'm getting the error Invalid argument supplied for foreach(). I don't really understand why I'm getting this error.

I've tried to fix this, but couldn't think of a solution.

Upvotes: 1

Views: 179

Answers (1)

Scarecrow
Scarecrow

Reputation: 4137

Instead of checking empty($pages) check !is_array($pages).

<?php $page = new Page();
$pages = $page->get();

     if(!is_array($pages)): ?>
            <p>No pages at the moment</p>
        <?php else: ?>
              <?php foreach($pages as $item): ?>
                    <tr>
                        <td><?php echo e($item->data()->label); ?></td>
                        <td><?php echo e($item->data()->title); ?></td>
                        <td><a href="<?php echo BASE_URL; ?>/page.php?page=<?php echo e($item->data()->slug); ?>"><?php echo e($item->data()->slug); ?></a></td>
                        <td><a href="<?php echo BASE_URL; ?>/admin/edit.php?id=<?php echo e($item->data()->id); ?>">Edit</a></td>
                        <td><a href="<?php echo BASE_URL; ?>/admin/delete.php?id=<?php echo e($item->data()->id); ?>">Delete</a></td>
                    </tr>
              <?php endforeach; 
 endif; ?>

Upvotes: 1

Related Questions