[eluser]Andrew Ul'din[/eluser]
In my project I use tags for news. I want to write data to database and get tag-string. So i want do it by helper. This is the code of helper:
Code:
function showmetags($id, $type, $tags) {
/* TAGS */
$this->load->database();
$sql = "DELETE FROM `tags_table` WHERE `obj_type`=? AND `obj_id`=?";
$this->db->query($sql, array($type, $id));
$taglist = explode(",", $tags);
$tags_this = array();
$tags_name = array();
foreach ($taglist as $tag) {
$tag = trim($tag);
$sql = "SELECT * FROM `tags` WHERE `tag_value`=? LIMIT 1";
$query = $this->db->query($sql, $tag);
if ($query->num_rows() > 0) {
$row = $query->row_array();
$tags_this[] = $row['tag_id'];
$tags_name[$row['tag_id']] = $row['tag_value'];
} else {
$sql = "INSERT INTO `tags` VALUES (0, ?);";
$query = $this->db->query($sql, $tag);
$lasttag = $this->db->insert_id();
$tags_this[] = $lasttag;
$tags_name[$lasttag] = $tag;
}
}
$tagstring = "";
foreach ($tags_this as $t) {
$sql = "INSERT INTO `tags_table` VALUES (0, ?, ?, ?);";
$this->db->query($sql, array($id, $type, $t));
$tagstring .= "<a href='/news/tags/".$t."/'>".$tags_name[$t]."</a>, ";
}
return $tagstring;
}
code in controller:
Code:
$tagstring = showmetags($id, 'news', $this->input->post('tags'));
but all crash on this string in hepler:
Code:
$this->db->query($sql, array($type, $id));
Can't do query to database. Why? How i can do it from helper? If code write in controller - all work! From helper - no.
p.s. sorry for my english