sliced bread snippets: Dynamic main page content without using extra modules...or putting "block" type content in the main page

Dublin Drupaller - April 16, 2005 - 13:57

Hey..

Have been trying to do this for a while: the ability to create dynamic pages without the need to install extra modules or being restricted to adding a new blocks which tend to be only available in the left or right columns.

THE THEORY

Blocks are great. But one of the drawbacks is that when a site begins to grow and more content is added, they tend to end up looking like a "kitchen sink" type link-fest. Where the left & right hand columns of blocks start getting very long and unsightly.....

By "kitchen sink"..I mean sites that are just one massive page full of links to everything and anything.

So, by allowing simpletons & newbies like me (who haven't got an expert eye for php or sql) to be able to generate dynamic main page content..(pulling information from the drupal database in the same way blocks do)...it enables us to move away from kitchen sink drupal sites and increasing the ergonomics and useability of a drupal site for visitors.

Thats the theory...and in practice, what I would like to do is kick off a new thread where we could build up some "sliced bread" php snippets repository for newbies like me..

php page snippet that lists all events in your drupal database

To kickstart the idea, here is a simple snippet I created earlier. It's very basic and very simple but it's an attempt at inserting the "upcoming events" listings that the event block generates and putting that into a page.

To implement it...follow these steps.

1. go to CREATE CONTENT - PAGE

2. paste in the following php snippet

3. Check the PHP code filter and Save the page.

<?php

/**
* Creates a list of all the events in your drupal database
* with a link to each event node. inended as a very simple illustration of what can be done.
*
*/
$gigssql = mysql_query("SELECT * FROM event");  // look in the event table in the drupal database
 
while ($row = mysql_fetch_row($gigssql)) {
   
$timestamp = $row[2];
   
$gigdate= date("D M j G:i ", $timestamp); // formats the date of the event            
   
print "<a href=\"node/$row[0]\">more details</a> --¦--  $row[1] --¦-- $gigdate <br />"; // outputs each event on an individual line with a link to more details.
   
}
?>

Hope that makes sense and nudges others who are as new to sql & php as I am to make the most out of the out-of-the-box drupal download rather than having to install and hack extra modules to get drupal to do what you want to do.

Dub

Blocks are moving out of left and right

Boris Mann - April 16, 2005 - 15:59

The concept of only left and right blocks is changing. They are going to be moving into less specific areas that can be placed anywhere.

I haven't looked at it in detail, but you could potentially write a small script to pull a block out directly. So, still use blocks to store your content, but then just call it in your template wherever you want it.

So, you would still use the code above, but place it in a block. In your template, you could do

<?php
print display_block(#blockid);
?>

aaaah jaysus!

Dublin Drupaller - April 16, 2005 - 16:02

aaaah jaysus! I didn't know that.

Thanks Boris for the heads up..it actually took me ages to work out the snippet I pasted earlier..had no idea it was as simple as how you outlined.

Where is that documented? I spent blinking ages trawling through the docs looking for guidance.

Thanks again..my time spent trawling around wasn't a complete waste of time, I suppose..managed to learn a bit about sql and php in the process.

And I suppose the php snippets might also be useful for others..regardless if they are used in a block or a main page node.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

No, it's not documented or done...yet

Boris Mann - April 16, 2005 - 16:16

Just to be clear: this *isn't* done yet...but no reason it couldn't be. Like I said, I have to go look at the block code, but it should be eay enough to write a helper function (display_block) that you could drop in a template.php file, thus allowing you to use it anywhere in your template.

And yes, you are right: the snippets will always be useful.

point taken..

Dublin Drupaller - April 16, 2005 - 16:52

Thanks Boris,

As an afterthought..what I liked about using the php snippets in a normal drupal page node....is it uses out-of-the-box features from drupal..as opposed to having to introduce extra resources or being tied to having to use extra modules/templates etc.

Am I right in thinking the efficiency of working that way (php snippets in a page calling direct from the drupal database) is slightly more efficient than calling a new function in a template.php file or calling up a module file?

or is the difference so slight it doesn't really make a difference?

Also..how soon do you see the display_block function being implemented?

Cheers.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

I just re-read your original post

Boris Mann - April 16, 2005 - 19:54

And realized that you meant PHP content type pages rather than template level stuff. Yes, PHP content pages are a great way to add interesting functionality, and it is "efficient". The theme/template level stuff is just for things you want to appear on every page.

Something really useful would be a tabular overview of all events for the upcoming year. Look at theme_table for how to properly output it.

Note as well the theme snippets repository. Hmmm....I'm thinking that there are lots of little chunks of code floating around that people might want to re-use. Creating different handbook areas for everything doesn't sound that ideal, but then forum threads definitely aren't ideal.

theme snippets

sepeck - April 16, 2005 - 21:05

theme snippets repositories/theme snippets ideas? Neat tricks with theme's?
Like blocks, someones good idea may be just the thing to generate someone else's.

-sp
---------
Test site...always start with a test site.
Drupal Best Practices Guide

tricks

moshe weitzman - April 30, 2005 - 11:39

just create a directory in Contrib under /tricks

2 column dynamic php page in drupal....

Dublin Drupaller - April 30, 2005 - 08:47

Hi..

Managed to pull together a dynamic page using a standard drupal php page that I thought others might find useful...and was inspired by the way MTV.COMs front page has a funky looking summary of what is new.

The php snippet I have added below is very basic and uses a table to layout the columns...but feel free to play with it..as you please.

Page layout:

!--------------!-------------!
!------site-----!--upcoming--!
!----mission----!---events---!
!--------------!-------------!
!----recent weblog entries---!
!--------------!-------------!

the upcoming events are listed like they are in a block..(event title followed by number of days to go in brackets).

And the page is automatically generated.

To implement the above..simply:

STEP 1: Click on CREATE CONTENT -->> PAGE

STEP 2 : Give the page a title etc. and paste the PHP code below into the body box

STEP 3: Select the PHP CODE filter type.

STEP 4: SUBMIT.

[CODE PASTING DOESN'T WORK ON THIS FORUM. SORRY]

Notes:

1. I have used a simple table to layout the page.
Probably better to use DIVS. For a superb quick reference for multi-column DIV pages...click on the link below.
http://www.thenoodleincident.com/tutorials/box_lesson/boxes.html

2. The upcoming events block will obviously not appear unless you have the events module installed.

3. If you tweak this snippet and come up with other neat ideas or have another snippet, please share. A lot of drupal users are like me and are not very familiar with php instructions. So while the API docs are great....they bamboozle a lot of us.

Hope someone finds the above useful.

before starting this little snippet, I didn't realise you could simply use one line in a php page to display a bock item.

Cheers

Dublin DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

dynamic layout with block type information in a drupal page

Dublin Drupaller - April 30, 2005 - 08:51

Sorry guys...this is the php code you need to paste...something strange going on with posting on the forum today..

Something wouldn't let me post this earlier..

<?php

/**
* This php snippet creates a simple php page that pulls content from the database
*
* If you use it, tweak it or have come up with a new idea that would be of use
* to other drupallers please post it on <a href="http://www.drupal.org/node/20630
" title="http://www.drupal.org/node/20630
" rel="nofollow">http://www.drupal.org/node/20630
</a> *
*/

global $user;

/**
* the following line displays the site mission
*
*/


 
print "<table style=\"WIDTH: 100%\" cellspacing=\"10\" cellpadding=\"10\" border=\"0\"><tbody><tr><td valign=\"top\" width=\"65%\" >";

/**
* the following line displays the site mission
*
*/



print variable_get("site_mission", "");



   print
"</td><td valign=\"top\" width=\"35%\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"1\" bordercolor=\"#eaeaea\">
<tbody><tr><td bgcolor=\"#eaeaea\"><strong>UPCOMING EVENTS</strong></td></tr><tr><td>"
;


/**
* the following displays a list of the 6 upcoming event titles and
* links to their full event nodes. To increase/decrease
* the number of titles displayed..simply change the number 6
* at the end of the statement.
*
*/

 
print event_block_upcoming($limit = 6);


   print
"</td></tr></tbody></table></td></tr></tbody></table>";

/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs. If you want to increase/reduce
* the number of titles displayed..simply change the number 10
* at the end of the SQL statement.
*
*/



 
$output = node_title_list(db_query_range('SELECT DISTINCT(n.nid), n.title, n.created FROM {node} n '. node_access_join_sql() ." WHERE n.type = 'blog' AND n.status = 1 AND ". node_access_where_sql() .' ORDER BY n.created DESC', 0, 10));


print
$output;
?>

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

node_access_join_sql() undefined

grabur - April 30, 2005 - 09:30

Using drupal 4.6.0, I tried taking the weblog part but got:
Fatal error: Call to undefined function: node_access_join_sql() in common.inc

Can you post the drupal version your code works with in the comments, as I've noticed a lot of code isn't working in newer versions.

Ta,
Bill

displaying a list of recent blog titles and links in main page

Dublin Drupaller - April 30, 2005 - 09:44

sorry Bill...

Should have mentioned it was done (php snippet above) for 4.5.x

While it was fresh in my mind...I thought I would just have a look at the code needed for 4.6 and here is the php that calls the recent titles

This PHP snippet displays a list of recent weblog titles in the main page.

Step 1: go to CREATE CONTENT -->> PAGE

Step 2: Give the page a title as normal and paste in the following code.

Step 3: Select PHP CODE as the INPUT FORMAT

Step 4: SUBMIT or PREVIEW the page.

<?php

/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs. If you want to increase/reduce
* the number of titles displayed..simply change the number 10
* at the end of the SQL statement.
*
* This php snippet works with drupal 4.6.
*
*/
$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), 0, 10));
print
$output;
?>

Hope thats of use to others..

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

that's great

grabur - April 30, 2005 - 10:08

I have used this to extract other node types - with the flexinode module. I changed:

WHERE n.type = 'blog'

to
WHERE n.type = 'flexinode-1'

You'll have to check the database tables node and the filed type to find the flexinode content type as it doesn't use the alias you assign.
Cheers.

excellent

Dublin Drupaller - April 30, 2005 - 10:12

Nice one Bantam...by the way...I tried emailing you when I realised I didn't mention the earlier snippet I posted is 4.5. compatible..

Just thought I'd mention that your contact form isn't enabled...

There is another tweak that is handy if a drupal site admin uses his/her weblog as a "news" page...i.e. only list the weblog titles of a certain user..here's the tweak:

<?php

/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs of a certain user.
* If you want to increase/reduce
* the number of titles displayed..simply change the number 10
* at the end of the SQL statement.
*
* If you want to change the user simply change the number 8
* at  n.id=8  in the SQL statement to match the user ID
* you want listed.
*
* works with drupal 4.6.
*
*/


$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.uid = 8 AND n.status = 1 ORDER BY n.created DESC"), 0, 10));
print
$output;
?>

cheers

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

2 pieces of code I'm looking for

jasonwhat - May 14, 2005 - 18:14

Thanks for the tips. Is it possible to display the blogs with teasers? How about recent content that has been "promoted" to front page? Also, any tips on displaying aggregator content?

displaying a list of recent blogs with teasers...

Dublin Drupaller - May 14, 2005 - 18:42

Hi Jason..

To display a list of recent blogs with teasers....

To implement:

Step 1: go to CREATE CONTENT -->> PAGE

Step 2: give the node a title and paste the following into the body

Step 3: Select PHP CODE as the input format

Step 4: SUBMIT or PREVIEW page

<?PHP

/**
* This php snippet displays the 10 most recent weblog entries with 
* teaser & info.
*
* To increase/decrease the number of weblogs listed
* change the list_length field to suit.
*
* Works with drupal 4.6
*/
  $list_length=10;
  $result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created ASC"), variable_get('default_nodes_main', $list_length));

  while ($node = db_fetch_object($result1)) {
    $output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
print $output;

?>

I'm not certain about the aggregator question...maybe someone else out there knows..but, if you scroll down to the following linked post you can refer to the taxonomy relating to the aggregator (I think).

http://drupal.org/node/20630#comment-37361

By the way..please post your snippet up here when it's working....it maybe of use for others..like me who are not experts with php but are trying to do something similar.

HTH

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

thanks for the tip

jasonwhat - May 14, 2005 - 19:32

Any idea how this code would do in 4.5? I have some sites on both versions.

listing recent blogs with teasers...

Dublin Drupaller - May 14, 2005 - 19:46

Implement as you would the one for 4.6. the SQL statement is slightly different...

<?PHP
/**
* This snippet creates a list of the last 5 recent blog entries
* with teasers and info.
*
* To change the number of entries listed..change the $list_length
* value from 5 to something larger/smaller.
*
* This works with drupal 4.5
*/

  $list_length = 5;
  $result = pager_query('SELECT DISTINCT(n.nid), n.created FROM {node} n '. node_access_join_sql() ." WHERE n.type = 'blog' AND n.status = 1 AND ". node_access_where_sql() .' ORDER BY n.created DESC', variable_get('default_nodes_main', $list_length));

  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
  print $output;

?>

Hope that helps Jason..

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Aggregator and sticky

jasonwhat - May 14, 2005 - 23:22

Thanks for the examples. Drupal should have a repository of these code samples somewhere instead of spread all over the site. Anyways, the aggregator issue was answered in an earlier post about how to print a block, so no need to worry about that. I'm also looking to filter by sticky. For example, the front page would have one piece of featured content and that would be the latest piece of sticky content. I suppose this could be done by node type, or just all nodes. I'm guessing the above code would work with some type of if statements added.

I agree..

Dublin Drupaller - May 15, 2005 - 09:46

Someone mentioned on here how to setup a repository...but I've been having difficulties with doing stuff on CVS so am unsure how to get it up and running in a way that is efficient and easy to organise the different snippets..I think the way the block configuration book pages are done in the handbook might be the way to go.

Might start creating pages in there if people think it's worth it and of value.

Someone else mentioned that there are new developments in the pipeline that makes it easier to insert block-type content in to a node. Haven't seen that yet..but, the more I'm getting to grips with how to extract content from the database using php, the more I see the sheer power and flexibility of using standard out-of-the-box drupal features in a better way. i.e. drupal pages with the PHp filter..

You are correct when you say that the php snippets will work with IF, ELSE statements...worth nothing that it is a handy way of generating an http://www.mtv.com style layout...i.e. inserting snippets within tables or Divs in a drupal page with the filter set at PHP allows for very sophisticatd and dynamic node pages - mostly suited to front page type layouts...

Can you dig out that aggregator snippet you mentioned and paste it up here as an example ?

That's the main point of this thread...

cheers

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Aggregator snippet...

jasonwhat - June 8, 2005 - 06:45

is in the repository. It doesn't work with all module blocks as it should, but seems to handle aggregator well.


I'm trying to figure out sticky now. I'd like to display the 5 most unsticky blogs in one spot, and then have a "featured" location for sticky blogs. Any ideas on how to do this one?

I'm sure it's a simple

divrom - February 15, 2006 - 21:55

I'm sure it's a simple change, but how would I get a listing of recent blogs without teasers?

It is simple...

Zach Harkey - February 26, 2006 - 05:19

Change

$output .= node_view(node_load(array('nid' => $node->nid)), 1);

to

$output .= node_view(node_load(array('nid' => $node->nid)), $teaser = FALSE, $page = TRUE, $links = FALSE);

You can change the value of any of those optional parameters for node_view().

$teaser Whether to display the teaser only, as on the main page.

$page Whether the node is being displayed by itself as a page.

$links Whether or not to display node links. Links are omitted for node previews.

-zach
------------------------
harkey design

Code to display latest rss feeds (from all sources)

Carlos Miranda Levy - September 22, 2005 - 14:16

This lists the title of the latest rss feeds with links to their source page within our site...

<?php
  $output 
="Most Recent News Feeds";
 
$listlength='5';
 
$output .= '<div class="item-list"><ul>';
 
$query = "SELECT a.fid, a.iid, a.title, a.timestamp FROM {aggregator_item} a ORDER BY a.iid DESC LIMIT " . $listlength;
 
$queryResult db_query($query);
    while (
$links = db_fetch_object($queryResult)) {
      
$latestfeeds .= "<li>" . l($links->title,'aggregator/sources/'.$links->fid) . "</li>";
    }
 
$output .= $latestfeeds;
 
$output .= "</ul></div>";

  if (
$latestfeeds):
    print
$output;
   else:
    print
"There are no news on this site yet...";
  endif;
?>

New users may feel more comfortable or understand the code better if the l function is presented as:

$latestfeeds .= '<li><a href="/'.url('aggregator/sources/'.$links->fid).'">'. $links->title . '</a></li>';

------
Con paciencia y calma,
sube un burro a una palma

Listing blog (or node type) titles in reverse (most recent last)

Dublin Drupaller - April 30, 2005 - 11:55

To implement:

Step 1: go to CREATE CONTENT -->> PAGE

Step 2: give the node a title and paste the following into the body

Step 3: Select PHP CODE as the input format

Step 4: SUBMIT or PREVIEW page

<?php

/**
* the following displays a list of the 10 most recent weblog titles
* and links to the full weblogs in reverse order (most recent last)
*
* If you want to increase/reduce
* the number of titles displayed..simply change the number 10
* at the end of the SQL statement.
*
* works with drupal 4.6.
*
*/
$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created ASC"), 0, 10));
print
$output;
?>

to change the node type you want listed...just change the following to suit:

n.type = 'blog'

To list all recent blog entries (with teasers and info) use this php snippet instead.

<?php

/**
* the following displays a list of the 10 most recent weblog teasers
* and links to the full weblogs in reverse order (most recent last)
*
* If you want to increase/reduce
* the number of titles displayed..simply change the number 10
* at the end of the SQL statement.
*
* works with drupal 4.6.
*
*/


 
$result = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created ASC"), variable_get('default_nodes_main', 10));

  while (
$node = db_fetch_object($result)) {
   
$output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
 
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
 
$output .= theme('xml_icon', url('blog/feed'));
print
$output;

?>

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

snippet for displaying the most recent event & blog

Dublin Drupaller - April 30, 2005 - 14:07

Hi Guys,

Got an email requesting the following:

This php snippet displays the most recent weblog entry, including teaser and info..and the next upcoming event + teaser and info.

To implement:

Step 1: go to CREATE CONTENT -->> PAGE

Step 2: give the node a title and paste the following into the body

Step 3: Select PHP CODE as the input format

Step 4: SUBMIT or PREVIEW page

<?PHP

/**
* Displays the most recent weblog entry and the next 
* upcoming event with teaser & info.
*
* (Assumes that your event listings = flexinode-1)
*
* Works with drupal 4.6
*/

  $result1 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), variable_get('default_nodes_main', 1));

  while ($node = db_fetch_object($result1)) {
    $output .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
print $output;

$result2 = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'flexinode-1' AND n.status = 1 ORDER BY n.created DESC"), variable_get('default_nodes_main', 1));

  while ($node = db_fetch_object($result2)) {
    $output2 .= node_view(node_load(array('nid' => $node->nid)), 1);
  }
print $output2;


?>

Notes: Any more experienced php coders...who see an easier way of doing this..please post a response up here.

Hope that is of use.

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Listing a certain number of node titles from a certain taxonomy

Dublin Drupaller - May 1, 2005 - 10:16

The following snippet works with 4.5 & 4.6 and simply lists a certain number of node titles from a certain category with links to the full nodes.

To Implement:

Step 1: Go To Create Content -->> Page

Step 2: Give the page a title as normal and paste in the following code in the body & change the two variables to suit...i.e.

$taxo_id = the category number you want to reference
$list_no =the number of node titles you want in the list.

Step 3: Select PHP CODE as the input format and SUBMIT.

<?php

/**
* Creates a list of node titles from a specific category
* with a link to each node.
*
* To change which taxonomy is listed, simply edit the $taxd_id number.
* To change the number of node titles listed, simply edit the $list_no number.
*
* This works with drupal 4.5 and drupal 4.6
*/

$taxo_id = 17;
$list_no =5;
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $taxo_id LIMIT $list_no";
$output .= "<ul>";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
return
$output;
?>

Hope that is of use to others...

Dub

Dublin Drupaller

how about combination of categories and/or vocab?

chromatic - May 20, 2005 - 19:14

I was psyched to try this out but realized that I'd want something like /term/id1+id2
or actually vocab1term1+vocab2term1...
to filter to specific content. (i'm sort of building-my-own articles module - using book and sliced bread)
i'm sure the opposite where the terms get combined together to grab ALL the posts in either would be useful too... I'm really not familiar with mySQL queries or i'd be posting the solution here.... any ideas?

was just thinking that...

Dublin Drupaller - May 20, 2005 - 19:50

Hi Chromatic...

I was actually thinking something very similar...while I'm not an sql expert myself (the snippets have been cobbled together using very basic php/mysql knowledge and pasting from functions in drupal modules) here is something that I just tested and worked...should be easy enough to follow.

there is a probably a better way to do an array of some sort...which will save a few lines...but I don't know how to do that.

<?php
/**
* Creates a list of node titles from specific multiple categories
* with a link to each node.
*
* To change which taxonomies are listed, simply edit
* $taxo_id1 which is the first taxonomy term referenced
* $taxo_id2 which is the second taxonomy term referenced
* $taxo_id3 which is the third taxonomy term referenced
*
* To change the number of node titles listed, simply edit
* the $list_no number.
*
* This works with drupal 4.5 and drupal 4.6
*/
$taxo_id = 1;
$taxo_id2 = 2;
$taxo_id3 = 3;
$list_no =20;
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE (term_node.tid = $taxo_id OR $taxo_id2 OR $taxo_id3) LIMIT $list_no";
$output .= "<ul>";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
return
$output;
?>



Adding more taxonomy terms to the list

If you have more taxonomy terms you want to reference, it should be fairly self explanatory what to add in.

Step 1
Insert new taxonomy references at the beginning of the PHP. e.g.

$taxo_id4=number
$taxo_id5=number
.....etc.

Step 2
then change the piece of code within the SQL statement as follows:

(term_node.tid = $taxo_id OR $taxo_id2 OR $taxo_id3 OR $taxo_id4 OR $taxo_id5)

Hope that makes sense and is of use...if you work out a snappier way of doing it, please post back up here.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

What about order?

Zach Harkey - June 5, 2005 - 00:44

How would return this list in order by creation date, descending? Or can I?

I'm wondering this as well...

chromatic - June 5, 2005 - 03:09

It seems like there are two different types of bread here:

$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC"), 0, 10));

and these:
$taxo_id = 17;
$list_no =5;
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $taxo_id LIMIT $list_no";
$output .= "<ul>";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
</li>

and it seems like only on one script can the asc / descending be added. Is there a place to stick the DESC code in here? It would be very useful to list the last five pages in a category.

Figured it out thru trial

Zach Harkey - June 5, 2005 - 03:45

Figured it out thru trial and error. I changed the relevant line to:

$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $taxo_id AND node.status = 1 ORDER BY node.created DESC LIMIT $list_no";

This is great. I believe

Marandb - June 30, 2005 - 06:32

This is great. I believe that this will pretty much do what I have been lookin to do.

One question. I really need to add the ability to display teasers in the list. How would one go about doing this?

Thanks alot.

here's a link to more snippets..

Dublin Drupaller - June 30, 2005 - 08:40

Hi Marandb

there are a few display with teaser examples in the new php snippets handbook pages I put together.

Hope that helps...

Dub

how to display node title, author and reads

sangamreddi - September 6, 2005 - 15:40

how to Creates a list of node titles, node author, reviews and no of reads from specific multiple categories with a link to each node.

can this be possible? I'm not an sql expert.

Thanks in Advance

Sunny
www.gleez.com

Display a quicklist of active forum topics

Dublin Drupaller - May 16, 2005 - 16:08

1. go to CREATE CONTENT - PAGE

2. paste in the following php snippet

3. Select the PHP code filter and Save the page.

<?php
/**
* Creates a quicklist of recent forum topic titles and a link to their
* node.
*
* works and tested with 4.6
* does not work with 4.5
*
* To increase the length of the list, change $listlength
*
*/


$listlength="10";
$sql = "SELECT n.nid, n.title, l.last_comment_timestamp, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 AND n.type='forum' ORDER BY l.last_comment_timestamp DESC";
       
$sql = db_rewrite_sql($sql);
       
$content  = node_title_list(db_query_range($sql, 0, $listlength), t('Active forum topics:'));

       if (
$content) {
         
$content .= '<div class="more-link">'. l(t('more'), 'forum', array('title' => t('Read the latest forum topics.'))) .'</div>';
        }
print
$content;
?>

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

site slogan / mission statement

kvarnelis - May 17, 2005 - 19:43

Maybe I'm being a dope about this, but i'm trying to come up with a snippet to display the site slogan. What would this be or, better yet, how would I go about finding out about this?

i guess it's NOT

<?php
print($site_slogan)
?>
, right??

PHP Snippet to display the site mission

Dublin Drupaller - May 17, 2005 - 19:57

Hi Koolhouse...

here's that snippet

<?php
/**
* The following line displays the site mission as determined under
*  ADMINISTER -  Settings
*
*/

print variable_get("site_mission", "");
?>

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

PHP Snippet to list the next few upcoming events

Dublin Drupaller - May 17, 2005 - 20:02

While I'm at it...this is a handy one to have on it's own.

<?php
/**
* the following displays a list of the 6 upcoming event titles and
* links to their full event nodes. To increase/decrease
* the number of titles displayed..simply change the number 6
* at the end of the statement.
*
*/
 
print event_block_upcoming($limit = 6);
?>

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

PHP Snippet to display the Site slogan

Dublin Drupaller - May 17, 2005 - 20:09

<?php

/**
* the following displays the site slogan
*
*/
print variable_get("site_slogan", "");
?>

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Thanks but

Rick Cogley - May 17, 2005 - 20:37

... when I do this, it displays the text correctly for all the examples above for 4.6, but I "lose" the css somehow. I get a display that looks like NS4 with no formatting and everything in one ugly column on the left. Any idea how this could be happening?

Rick Cogley :: rick.cogley@esolia.co.jp
Tokyo, Japan

you need to put styles in..

Dublin Drupaller - May 17, 2005 - 21:10

Hi Rick.

You neeed to put styling/tags in...the snippets just pulls the text/content from the database into the page....for example...this basic example simply illustrates how to wrap the site_slogan in a paragraph styled red and a point size of 48 pixels.

<?php
print "<p style=\"color: red; font-size:48px\">";
print
variable_get("site_slogan", "");
print
"</p>";
?>

Obviously you can get more spophisticated...creating WWW.MTV.COM or WWW.YAHOO.COM style pages

In the example linked below..there is a php snippet that illustrates how to put content into a table...as well as TABLE/DIV tags you can put in other STYLE tags as well to make the content appear whatever way you want..

http://drupal.org/node/20630#comment-37248

Hope that makes sense...bottom line is the PHP snippets only pulls the content into the page..you need to tell the page how you want it to look or "wrap" the dynamic content in specific style tags.

please post any of your own php snippets (with or without style tags) that you come up with on this thread with a little description of what it does so others can get ideas/tweak for their own use...

Cheers

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Ah...

Rick Cogley - May 17, 2005 - 21:29

Ah, yes. Styling. Thanks! Devil's in the details, as always. If I manage to create anything halfway decent I will put it up.

Rick Cogley :: rick.cogley@esolia.co.jp
Tokyo, Japan

no worries..

Dublin Drupaller - May 17, 2005 - 21:36

No worries Rick...glad to be of help..its doesn't really matter how simple/sophisticated your php snippet is...plesae post it up here anyway as it maybe just what someone else is looking for - whats simple to you, might be difficult for others (like me!).

I'm very new to php and sql..but I just realised using out of the box features from drupal and a bit of php code, I could achieve an awful lot without hacking, installing and tweaking more modules etc.

So the more snippets that are there to work from the better. Hopefully, someone will compile a snippets page for the handbook - in a similar way to the block examples, so it is easier for people to get at the snippet they need...

Cheers

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Deep in the handbook -

Rick Cogley - May 17, 2005 - 21:41

There is an example page for block PHP snippets in the handbook -

http://drupal.org/node/21867

Rick Cogley :: rick.cogley@esolia.co.jp
Tokyo, Japan

yeah i saw that..

Dublin Drupaller - May 17, 2005 - 21:58

Yeah Rick..I saw that..was thinking that it might be an idea to start a "Using PHP in Drupal pages & the PHP Filter" example page for the handbook...I didn't want to put them under the block examples in case it confused other users..i.e. blocks are associated with lefft/right columns...where as these snippets are intended for use within the main page/node.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

However...

Rick Cogley - May 17, 2005 - 21:37

... even when I put styles in, it just formats that one part, and I still have a style-less page in general, and just the PHP snippet has been formatted. Something's going on here...

Rick Cogley :: rick.cogley@esolia.co.jp
Tokyo, Japan

post your php snippet up here...

Dublin Drupaller - May 17, 2005 - 21:44

Hi Rick...

Post your php snippet up here an I'll have a quick look..assume that you are doing the following:

1. (using an out-of-the-box standard 4.6. install) go to CREATE CONTENT -->> PAGE

2. Give the page a title

3. Pasting your php snippet in the body text area

4. Selecting the PHP filter

5. SUBMITTING the Page.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Just testing now...

Rick Cogley - May 17, 2005 - 21:54

Hi Dub - I was just copying the ones above in this thread. I have other filters enabled and my Drupal 4.6 is therefore not "vanilla" 4.6, so, I might try disabling those and see if it works.

Rick Cogley :: rick.cogley@esolia.co.jp
Tokyo, Japan

Further...

Rick Cogley - May 17, 2005 - 22:19

... when I first tried putting the PHP samples in, just as you describe, I got "munged" content back on the first save and I think that was due to using tinyMCE as the editor. The second save, I was able to re-paste, and the editor got turned off. Then, it returns the right result, but just in a css-less page.

Rick Cogley :: rick.cogley@esolia.co.jp
Tokyo, Japan

A theme problem?

Rick Cogley - May 19, 2005 - 20:58

You know, this is strange. I get the css-less view only when I go directly to the page with PHP in it. When I go to the top page, the PHP result set shows up correctly, within the page framework. It is only when I click the title of that post that I get the css-less view.

That makes me think that my theme (FriendsElectric) and the portion of it that governs the display of single pages as opposed to the front page, are somehow at fault.

Rick Cogley :: rick.cogley@esolia.co.jp
Tokyo, Japan

strange...

Dublin Drupaller - May 20, 2005 - 09:47

Hi Rick,

that sounds very strange...can you post a link to one of the test php pages?

It sounds like something is been added to your links that is forcing you out of the theme.

As a long shot guess, do you have any modules or hacks that might be affecting how links are displayed in nodes?

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

it works!

kvarnelis - May 17, 2005 - 23:01

Great, Dub, the code you posted worked perfectly. Basically, I want to get the front page content at my site varnelis.net to be dynamically generated. The material in the scroll box is dynamic, as is the site mission statement which tops the middle column. Next up to turn some of this other material into dynamic content while making the rest of the site look somewhat like it. This is going to wind up looking pretty different from the standard Drupal installation, I hope!

Next will be bigger and more difficult things! For now, here's a real no-brainer based on your code.

<?php
/**
* the following displays the site mission
*
*/
print variable_get("site_mission", "");
?>

Hey!

Dublin Drupaller - May 17, 2005 - 23:10

that looks very clever..koolhouse. Great idea to have a scrolling box. Moves very smoothly.

Can you post up that snippet?

Are you using a normal scrolling text box DIV or is it javacript/flash based?

Looking good...

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Can we get a documentation page for this?

chromatic - May 18, 2005 - 01:51

This topic and the "help needed with user profile..." topic are getting unwieldy... Can we get a documentation page for this stuff? It's just getting harder to find the replies and follow this thread.

this should do the trick as

kvarnelis - May 18, 2005 - 17:34

this should do the trick as far as the scrolling text box goes. be warned: it's javascript. don't know how to do it in php. http://lists.evolt.org/archive/Week-of-Mon-20020401/108317.html

but now for my new problem. i've already got my blog on the front page, but i want to put blocks of text and links onto my front page as well. how to do that? if you take a look at my site at http://www.varnelis.net, i'm thinking about the middle and right columns. why should the links and text be static? no reason! i should be able to generate them in drupal. but now i'm lost again. i've really tried to look at the code and i just don't know.

i don't like the idea of using primary and secondary links since they appear on every page and i don't want that. i was thinking of making each unit of text its own page and displaying it. but how to do that? i think it's going to be a while before i figure this out...

Flash Map?

jasonwhat - May 19, 2005 - 14:33

How did you get that flash map of where you are integrated? Very cool.

paid $5 to

kvarnelis - May 19, 2005 - 16:43

paid $5 to bryanboyer.com

can anybody point me in the right direction to look re: how to include other page content or block content on the front page, as per my above question? must be some way, but frankly the drupal handbook is still too confusing for me.

are you using the frontpage

quam - May 19, 2005 - 17:31

are you using the frontpage module now?

use the front_page.module

Dublin Drupaller - May 19, 2005 - 19:43

Use the front_page.module with PHP enabled...i.e.

(a) download, install and setup the front_page.module (make sure you follow all the steps in the readme

(b) Paste PHP snippets into the Text area(s)

(c) remember to click on ENABLE PHP before saving your front_page settings.

Hope that helps..

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

PHP SNIPPET: Scrolling box without using javascript

Dublin Drupaller - May 19, 2005 - 19:40

Hi Kooklhouse

Here's the style and php snippet that puts content into a scrolling box without javascript.

<?php
/**
* The following snippet displays the next 10 upcoming events
* in a scrolling box that is set in a simple DIV styled inline.
* To increase or decrease the number of events listed just change
* the $listlength value
*
* no javascript is required and the current
* height, width is 150 X 320 pixels.
* to increase decrease that simply change the values for
* the $boxheight and $boxwidth
*
* works with most popular browsers
* Tested in IE6, Mozilla Firefox 1.0 & 1.5, and NN7
*
* Tested and works with with drupal 4.5.x and Drupal 4.6.x
*
*
*/

$listlength="15";
$boxheight="150px";
$boxwidth="320px";
print
"<h1>Upcoming Events</h1><div style=\"unicode-bidi:bidi-override; direction:rtl; display:block; width:$boxwidth; height:$boxheight; overflow:auto; padding-left:10px; border:1px solid #ba8; \"><div dir=\"ltr\"><p>";
print 
event_block_upcoming($limit=$listlength);
print
"</p></div></div>";
?>

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

well, actually...

kvarnelis - May 19, 2005 - 21:23

hmm... sorry, i guess i wasn't clear, not enough coffee or perhaps too much.

i'm looking for how i would include other content.

i already have my blog on there, what i'd like is the content from two (or more) more nodes, or alternatively two specific blocks. if you look at http://varnelis.net, you can see that there is a blog run by drupal. the site slogan in the middle is run by drupal, the rest of the page is static. how would i take the articles and student work sections on the right and turn them into content generated by drupal. i could stick them in as primary or secondary links, but then those appear on every page, right? and i don't want them there. ok, i could hack a new theme to suppress primary or secondary links for other pages, i guess, but what i'd really like to do is have them as either blocks or nodes.

does that make sense??

if you have categories setup..

Dublin Drupaller - May 20, 2005 - 09:37

Hi Koolhouse...

If you have setup your catgories/taxonomy to organise your nodes...you could use this snippet to call up any list of nodes from a specific category.

http://drupal.org/node/20630#comment-37361

includes instructions on how to change which taxonomy is referenced and how long the list is.

Hope that helps

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

using themes vs. front page module

kvarnelis - May 21, 2005 - 15:40

the front page http://varnelis.net is now entirely driven in drupal (only one other page is though...but that's next). this is a pretty different layout from the regular drupal approach and, for someone new to the CMS, seemed impossible at times.

the key was not to use the front page module, but rather to use the sections module, assign a phptemplate theme to the front page and base that theme on the code that had originally driven my front page.

things i'd still like:

to have tabs on the right column so that more of the content could be there (e.g. you could switch between a list of student work, articles, course syllabi, etc.). this should be easy to do via stylesheets.

make the random image tied to a specific gallery, either from menalto gallery or the built in drupal gallery.

tabs on css

jasonwhat - May 21, 2005 - 16:18

tabs would be interesting. Let me know how that progresses.

Most Popular Snippet? And Admin Options Snippet?

quam - May 18, 2005 - 19:50

Suggestions on creating a list of the x most popular nodes visited? Or x most popular categories visited?

Also looking to create a page including all options for admin (including various settings options), like a sitemap just for admin. Any suggestions?
Thanks

File list from attachment table

bomarmonk - May 20, 2005 - 22:09

I've already mentioned this in another thread (but this does seem the appropriate place to bring this up); I think that a snippet of PHP that displays links to files uploaded with the attachment module would be quite invaluable (I'll be honest: it's an absolute necessity for me).

Allow a way to list these files by each term in a particular vocabulary, and you've got a pretty useful bit of code, especially if it generates links to the file itself, and not merely to the file node.

The problem with the nodelist_module (and other listing modules) is that they generate links that require two clicks to get to a file (one to the node, and a second to reach the file). Also, node_list only creates lists based on a vocab, not individual terms.

There is a module that works with filestore2 that seems promising, but it requires three modules to work (fscache, filestore2, and the file_listing module). I'll wait and see if it gets an update (I look forward to testing a version that works with 4.6).

In the meantime, does anyone have an itch to try to make this happen with a snippet?

Thanks again for any help with this... and sorry if I've been relentless in pursuing this functionality!

Nysus helped me recently

Venkat-Rk - June 5, 2005 - 05:01

Nysus helped me recently with some code that makes the link to the attachment show up directly in the teaser/node view (depending on whether teaser was enabled or not).

I know this doesn't answer your request, but if you know a bit of php (which I don't), you may get some ideas. Here is the thread:
http://drupal.org/node/24067

new handbook pages for this thread

Dublin Drupaller - May 21, 2005 - 17:39

This thread is useful, but, it has become too big and hard to follow..please click through to the new PHP Snippets handbook pages and examples for the key snippets.

Cheers

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Thank you so, so much.

chromatic - May 22, 2005 - 00:11

Thank you so, so much.

Online drupal snippets repository

Gunnar Langemark - June 7, 2005 - 13:42

Just an idea:
When you edit a block or a page/blog/story - could we have a link to a "drupal snippets repository" with all kinds of cool tricks?
Maybe even integrated in such a manner that the code could be automatically added to the page?

Security?
Already done?
Bad idea?

Best
Gunnar Langemark
http://www.langemark.com

you mean...

sepeck - June 30, 2005 - 06:37

http://drupal.org/node/23220

-sp
---------
Test site...always start with a test site.
Drupal Best Practices Guide

Pulling Shout Box module into main page?

amp - July 5, 2005 - 23:39

How would I pull the shout box into the main page instead of a box? Are there any snippets available? I searched and couldn't find any.

Thanks.

A suggestion-integrating snippeds here to frontpage.module

ica - July 21, 2005 - 02:47

It would be nice improvement to Drupal user interface integtrated with -working snippeds described here on this post - that would allow ordinary users customise Drupal sites better and make use of snippeds with ease..

with options of module , node and block integration on the front page.module with an interface
like;

insert -> module-> [pulldownmenu of modules]
insert -> node----> [pulldownmenu of nodes/numbers or short urls]
insert -> block-> [pulldownmenu of blocks]

taking a step further
introduction of columns -maybe with collimator.module integration?

row one |
column one | insert > module-> [pulldownmenu of modules]
----------------insert > node---> [pulldownmenu of nodes/numbers or short urls]
----------------insert > block-> [pulldownmenu of blocks]
column two | insert > module-> [pulldownmenu of modules]
----------------insert > node---> [pulldownmenu of nodes/numbers or short urls]
----------------insert > block-> [pulldownmenu of blocks]

row two |
column one | insert > module-> [pulldownmenu of modules]
----------------insert > node---> [pulldownmenu of nodes/numbers or short urls]
----------------insert > block-> [pulldownmenu of blocks]
column two | insert > module-> [pulldownmenu of modules]
----------------insert > node---> [pulldownmenu of nodes/numbers or short urls]
----------------insert > block-> [pulldownmenu of blocks]
etc.

I hope it makes sense and it is do-able...

- I also posted this suggestion as frontpage.module request
http://drupal.org/node/27178

don't give up the coding!

Harry Slaughter - July 31, 2005 - 21:39

regardless of what features the great drupal team makes available through the GUI, the only way to take full advantage of drupal is to know some level of php.

drupal does a great job of making 95% of what you want doable through the GUI. anyone can have a full fledged "community site" (whatever that means) with no programming knowledge and just some basic web knowledge (and a host). modules allow you to add on more specialized features. but if you really want to add that secret sauce to your mix, that thing that makes your site special, you're probably going to have to code it yourself.

a language like PHP makes it very easy for a programmer to learn through cut/paste development. but as you cut and paste, try to understand the code you're using and how/why it works. at the same time, get yourself a good beginners book on php. within no time, you'll be creating your own modules.

i'm working on my first module, and i'm amazed at how quickly everything can be done and integrated into the drupal site. i don't have to spend any time coding anything but the features i'm actually creating. the drupal api lets me leverage code to do st