Community

Undefined variable: node in include_once()

Hello there,

I'm trying to do the following:

$sql_tid = db_query('SELECT tid FROM taxonomy_index WHERE nid = :nid', array(':nid' => $node->nid));

But it doesn't seem to get the nid right because I get the following notice:

Notice: Undefined variable: node in include_once() (line 3 of mymodule/mymodule.module).
Notice: Trying to get property of non-object in include_once() (line 3 of mymodule/mymodule.module).

What am I doing wrong? And is this the right approach?
I'm new to Drupal and don't fully understand how to use its framework.

Comments

There is no global $node.

There is no global $node. What are you trying to achieve and what is the scope of your code (where is it placed)?

Thank you nevets for your

Thank you nevets for your reply!

However this is the code I've made so far:

$previous = $node->nid - 1;

$sql = "SELECT taxonomy_index.nid, node.title, taxonomy_term_data.`name`
FROM taxonomy_index
INNER JOIN node ON taxonomy_index.nid = node.nid
INNER JOIN taxonomy_term_data ON taxonomy_index.tid = taxonomy_term_data.tid
WHERE node.nid =" . $previous . ";";

$query = mysql_query($sql);
$link = mysql_fetch_array($query);

And for a back url:
echo '<a src="www.cycletagging.com/' . $link['name'] . '/' . '$link['title']' . '">back</a>'

What this code does is to create a back button so that it browses back through nodes with a specific term on it. It only prints nids with the same taxonomy term and it only browses through lower nids. It's not allowed to print nids higher then the current nid.

I don't have a clue how to make this code Drupal compatible. I tried to find information and got some code right with Drupal but it still doesn't work. :(

Any help would be appreciated! :)

Why not use views with a

Why not use views with a pager?

mysql_query? We use PDO in

mysql_query? We use PDO in Drupal 7.
And your code needs some changing to make it the Drupal way.

As nevets mentions, I second that you might want to go for Views module.

Plus, there is no $node global ($user do).
If you want to get current node's node ID from URL, use arg(1)

// Ayesh

I tried Views, added a

I tried Views, added a contextual filter to get current node ID from URL.
And it kind of works, the thing is, when you hit back it goes back to the previous page (like I want to). But if you're on that page (with a lower nid) and hit 'back' again it goes back to the latest page (with a higher nid).

That's why I decided to write a query on my own.

What did you sort the view

What did you sort the view by?

I've made a screenshot of my

I've made a screenshot of my created View. Excuse the Dutch language:
http://www.cycletagging.com/backbutton-views.png

Note: Contextual filter "Content: Nid" (Inhoud means Content) is checked as excluded so the current nid url will not be shown.

this is my code now, 'trying'

this is my code now, 'trying' to change it to Drupal standards:

global $node;
$previous = $node->nid - 1;

$sql = "SELECT taxonomy_index.nid, node.title, taxonomy_term_data.`name`
FROM taxonomy_index
INNER JOIN node ON taxonomy_index.nid = node.nid
INNER JOIN taxonomy_term_data ON taxonomy_index.tid = taxonomy_term_data.tid
WHERE node.nid =" . $previous . ";";

$link = db_query($sql)->fetchAssoc();

I included 'global $node' to define the variable $node.
It still gives an error saying:
Notice: Trying to get property of non-object in include_once()

Ayesh, what do you mean there's no $node global, is there any other workaround then?
Thanks!

Anybody? please?

Anybody? please?

Sorry I'm reply just

Sorry I'm reply just now.
This is a little trick to load the node from the node ID from URL. Even if you have a path alias, internal path of the node is still node/X where X is always numeric.

arg() function gives specific part of the internal URL.

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
 
$node = node_load(arg(1));
 
$previous = $node->nid - 1;
 
 
$sql = "SELECT taxonomy_index.nid, node.title, taxonomy_term_data.`name`
  FROM taxonomy_index
  INNER JOIN node ON taxonomy_index.nid = node.nid
  INNER JOIN taxonomy_term_data ON taxonomy_index.tid = taxonomy_term_data.tid
  WHERE node.nid ="
. $previous . ";";
 
 
$link = db_query($sql)->fetchAssoc();
}
?>

However, as you are trying to get the previous node ID, you can do it like this as well:

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(1) - 1)) {
 
$node = node_load(arg(1));
 
$previous = arg(1) - 1;
 
 
$sql = "SELECT taxonomy_index.nid, node.title, taxonomy_term_data.`name`
  FROM taxonomy_index
  INNER JOIN node ON taxonomy_index.nid = node.nid
  INNER JOIN taxonomy_term_data ON taxonomy_index.tid = taxonomy_term_data.tid
  WHERE node.nid ="
. $previous . ";";
 
 
$link = db_query($sql)->fetchAssoc();
}
?>

Still your code has a SQL injection issue. try this:

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(1) - 1)) {
 
$node = node_load(arg(1));
 
$previous = arg(1) - 1;
 
 
$sql = "SELECT taxonomy_index.nid, node.title, taxonomy_term_data.`name`
  FROM taxonomy_index
  INNER JOIN node ON taxonomy_index.nid = node.nid
  INNER JOIN taxonomy_term_data ON taxonomy_index.tid = taxonomy_term_data.tid
  WHERE node.nid =:nid;"
;
 
 
$link = db_query($sql, array(':nid' => $previous))->fetchAssoc();
}
?>

give a try to the last snippet. I didn't test the sql itself but $previous should contain the previous node ID now and your sql execution should be secure according to the "Drupal way" :)

// Ayesh

Sorry for my late reply, got

Sorry for my late reply, got some family stuff going on.

I tried the snippet you provided but unfortunately it doesn't seem to work.
Like you said, you use the arg() function to get the internal URL so you use the arg() function in your snippet.

To get the results of that snippet of yours I did the following:

function hetblok_contents() {
$url = arg();
var_dump($url);
return '<a href="' . $url . '">back</a>';
}

I made a new function (it's part of my other code, everything in hetblok_contents will be placed as a block) and let it print out the url with the results of arg(). It gives no errors but the url looks like this:
domainname.com/currenttaxonomyterm/Array

If for example I do this (silly, I know, but just to test out):
return '<a href="' . $url['bla'] . '">back</a>';
It gives the current page url but not the previous one.
Maybe it's a small problem, but any clue what i'm doing wrong?
And what about $link?

Thank you!

Oh, and some more information

Oh, and some more information that might come in handy:

var_dump($url);
gives a result:
array(2) { [0]=> string(4) "node" [1]=> string(2) "36" }

36 is the number of the current node.

arg() function, if called

arg() function, if called without any arguments, will return an array of all URL parts, excluding the base path. If you call arg() with an argument, (arg(n) for example), it will return a string with the n-th part of the URL.

See some useful function that you might want to use to get the URL:
l() - format an a tag with a Drupal path
url() - Get the aliased and basepath-resolved URL or path.

// Ayesh

Thank you Ayesh for pointing

Thank you Ayesh for pointing me to the right direction. The API page of Drupal come in handy!
Actually i've got it working but it's not completely what I want.

What happens now is that I can hit 'Back' and it will go to the previous node.
What I want is that it only goes to a lower node number that has the same taxonomy term then the current node you are viewing.

Also, what if you're on node 39 and the previous nid is 37. The current code brings me to nid 38, but that nid doesn't exist anymore or has another taxonomy term.

I know that $previous = arg(1) - 1; does that but is there a way to do this differently and let it go to the next previous nid number within a term? Could there be a special Drupal function for that or is there a query way of achieving this?

The full code I have now:

/**
* Implements hook_block_info().
*/
function backbutton_block_info() {
  $blocks['my_block_id'] = array(
'info' => t('The back block'),
    'visibility' => BLOCK_VISIBILITY_LISTED,
  );

  return $blocks;
}

/**
  * Implements hook_block_view().
  */
function backbutton_block_view($delta='') {
  $block = array();

  switch ($delta) {
    case 'my_block_id':
      $block['subject'] = t('Block Name');
      $block['content'] = hetblok_contents();
      break;
  }

  return $block;
}

function hetblok_contents() {
if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(1) - 1)) {
  $node = node_load(arg(1));
  $previous = arg(1) - 1;

  $sql = "SELECT taxonomy_index.nid, node.title, taxonomy_term_data.`name`
  FROM taxonomy_index
  INNER JOIN node ON taxonomy_index.nid = node.nid
  INNER JOIN taxonomy_term_data ON taxonomy_index.tid = taxonomy_term_data.tid
  WHERE node.nid =:nid;";

$link = db_query($sql, array(':nid' => $previous))->fetchAssoc();
}
$my_url = url( 'node/' . $previous, array('absolute' => true ));
return l(t('Back'), $my_url);
}

Once again, thank you for your patience. Ordered "Pro Drupal 7 Development" to learn more about Drupal programming. :)
Heard it's a good book!

Cheers,
Cesar

I couldn't touch Taxonomy

I couldn't touch Taxonomy system in a while but there should be a table term_node that lists term <=> node relationships that you might want to ORDER BY the nid and have same tid from the current node.

// Ayesh

I noticed that you used :nid

I noticed that you used :nid to get the current nid in the piece of query. Assuming this is the 'Drupal way', is there something the same with the current tid? Like, for example :tid?

Can't find anything about it in the Drupal documentation.

Kind regards,
Cesar

Forget what I said, figured

Forget what I said, figured it out myself. Didn't know the use of query placeholders.
Now i'll try to make my back button work with the ORDER BY suggestion from your latest post.

Ayesh, I love you! :) I've

Ayesh, I love you! :)
I've got it perfectly working now by adding ORDER BY to the (rewritten) query.

This is the result of the code:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  $nid = arg(1);
  $tid = $node->field_tags['und'][0]['tid'];

$result = db_query('SELECT tid, nid FROM taxonomy_index WHERE nid < :nid AND tid = :tid ORDER BY nid DESC LIMIT 1', array(':nid' => $nid, ':tid' => $tid))->fetchField($index = 1);
}

$my_url = url( 'node/' . $result, array('absolute' => true ));
return l(t('Back'), $my_url);

Thanks a million for your patience and being such a great help.
I really appreciate it!

Cheers,
Cesar

No prob:) great that you

No prob:) great that you could finally do that.
All the best!

// Ayesh

nobody click here