Almost, but $value_2 will never be assigned. Perhaps the OP could shed some light on the bigger picture as there may be a better way to approach this.
On Fri, Aug 21, 2009 at 5:07 AM, rich...@home<richardath...@gmail.com> wrote: > > 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 <radialremo...@gmail.com> 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" <richardath...@gmail.com> 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 <andydawso...@gmail.com> wrote: >> >> > > On 20 ago, 15:39, albe <radialremo...@gmail.com> 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 cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---