0

I have this function for show tags list :

$DB_QUERY = Access::FETCH("SELECT name, " . NEWS_TAGS . ".id,tags_id,content_id  FROM " . NEWS_TAGS . " JOIN " . NEWS_POSTS_TAGS . " ON 
   " . NEWS_TAGS . ".id = " . NEWS_POSTS_TAGS . ".tags_id WHERE " . NEWS_POSTS_TAGS . ".content_id = ? AND " . NEWS_TAGS . ".type = ? ORDER BY " . NEWS_TAGS . ".name ASC LIMIT 8 ", filter_var($id, FILTER_VALIDATE_INT) , $type);
$ret = array();

foreach($DB_QUERY as $row)
    {
    $ret[] = $row['name'];
    }

$tags = '<li class="label"><a href="#"><i class="fa fa-tag"></i>' . $ret . '</a></li>';

echo $tags;

But i see this error :

Notice: Array to string conversion in C:\xampp\htdocs\frontcms\class\functions.php on line 449

EDIT:

I print result print_r($DB_QUERY) using PHP print_r:

Array(
    [0] => Array(
        [name] => 321[id] => 393[tags_id] => 393[content_id] => 244
    ) [1] => Array(
        [name] => tagname1[id] => 395[tags_id] => 395[content_id] => 244
    ) [2] => Array(
        [name] => tagname2[id] => 394[tags_id] => 394[content_id] => 244
    ) [3] => Array(
        [name] => tagname3[id] => 396[tags_id] => 396[content_id] => 244
    )
)

how do fix this ?!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Perspolis
  • 862
  • 3
  • 11
  • 28

2 Answers2

1

$ret is an array and you use it in $tags. You could for example do

$tags = '<li class="label"><a href="#"><i class="fa fa-tag"></i>' . implode(", ", $ret) . '</a></li>';

which separates all names by a ", ".

But what you actually want to do i guess is:

$tags = "";

foreach($DB_QUERY as $row)
    {
    $tags .= '<li class="label"><a href="#"><i class="fa fa-tag"></i>' .  $row['name'] . '</a></li>';
    }

echo $tags;
PhilMasteG
  • 3,095
  • 1
  • 20
  • 27
1

You don't need to define a new array. You can echo the results directly from the foreach statement:

$DB_QUERY = Access::FETCH("SELECT name, " . NEWS_TAGS . ".id,tags_id,content_id  FROM " . NEWS_TAGS . " JOIN " . NEWS_POSTS_TAGS . " ON 
   " . NEWS_TAGS . ".id = " . NEWS_POSTS_TAGS . ".tags_id WHERE " . NEWS_POSTS_TAGS . ".content_id = ? AND " . NEWS_TAGS . ".type = ? ORDER BY " . NEWS_TAGS . ".name ASC LIMIT 8 ", filter_var($id, FILTER_VALIDATE_INT) , $type);

foreach($DB_QUERY as $row) {
    echo '<li class="label"><a href="#"><i class="fa fa-tag"></i>' . $row['name'] . '</a></li>';
}
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50