DB query in for loop errors on second loop (PHP, Codeigniter) -
a simple loop loops through array , deletes database records...
controller:
foreach ($deletedtags $deletedtag) { $tagid = $this->tags_model->get_tag_id($deletedtag); $this->tags_model->delete_tag_association_by_tag($workid, $tagid); }
$deletedtags
array, example being:
array ( [0] => purple [1] => trees [2] => green )
model:
function get_tag_id($tag) { $this->db->where('tags.name', $tag); $query = $this->db->get(self::table); return $query->row()->id; }
when there 1 value in $deletedtags
works fine. when there's more 1 value model function get_tag_id($tag)
breaks on second loop. errors on line return $query->row()->id;
with:
undefined property: stdclass::$id
any idea why?
i've solved it. reason it's erroring because can find first tag in array in db (even though other tags exist in db). added tag_exists check , fine...
foreach ($deletedtags $deletedtag) { if ($this->tags_model->tag_exists($deletedtag)) { $tagid = $this->tags_model->get_tag_id($deletedtag); $this->tags_model->delete_tag_association_by_tag($workid, $tagid); } }
weird.
Comments
Post a Comment