简体   繁体   中英

Query to get a all tags field values and pour all value in field

UPDATE : i need get a all values of tags field !

MY Query :

$query = db_select('node', 'node');
$query->fields('tagsdata',array('name'));
$query->fields('node', array('nid'));

$query->leftJoin('field_data_field_tags', 'tags', 'tags.entity_id = node.nid');
$query->leftJoin('taxonomy_index', 'tagsindex', 'tagsindex.nid = tags.entity_id');
$query->leftJoin('taxonomy_term_data','tagsdata','tagsdata.tid = tags.field_tags_tid AND node.nid = tagsindex.nid');

$result = $query->execute();

    while( $record = $result->fetchAssoc() ) { 
        $items[] = $record;
    }

AND MY CODE :

    //SORT
   array_multisort(array_column($items, 'nid'), $items);
foreach ($items as $row) {

    $hash[$row[nid]] = $row;
}

$resultfinal = ($hash);
    // END SORT 
foreach($resultfinal as $finalarrays)
      {
         $tags=$finalarrays['name'];
         print_R ($tags);
      }

WITH above code just return one and first value of tags, i need to print all of them !

You can use GROUP_CONCAT mysql function to get all value imploded by comma :

$result = db_query("SELECT tags.entity_id as nid, GROUP_CONCAT(t.name) as tdata  FROM field_data_field_tags 
INNER JOIN taxonomy_term_data t ON t.tid = tags.field_tags_tid 
WHERE tags.entity_type = :type GROUP BY tags.entity_id", 
array(':type' => 'node'))->fetchAllKeyed();

NB : sometimes you have too much string to concat so you need to increase limit before by :

db_query('SET SESSION group_concat_max_len=10000');
$result = db_query("SELECT tags.entity_id as nid, GROUP_CONCAT(t.name) as tdata  FROM field_data_field_tags 
INNER JOIN taxonomy_term_data t ON t.tid = tags.field_tags_tid 
WHERE tags.entity_type = :type GROUP BY tags.entity_id", 
array(':type' => 'node'))->fetchAllKeyed();

https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

foreach($result as $nid => $tags) {
  echo $nid . ' : '.$tags;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM