1

i have created a custom view. its just showing nodes of a particular type. Now i want to do something with it. I am writing a SQL query:

global $user;
$query = db_select('challenges','c');
$query->join('node','n','n.nid = c.cid');
$query->join('field_data_body','f','n.nid = f.entity_id');
$query->join('field_data_field_challenge_pic', 'fp','n.nid = fp.entity_id');
$query->join('file_managed','fm','fp.field_challenge_pic_fid = fm.fid');
$query->fields('n',array('nid','title','created'))
      ->field('f',array('f','body_value'))
      ->field('fp',array('fm','uri'))
      ->condition('c.cid','$user->uid','=');
$result = $query->execute(); 

Where should i place this query in my custom module and how can i get my view to display this? I tried writing this code in a tpl file for the view and then did print_r and render() but no effect. I think it should be done through views.

| improve this question | | | | |
0

write code in your module like this way.

Declaring the block

function mymodule_block_info() {
  $blocks['current_posts'] = array(
    'info' => t('Current posts'), //The name that will appear in the block list.
    'cache' => DRUPAL_CACHE_PER_ROLE, //Default
  );
  return $blocks;
}

Retrieving data

    function mymodule_contents(){
      $query = db_select('challenges','c')
$query = db_select('challenges','c');
$query->join('node','n','n.nid = c.cid');
$query->join('field_data_body','f','n.nid = f.entity_id');
$query->join('field_data_field_challenge_pic', 'fp','n.nid = fp.entity_id');
$query->join('file_managed','fm','fp.field_challenge_pic_fid = fm.fid');
$query->fields('n',array('nid','title','created'))
      ->field('f',array('f','body_value'))
      ->field('fp',array('fm','uri'))
      ->condition('c.cid','$user->uid','=')
        ->execute(); 
      return $query;  
    }

Generating block content

function mymodule_block_view($delta = '') {
  switch($delta){
    case 'current_posts':
      $block['subject'] = t('Posts');
      if(user_access('access content')){
        //Use our custom function to retrieve data.
        $result = mymodule_contents();
        //Array to contain items for the block to render.
        $items = array();
        //Iterate over the resultset and format as links.
        foreach ($result as $post){
          print $post;
        }

        if (empty($items)) { //No content in the last week.
          $block['content'] = t('No posts available.');  
        } 
        else {
          //Pass data through theme function.
          $block['content'] = theme('item_list', array(
            'items' => $items));
        }
      }
    return $block;
  }

}
| improve this answer | | | | |
  • hi...thanks but this is to display a block...i want to create a view – why Feb 12 '13 at 12:31
  • do u mean views page??? – monymirza Feb 12 '13 at 12:36
  • yes...i am trying to create a custom view – why Feb 12 '13 at 12:38
  • are you not using views module??? – monymirza Feb 12 '13 at 12:41
  • what i am trying to achieve cant be done through the views UI...as you may gathered i have created a custom table 'challenges'. Here through a custom form submit action i am entering some values...node id of the page on which the form is, and the user id of the user who clicked the form. Now i want that the user be able to look at all his challenges he accepted by clicking the submit button – why Feb 12 '13 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.