Would like to create a block that shows pages that reference the current page's title - Please Help.

pyutaros - May 4, 2007 - 20:58

DISCLAIMER: This is a crosspost and I do apologize. I realized after not getting any response here, http://drupal.org/node/140869, that it was likely because I had posted in the wrong forum category. I have since made some progress, but would like to see if I may inquire for additional help. I will repost the original with a comment noting progress.

I have been trying to learn how to do this but I just can't seem to get my head around this concept. I have tried it in Views. I have tried writing PHP. I have struggled with the database abstraction layer. I just can't seem to grasp it. Here's what I am specifically trying to do.

At http://www.kfol.org/wiki/ I have pages that give instructions on how to do or create things. Each of these pages have a field called PREREQUISITES, which link back to articles describing how to make the prerequisites. For example an article on how to make a peanut butter and jelly sandwich would have a prerequisite of peanut butter, which would link to an article on how to make peanut butter. These links are created by the author when the article is written.

I would like to create a block which dynamically points out possible uses of the product of an article. If peanut butter were listed as a prerequisite for an article, this block would find every instance where peanut butter is listed as a prerequisite and return the page title of those articles. There is a catch here too as I am using revisioning.

This seems like it should be simple, but I just kind seem to find the proper syntax anywhere. Please help me fill in the blanks. Things in {} brackets are things I don't know the syntax for. Numbering is only done for clarity. I think it would go something like this, yes?

<?php
1
) {GET CURRENT PAGE TITLE}
2) SELECT * FROM {TABLE} WHERE {FIELD PREREQUISITES} LIKE "[["{CURRENT PAGE TITLE}"]]"
3) {OUTPUT RESULT}
?>

Really simple I think. I am just a dummy who can't figure it out. Thanks in advance to anyone who can help with this.

Well, even though getting

pyutaros - May 4, 2007 - 21:22

Well, even though getting this far has taken me approx 2 - 3 weeks, I still feel like this was a fairly simple problem. The difficulty was in figuring out where to start looking for answers. Three threads really helped me out in figuring out the majority of my solution:

http://drupal.org/node/74051
http://drupal.org/node/62304
http://drupal.org/node/110845

The first gave me the basics about using the database abstraction layer. The second was link from the first node to a handbook page, Writing secure code. This was good for a conceptual understanding. Finally, the last link had a comment which allowed me to find the title of the current page with some tweaking. http://drupal.org/node/110845#comment-200385

I also google searched some pages on PHP and MySQL and found the proper syntax to eliminate duplicate results and some other things. The biggest challenge was Drupal's internal language though.

Here is the code I currently have for my block.

<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
 
$node = node_load(arg(1));
 
$title = $node->title;
  print
"<h4>$title</h4>";
 
$results1 = db_query("SELECT * FROM {content_type_wiki_node} WHERE field_prerequisites_value LIKE '%[[{$title}]]%' GROUP BY 'nid' ORDER BY NULL");
//  if ($results1 == FALSE) {
//    print "<a href=\"http://www.kfol.org/node/add/wiki-node\">Add new page</a>";
//  }
 
while ( $data1 = db_fetch_object($results1) ) {
   
$value1 = $data1->nid;  
   
$results2 = db_query("SELECT * FROM {node} WHERE nid='{$value1}'");
   
$data2 = db_fetch_object($results2);
   
$value2 = $data2->title;
    print
"<a href=\"http://www.kfol.org/node/$value1\">$value2</a>";
    print
"<br>";
  }
}
?>

I have commented out the section that I believe would give me the last piece of the puzzle. I realize from some troubleshooting that $results1 returns a ref ID regardless of whether the results set is empty. So what I would like to know, if anyone would be gracious enough to share, is how can I check to see if db_query found no results and then print "Add new page"? Thanks once again for any help.

Drupal's so easy, even I could do it.
http://www.kfol.org/

Dont mean to throw a spanner

jmburnz - May 4, 2007 - 22:07

Dont mean to throw a spanner in your works, but I would use taxonomy & views to acheive your goal.

www.sitespring.eu

Thanks for the advice, but

pyutaros - May 5, 2007 - 02:11

Thanks for the advice, but if you read the top of the post you'll see that I've already tried Views for this task and found it too cumbersome and hard to figure out for this. I use it in other parts of the site, for simple Views, but there's not a good enough write up yet for me to get into the complex stuff. I also have Taxonomy configured for my site, but it's implemented more as a folksonomy so it wouldn't be practical for this kind of tracking. If you can explain how to accomplish what I've already done in Views, I would greatly appreciate it. Otherwise, I'll wait to see if someone can tell me how to check for an empty dataset in db_query. Thanks again for the response. I obviously had this posted in the wrong place before as I got zero responses there.
Thanks,
Jonathan
Drupal's so easy, even I could do it.
http://www.kfol.org/

I really am desperate enough

pyutaros - May 5, 2007 - 21:11

I really am desperate enough to keep bumping this to the top.

Drupal's so easy, even I could do it.
http://www.kfol.org/

Have you thought about using

csc4 - May 6, 2007 - 23:16

Have you thought about using CCK and node reference using a view to restrict to prerequisites and creating the node reference - it's then easy to pull that back in a block of related documents - it won't do a dynamic search but then the performance issues on that could become quite large...

Thanks for the reply csc4.

pyutaros - May 7, 2007 - 01:44

Thanks for the reply csc4. Yes. I started out working with CCK and Views, but found it to be less intuitive than just writing the PHP code. If you could explain exactly how to do this in Views, I would greatly appreciate it. Otherwise, I will wait around to see if anyone knows how to write a check for an empty result set from DB_QUERY.
Thanks,
Jonathan
Drupal's so easy, even I could do it.
http://www.kfol.org/

Okay. It works now, but

pyutaros - May 7, 2007 - 14:45

Okay. It works now, but it's ugly. I mean I can actually say my own code makes me want to gag. There are three database calls. I couldn't figure out how to cheeck for an empty dataset without redoing the DB_QUERY, so I end up running the same query on the database twice. For my sake and the sake of anyone who wants to try something similar in the future, please let me know if there is a simpler (read as cleaner) way of doing this. I got the if(empty()) syntax from http://docs.projectopus.com/releases/shout. Here's the final code with comments.

<?php
// only display this block on pages with path of node/# (pages with path alias will still resolve with their original args)
if ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
//get current node's title and print
 
$node = node_load(arg(1));
 
$title = $node->title;
  print
"<h4>$title</h4>";
//check to see if any pages exist with the current node's title in their prereq's field.  If not print link for creating new page
 
$check_results1 = db_query("SELECT * FROM {content_type_wiki_node} WHERE field_prerequisites_value LIKE '%[[{$title}]]%' GROUP BY 'nid' ORDER BY NULL");
 
$check1 = db_fetch_object($check_results1);
  if (empty(
$check1)) {
      print
"<a href=\"http://www.kfol.org/node/add/wiki-node\">Add new page</a>";
  }
//run db_query again to reload data to print out page titles
 
$results1 = db_query("SELECT * FROM {content_type_wiki_node} WHERE field_prerequisites_value LIKE '%[[{$title}]]%' GROUP BY 'nid' ORDER BY NULL"); 
  while (
$data1 = db_fetch_object($results1) ) {
   
$value1 = $data1->nid;  
//cross table db_query to get page title
   
$results2 = db_query("SELECT * FROM {node} WHERE nid='{$value1}'");
   
$data2 = db_fetch_object($results2);
   
$value2 = $data2->title;
    print
"<a href=\"http://www.kfol.org/node/$value1\">$value2</a>";
    print
"<br>";
  }
}
?>

Drupal's so easy, even I could do it.
http://www.kfol.org/

 
 

Drupal is a registered trademark of Dries Buytaert.