Reputation: 95
I need to print tag id from database like this: '1','2','3'
. I have this code :
$_POST['tags'] = "1,2,3";
$arr_tag = explode(",", $_POST['tags']);
$arr_tag = str_replace(' ', '-', $arr_tag);
foreach($arr_tag as $tag)
{
$__SQL__ = data::FETCH("SELECT id FROM " . TAGS . " WHERE name = ?", $tag);
$tags_id[] = $__SQL__[0]['id'];
$quoted_tags = array_map(function ($x){ return "'$x'";}, $tags_id);
$string = implode(',', $quoted_tags);
echo $string;
}
OUTPUT Is:
'126''126','303''126','303','308'
In Action $_POST['tags'] = "1,2,3";
have 3 array value But in output i see 6 value : '126''126','303''126','303','308'
.
how do can i fix this?
Upvotes: 1
Views: 41
Reputation: 782025
The problem is that you're doing the array_map
and implode
inside the loop. So each time you're seeing the running list of results. You should do it just once, after the loop is done:
foreach($arr_tag as $tag)
{
$__SQL__ = data::FETCH("SELECT id FROM " . TAGS . " WHERE name = ?", $tag);
$tags_id[] = $__SQL__[0]['id'];
}
$quoted_tags = array_map(function ($x){ return "'$x'";}, $tags_id);
$string = implode(',', $quoted_tags);
echo $string;
Upvotes: 1