I am trying to remove duplicate entries from tags input string before inserting them into a database but something is wrong in my code. Tags are separated by commas in this example.
form.html
<input type="text" name="tags[usage]" value="{$smarty.post.tags.usage|escape}" />
items.class.php
require_once ROOT_PATH.'/modules/tags.class.php';
$tagsClass = new tags();
foreach($_POST['tags'] as $type=>$tags) {
$arr = explode(',', $tags);
foreach($arr as $tag) {
$tag = trim($tag);
$tag = array_unique($tag);
if($tag != '') {
$tagID = $tagsClass->getTagID($tag);
$mysql->query("
INSERT INTO `items_tags` (
`item_id`,
`tag_id`,
`type`
)
VALUES (
'".intval($itemID)."',
'".intval($tagID)."',
'".sql_quote($type)."'
)
");
}
}
}
I'd appreciate any help.
You're getting a comma seperated string in _POST variable,into this line
foreach($_POST['tags'] as $type=>$tags)
Try below code
$new_array = explode(",", $_POST['tags']);
$new_array = array_unique($new_array);
foreach ($new_array as $value){
//Your code
}