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

Comments

Boris Mann’s picture

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

print display_block(#blockid);
Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Boris Mann’s picture

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.

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Boris Mann’s picture

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.

sepeck’s picture

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

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

moshe weitzman’s picture

just create a directory in Contrib under /tricks

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Dublin Drupaller’s picture

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 http://www.drupal.org/node/20630
 *
 */

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

grabur’s picture

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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

grabur’s picture

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.

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

jasonwhat’s picture

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?

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

jasonwhat’s picture

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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

jasonwhat’s picture

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.

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

jasonwhat’s picture

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?

divrom’s picture

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

Zach Harkey’s picture

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

: z

Carlos Miranda Levy’s picture

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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Dublin Drupaller’s picture

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

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

freyquency’s picture

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?

Dublin Drupaller’s picture

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.

/**
 * 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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Zach Harkey’s picture

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

: z

freyquency’s picture

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.

Zach Harkey’s picture

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";

: z

Marandb’s picture

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.

Dublin Drupaller’s picture

Hi Marandb

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

Hope that helps...

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

sangamreddi’s picture

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

Dublin Drupaller’s picture

1. go to CREATE CONTENT - PAGE

2. paste in the following php snippet

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

/**
 * 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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

kvarnelis’s picture

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 print($site_slogan) , right??

Dublin Drupaller’s picture

Hi Koolhouse...

here's that snippet

/**
 * 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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Dublin Drupaller’s picture

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

/**
 * 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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Dublin Drupaller’s picture

/**
 * 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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Rick Cogley’s picture

... 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

Dublin Drupaller’s picture

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.

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Rick Cogley’s picture

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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Rick Cogley’s picture

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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Rick Cogley’s picture

... 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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Rick Cogley’s picture

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

Rick Cogley’s picture

... 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

Rick Cogley’s picture

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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

kvarnelis’s picture

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.

/**
* the following displays the site mission
*
*/
print variable_get("site_mission", ""); 
Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

freyquency’s picture

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.

kvarnelis’s picture

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...

jasonwhat’s picture

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

kvarnelis’s picture

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.

Carl Ditzler’s picture

are you using the frontpage module now?

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Dublin Drupaller’s picture

Hi Kooklhouse

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

/**
 * 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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

kvarnelis’s picture

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??

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

kvarnelis’s picture

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.

jasonwhat’s picture

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

Carl Ditzler’s picture

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

bomarmonk’s picture

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!

venkat-rk’s picture

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

Dublin Drupaller’s picture

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.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

freyquency’s picture

Thank you so, so much.

Gunnar Langemark’s picture

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

sepeck’s picture

http://drupal.org/node/23220

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

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

amp-1’s picture

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.

ica’s picture

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

Harry Slaughter’s picture

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 stuff like theme'ing a page, paginate search results, create a configuration/settings panel in the GUI. it's very cool!

--
Devbee - http://devbee.net/

kvarnelis’s picture

hi everyone. i'm embarking on a new drupal project, to turn

http://www.laforum.org into drupal. it's on pmachine now.

the front page now is static. it leads you to a news page that has a front page like list of items.

what i would like is to replicate this in drupal. in other words items "promoted to front page" would actually wind up on http://www.laforum.org/news whereas http://www.laforum.org would have a static page without promoted content.

right now, i have this done via a static, html page and an htaccess line that makes a simple http://www.laforum.org/d go to http://www.laforum.org/d/index.html which in turn references http://www.laforum.org/d/index.php, the real drupal'ized front page.

In other words, I'd like a cleaner solution in which the entire site was in drupal.

my hunch is that this could be done with the static content tossed into front page module and then a news having a snippet on it that replicated traditional front page functionality.

does that make sense?

Dublin Drupaller’s picture

Hi Kvarnelis,

Nice site. I like the simplicity and it's very clean and easy to move around.

Can I recommend the front_page.module?

That will do precisely what you want..i.e. follow the instructions that comes with the module and paste your static content in to the ADMIN-->>SETTINGS-->>FRONT_PAGE.

There are other ways of doing the same using theme tweaks and php, but, it's a very simple and straightforward way of doing it.

I find it useful for sites where there is a membership involved - where for non-members drupal displays "who are we and why you should join" type brochure content, while the front page changes automatically if the member is logged in.

I.e. when a member of your design & architecture forum visits the site, they bypass that brochure type info and goes straight into the "members only area" if that makes sense.

As a tip, if you're migrating from pmachine to drupal, I recommend you migrate the theme you have at the moment using the phptemplate engine. don't be put off by it's name - it is based on php, but it is extremely simple to get to know and tweak. If you're very new to it, simply download a phptemplate that is close to your layout and modify it to look like your existing one.

Hope thet helps..

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

kvarnelis’s picture

hi dub,

thank for the kind words. phptemplate is great. that's exactly how i've replicated the pages at http://laforum.org/d ... it took about four hours to get almost all the functionality down. i estimate a day's total work, which is impressive. the original site took about a month and required lots and lots of hand coding due to the way pmachine works.

i had some problems because of an errant directoryindex command in htaccess that was relentlessly redirecting users to index.html instead of index.php

all is well now, though! thanks.

kvarnelis’s picture

my turn to give back:

php code for an easily extendable random image block...

there are two arrays here, one of images, one of captions. you could also add another one for links or whatever you'd like. directories containing the images can be anywhere you want and you don't have to worry about how many images there are, only that the # of images = the # of captions

$images = array
('folder/blue.jpg','folder/green.jpg',);
$captions = array ('blue','green',);
$random_no = count($images);
$random = $random_no-1;
mt_srand ((double) microtime () * 1000000);
$rnd = mt_rand(0,$random);
$image=$images[$rnd];
$caption=$captions[$rnd];
$memonum=$memonums[$rnd];
echo "<img src=\"$image\" alt=\"$caption\"/>";
echo "<br />";
echo "$caption";
echo "<br />"; 

freyquency’s picture

I'm looking to display a list of all people with the role "staff" and I haven't seen the 'snippet' i'm looking for yet.

Basically it would have to start with something like:

<?php
global $user;
$output = '';
if (in_array('staff',$user->roles)) {}
return $output;
?> 

as shown in this handbook page but then in the IF area would need to be a FOREACH ______ output an html list item with links to the users page. I'm just not sure what to reference with FOREACH. any ideas?

abqaria’s picture

Any efforts to make these php snipetts to work on 4.7 ?

.carey’s picture

I've tried three of these snippets with 4.7.3 and they seem to work fine. I'm using two of them with the frontpage module.

The third one I tried, the taxonomy/vocab snippet, works fine but it's not exactly what I need. It displays everything, which is great if you want that. But what I need is a snippet that displays the latest stories based on taxonomy/vocab with the vocab title included prior to each list (and corresponding teasers) BUT does not include the stories that are stickys. (I hope I'm making sense.)

I'll keep looking. Maybe it's here somewhere.

If anyone can provide a snippet or point me in the direction of one that exists that does this, it would be much appreciated.

Carey

.carey’s picture

<?php
 
    $pageChk = $_GET[""];

    if(empty($pageChk)){
         $listlength="3";
     $charlength="200";
     $taxo_id = 1;
     $content_type = 'story';
     unset ($output);
     $result1 = pager_query("SELECT n.title, n.nid, n.teaser, n.uid, n.created, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC", $listlength);
     while ($node = db_fetch_object($result1)) {
    $output .= "<div class='pagetitle'><h2>" . l($node->title, "node/$node->nid") . "</h2></div>" . substr(strip_tags($node->teaser), 0, $charlength) . "... <br /><br /><em>Submitted by " . $node->name . "</em>";
    }
    $output .= "<br />";
    print $output;

        } else {

     print($content);

    }
   
?>

But I'm getting this error:

user warning: Unknown column 'n.teaser' in 'field list' query: SELECT n.title, n.nid, n.teaser, n.uid, n.created, u.name FROM node n INNER JOIN users u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = 'story' AND term_node.tid = 1 AND n.status = 1 ORDER BY n.created DESC LIMIT 0, 3 in /home/myroot/public_html/mysite/includes/database.mysql.inc on line 120.

*scratching head* Does anyone know what I'm doing wrong?

.carey’s picture

<?php
$listlength="1";
$charlength="150";
$taxo_id = 1;
$content_type = 'story';
unset ($output);
$result1 = pager_query("SELECT n.title, n.nid, nr.teaser, n.uid, n.created, u.name FROM {node} n join {node_revisions} nr on (n.nid = nr.nid and n.vid = nr.vid) INNER JOIN {users} u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC", $listlength);
while ($node = db_fetch_object($result1)) {
$output .= l($node->title, "node/$node->nid") . "<br />" . substr(strip_tags($node->teaser), 0, $charlength) ."...<br /><br />";
}
$output .= "<br /><br />";
print $output;
?>

But I'd like to be able to include the taxonomy/term page title above the listed results.