I'd like to use the active node in the content to define a variable which I can then use for a SQL query. I'm trying to define the variable in the following fashion, but it just comes up blank.


$page = $node->nid;
echo $page;

Note I am trying to use this code in a block and not in my template. In the template, it works just fine.

Any suggestions? I've searched and searched the forums, but can't find a solution.

Thanks in advance for any suggestions.

Regards,

Michael

Comments

nevets’s picture

There is no global node so assuming this is meant for pages that display a single node (node/{nid}), you would first need to get the node, though in your case you only need the node id (nid) so something like this should help

<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $nid = arg(1);
   // Now you can something with the nid
}
?>
mlondon77’s picture

Thanks so much! It worked like a charm.

If you have an extra second, just out of curiosity, what is the arg function?

Thanks again.

nevets’s picture

The arg() function returns the unaliased parts of the path. So if your path was 'node/47/edit', arg(0) would return 'node', arg(1) would return '47' and arg(2) would return 'edit'.

mlondon77’s picture

Thanks for the explanation. I'm sure it'll come in handy in the future.