Re: If-else within a foreach cycle

2009-08-21 Thread brian
Right, so delocalizer's suggestion should work. You wouldn't even need to do a find() first. $this->Post->id = $var; $this->Post->saveField('options', $value1); $this->Post->updateAll(array('options'=>value2),array('id <>'=>'$var)); On Fri, Aug 21, 2009 at 8:18 AM, albe wrote: > > @ brian: > > W

Re: If-else within a foreach cycle

2009-08-21 Thread albe
@ brian: What I need is to update existing records, and I've got to assign $value2 as well as $value1... so I need the ELSE branch. However, Thanks for the effort! On 21 Ago, 13:56, brian wrote: > Oops! I take that back. I just re-read the original post. > > On Fri, Aug 21, 2009 at 7:55 AM, bri

Re: If-else within a foreach cycle

2009-08-21 Thread brian
Oops! I take that back. I just re-read the original post. On Fri, Aug 21, 2009 at 7:55 AM, brian wrote: > I don't think albe is trying to update existing records, though. And, > anyway, with your example, $value1 is never assigned. > > On Fri, Aug 21, 2009 at 7:17 AM, delocalizer > wrote: >> >>

Re: If-else within a foreach cycle

2009-08-21 Thread brian
I don't think albe is trying to update existing records, though. And, anyway, with your example, $value1 is never assigned. On Fri, Aug 21, 2009 at 7:17 AM, delocalizer wrote: > > updateAll is the way to go... > eg. $this->Post->updateAll(array('options'=>value2),array('id <> > '=>'$var)); > redu

Re: If-else within a foreach cycle

2009-08-21 Thread delocalizer
updateAll is the way to go... eg. $this->Post->updateAll(array('options'=>value2),array('id <> '=>'$var)); reducing the number of transactions in this case from n(Posts) to just 2 - one for the matching record and one for all the rest. On Aug 21, 8:45 pm, brian wrote: > Almost, but $value_2 will

Re: If-else within a foreach cycle

2009-08-21 Thread brian
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 wrote: > > You don't need the else branch in the 2nd example, its taken care of > in the read: > > /

Re: If-else within a foreach cycle

2009-08-21 Thread rich...@home
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 fe

Re: If-else within a foreach cycle

2009-08-20 Thread albe
Please if anyone has an idea of the reason that could produce this result let me know... I've checked hundreds of times the code. On 20 Ago, 16:26, AD7six wrote: > On 20 ago, 15:39, albe wrote: > > > > > I have to make a foreach cycle to check every item of a certain group. > > For each and eve

Re: If-else within a foreach cycle

2009-08-20 Thread albe
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

Re: If-else within a foreach cycle

2009-08-20 Thread rich...@home
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 writ

Re: If-else within a foreach cycle

2009-08-20 Thread AD7six
On 20 ago, 15:39, albe 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->P

If-else within a foreach cycle

2009-08-20 Thread albe
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 post