dougan1465 2014-07-14 16:50
浏览 61
已采纳

在Drupal中的自定义块中显示SQL查询

i have problems when i'm trying to display the result of the following query:

    SELECT COUNT(`entity_id` ) icount, `field_tags_tid`, `taxonomy_term_data`.`name`
    FROM  `field_data_field_tags` 
    INNER JOIN `taxonomy_term_data` 
    ON `taxonomy_term_data`.`tid`=`field_data_field_tags`.`field_tags_tid`
    GROUP BY `field_tags_tid`
    ORDER BY icount DESC 
    LIMIT 0 , 30

in a custom block in Drupal.

Here is my my_tags.module file:

    <?php
    function my_tags_block_info() {
       $blocks = array();

      $blocks['my_first_block'] = array(
        'info' => t('My custom block'),
        // DRUPAL_CACHE_PER_ROLE will be assumed.
      );

      return $blocks;
    }


    function my_tags_block_view($delta = '') {
     if (arg(0) == 'node' && is_numeric(arg(1))) {
        $nid = arg(1);
      };
      $block = array();
      switch ($delta) {
        case 'my_first_block':
          $result = db_query('SELECT COUNT(`entity_id` ) icount, `field_tags_tid`,         `taxonomy_term_data`.`name`
            FROM  `field_data_field_tags` 
            INNER JOIN `taxonomy_term_data` 
    ON `taxonomy_term_data`.`tid`=`field_data_field_tags`.`field_tags_tid`
    GROUP BY `field_tags_tid`
    ORDER BY icount DESC 
    LIMIT 0 , 10');
  $list = array(
    '#theme' => 'links',
    '#links' => array(),

  );
  foreach ($result as $record) {
    $list['#links'][] = array('title' => $record->name , 'name' => $record->icount);
  }
  $block['subject'] = t('Popular tags');
  $block['content'] = $list;
  break;
      }

      return $block;
    }

    ?>

And as a result it is shown only the 'name' field, but i need and the 'icount' field. Thank you.

  • 写回答

1条回答 默认 最新

  • dongyan3562 2014-07-15 14:34
    关注

    Just solved my problem this way:

      function my_tags_block_view($delta = '') {
    
        $block = array();
        switch ($delta) {
          case 'my_first_block':
            $result = db_query('SELECT COUNT(`entity_id` ) icount, `field_tags_tid`,             `taxonomy_term_data`.`name`, `revision_id`
        FROM  `field_data_field_tags` 
        INNER JOIN `taxonomy_term_data` 
        ON `taxonomy_term_data`.`tid`=`field_data_field_tags`.`field_tags_tid`
        GROUP BY `field_tags_tid`
        ORDER BY icount DESC 
        LIMIT 0 , 10');
    
    
      foreach ($result as $record) {
    
        $list[] = l($record->name, 'taxonomy/term/'.$record->field_tags_tid);
    
      }
      dpm($list);
    
      $block['subject'] = t('Popular tags');
      $block['content'] = theme('item_list', array('items' => $list));;
      break;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告