user1393064
user1393064

Reputation: 411

cakephp foreach error

when trying to run this code cake is throwing an error with our foreach loop in the view, i will include the model, controller and view. The end goal of this code is to print out the users own invoices and not every invoice in the database.

model

<?php
    class Invoice extends AppModel{ 
    var $name='invoice'; 
    public $useTable = 'invoices';
    public $primaryKey = 'id';}

controller with relevant functions

<?php

class InvoicesController extends AppController{

    public function index(){
            $this->set('title_for_layout', 'indexPage');
        }


public function payinvoice($id = null){
        $this->set('title_for_layout', 'Pay Invoice');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.jpg');   
        $this->layout='home_layout';


        $this->set('invoice', $this->Invoice->read(null, $id));
} 

 public function viewinvoice(){
        $this->set('title_for_layout', 'View invoice');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.jpg');   
}

view file

<table width="100%" border="1">

            <table width="100%" border="1">
                <tr>
                    <th>Biller</th>
                    <th>Subject</th>
                    <th>Date</th>
                    <th>Action</th>
                </tr>
            <?php foreach($invoice as $invoice): ?>
            <tr><td align='center'>
<?php echo $this->Html->link($invoice['Invoice']['biller'], 
array('action' => 'viewinvoice', $invoice['Invoice']['id'])); ;?> </td>
<td align='center'><?php echo $invoice['Invoice']['subject']; ?></td>
<td align='center'><?php echo $invoice['Invoice']['datecreated']; ?></td>
<td align='center'><a href="viewinvoice"><button>View Invoice</button></a><a href="disputeinvoice"><button>Dispute Invoice</button></a></td>
</tr><?php endforeach; ?>

            </table>

Upvotes: 0

Views: 1079

Answers (2)

RichardAtHome
RichardAtHome

Reputation: 4313

Couple of points:

foreach($invoice as $invoice):

should be:

foreach($invoices as $invoice):

As mentioned by a few other people.

But the fundamental problem is that the data structure you are reading into $invoice is one record, not an array of multiple records.

You can do away with the foreach all together:

Upvotes: 2

Dunhamzzz
Dunhamzzz

Reputation: 14798

I spotted a couple of issues with your code, you should clear these up and try again (I don't think the error is on the foreach line like you say it is)

class Invoice extends AppModel{ 
    var $name='invoice'; 
    public $useTable = 'invoices';
    public $primaryKey = 'id';
}

All the properties you are defining here are the defaults, so there's no need to define them, the only one you might need is var $name if you're using PHP 4, otherwise your model could just be:

class Invoice extends AppModel{ }

Next, try not to name your foreach iterator variable the same as the array you are iterating over, this could possibly be the cause your problem. Personally I tend to call the array $items and then each element inside it $item.

<?php foreach($invoices as $invoice): ?>

That way you're not accidentally going to overwrite something in the original array...

Upvotes: 1

Related Questions