1

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?

1
  • 1
    FYI: I wouldn't define variables with 2 underscores, since build in PHP constants use this like: __FILE__ or __DIR__ Commented Jan 30, 2015 at 22:27

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Oh! this worked now. Barmar can u see this Q :stackoverflow.com/questions/28200930/… thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.