You don't need the else branch in the 2nd example, its taken care of
in the read:

// fetch back the record with an id of $var
$post = $this->Post->read(null, $var);

// update the record
$post['Post']['options'] = value1;

// save the record
$this->Post->save($post);

In the first example, you fetch back a number of records and loop
through them looking for one with an id of $var. It's quite likely
that id will be unique (its should be a primary key) so there's no
need for the loop.


On Aug 20, 5:21 pm, albe <[email protected]> wrote:
> foreach($posts as $p) {
>
>         if ($p['Post']['id'] == $var) {
>           $p['Post']['options'] = value1;
>         } else {
>           $p['Post']['options'] = value 2;
>         }
>         $this->Post->save($p);
>
> }
>
> This works! I just can't say how much I am grateful for your help!
> However I still don't understand why my code was wrong...
>
> About your second way:
>
> $post = $this->Post->read(null, $var);
> $post['Post']['options'] = value1;
> $this->Post->save($post);
>
> If I use this, where shall i put the ELSE branch?
>
> On 20 Ago, 18:00, "rich...@home" <[email protected]> wrote:
>
>
>
> > Shouldn't that be:
>
> > foreach($posts as $p) {
>
> >         if ($p['Post']['id'] == $var) {
> >           $p['Post']['options'] = value1;
> >         } else {
> >           $p['Post']['options'] = value 2;
> >         }
> >         $this->Post->save($p);
>
> > }
>
> > also, if you are just matching against the id, you could just write it
> > as:
>
> > $post = $this->Post->read(null, $var);
> > $post['Post']['options'] = value1;
> > $this->Post->save($post);
>
> > and forget about the loop
>
> > On Aug 20, 3:26 pm, AD7six <[email protected]> wrote:
>
> > > On 20 ago, 15:39, albe <[email protected]> wrote:
>
> > > > I have to make a foreach cycle to check every item of a certain group.
> > > > For each and every item I've got to check a condition and make a data
> > > > modification depending on this condition.
> > > > My code is the following:
>
> > > > foreach($posts as $p) {
>
> > > >         $this->Post->id = $p['Post']['id']; //consider the posts one
> > > > by one
>
> > > >         if ($this->Post->id == $var) {
> > > >           $this->data['Post']['options'] = value1;
> > > >         } else {
> > > >           $this->data['Post']['options'] = value 2;
> > > >         }
>
> > > >         $this->Post->save($this->data);
>
> > > > }
>
> > > > Here is my problem: the modifications are performed correctly for the
> > > > IF branch, but NEVER for the else branch.
> > > > Do you know why this occurs?
>
> > > aliens?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to