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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
MeMoR
  • 95
  • 9
  • 1
    FYI: I wouldn't define variables with 2 underscores, since build in PHP constants use this like: `__FILE__` or `__DIR__` – Rizier123 Jan 30 '15 at 22:27

1 Answers1

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;
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Oh! this worked now. Barmar can u see this Q :http://stackoverflow.com/questions/28200930/auto-increment-not-work-in-php-ignore-method thanks – MeMoR Jan 30 '15 at 22:43