Sunday, May 3, 2009

What's Wrong w/ This Loop?

I am attempting to make a loop that goes through all the tags a user
has provided(comma seperated) and then if it's not already in the tags
table, save it then add it to the event they just added. I can't seem
to get it to work because $tag_id is always the same for all the tags
so only the last tag is saved. Any thoughts?

function __saveTags($tags=array(),$event_id="") {

if(!empty($tags)) {

foreach($tags as $t) {

if($this->Event->Tag->find('count',array("conditions"=>array
("Tag.type"=>"Event","Tag.name"=>$t))) == 0) {

$this->data = array("Tag"=>array("type"=>"Event","name"=>$t));
$this->Event->Tag->save($this->data);
$tag_id = $this->Event->Tag->getLastInsertID();
$this->Event->query("INSERT INTO events_tags (tag_id,event_id)
VALUES('" . $tag_id . "','" . $event_id . "')");

}

}

}

}

--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: