very very basic question...

How do i find a node number from within the node?

what i am trying to do:

create a link that includes the node number like so:

www.somesite.com/node/[node number]

is there any way to add the node number into a link inside the node without hardcoding it?

Comments

Anonymous’s picture

try something like $node->nid or $edit->nid depends on what your node object is called

paul

Erofee’s picture

I tried both options but neither work.

here is my exact code:
<a href="/cart/add/<?php $edit->nid ?>">Add To Cart</a>
<a href="/cart/add/<?php $node->nid ?>">Add To Cart</a>
(note: you may wonder why i am trying to do this as products in the e-commerce module already contain 'add to cart' links, but this is a small project inside of a bigger project, hence the need to re-create it)

sangamreddi’s picture

This will work, it's working for me.

<?php print $node->nid ?>

Sunny                      
www.gleez.com | www.sandeepone.com

nevets’s picture

From you example, it looks like you are making the link from within a template. So the question is which template. page.tpl.php will have $node defined if the page is showing a node and node.tpl.php will always have $node defined.

A side note, instead of builing the link like you have it would be better to use the l (lower case L) function like this.

<?php
print l(t('Add To Cart'), "cart/add/$node->nid");
?>

That way your link works with or with out clean URLs and will keep working in 4.7

Erofee’s picture

Ok, a little more background:

I creating the link inside the body of the actual node, more specifically a product in the ecommerce module collection. So basically, i am trying to find out the node number from inside the node. I don't want to modify the .module files for one main reason, as soon as a patch or upgrade comes along, i will have to re-modify the files, which is fine for me, but as i host more and more e-commerce sites i will have to change many more files.

i have tried using the following to get the node id, but no result:
'cart/add/$edit->nid'
'cart/add/$node->nid'
'cart/add/print $edit->nid'
'cart/add/print $node->nid'
'cart/add/echo $edit->nid'
'cart/add/echo $node->nid'

(the syntax i am using may be wrong, i have very VERY limited PHP experience)

Anyone have any ideas?

nevets’s picture

First is this in node.tpl.php you are trying to access $node?
And if you examples are all exactly what you tried, they show the problem. All your examples use single quotes, and in PHP strings in single quotes are taken 'as is', no further processing is done. So '$node->nid' comes out $node->nid. If you use double quotes like this, "cart/add/$node->nid" you should either get something like cart/add/34 (if the nid was 34) or cart/add/ (if $node is not defined or set in the context of your code)

Erofee’s picture

I have tried in node.tpl.php and page.tpl.php. Both don't work. I was using double quotes so that is not the problem.

It does come out as "cart/add/" so i think that $node is not defined or set in the code at the time that my code is run.

This leads me to believe that my PHP code in the actual text of the node is being processed before anything else, before the $node variable has a chance to be filled with some data.

Could this be possible? And if so, is there any chance that i can get around it?

nevets’s picture

At this point the best thing if you are willing is to post a copy of node.tpl.php that has the changes that you want. $node should work there so I am unsure what the problem might be.

Erofee’s picture

I think that i may have incorrectly explained what i am trying to achieve. I am trying to call the node ID by placing code inside the body of my post.

Does this clear it up?

nevets’s picture

If you are actually trying to do iit in the body of the node I am not aware of of way to do what you want without a custom module (or you handle it in node.tpl.php iif using a custom template. Without a custom module the iinput fiilter would need to be PHP code but I am not aware of any variable that will get you the nid when the code is interpeted.

Now you could add processing for variables of your own chosing say like %nid% so that code in a module or node.tpl.php replaces those variables with the appropriate value.

Erofee’s picture

Thanks for the help on this matter so far. Can you extend or give a code example for adding my own variable like you have suggested?

Thanks,
Erofee.

nevets’s picture

This snippet will replace variables with context from with in node.tpl.php (so it only works with phptemplates). The existing code will have a line like

<?php print $content ?>

this would be replaced with

<?php
  // First define the variables and there values
  // This example shows the kind of things possible
  //  You may not what all the variables here and need others
  // Variable names are the index to the array
  //  %nid in the content will be replaced with the nid of the node
  global $user;

  $variables = array(
                           '%nid' => $node->nid,
                           '%username' => $user->name,
                           '%site' => variable_get('site_name', 'drupal')
                          );
   print strtr($content, $variables);
?>

This code could also be implement within a module using the nodeapi hook. The simple case would hard code the possible variables above. One could also provide an administrative interface that allows the variables to be choosen/defined.

HyperD’s picture

This initiative is useful but I would like to make possible that the node's number appears into the page where I writing the "Story"/"Page"

I tried to embed PHP into the page, without having results

Setting the content up as PHP, i tried to embed this code:

echo $node->nid;

but in the page doesn't appear any Node Number

nevets’s picture

The node body is unaware of the node so just using $node->nid will not work in the body. You can use $node->nid in node.tpl.php and even page.tpl.php (only for pages with paths of the form node/{nid})

mittalpatel’s picture

I am not sure what exactly you are looking for but if you just want to get the node id on node display page and not getting it through $node->nid then I guess you can use argument fetching.
Use "arg(1)" and it will return the node id of the node currently being viewed.

makes sense?

lake_passage’s picture

In the admin editor, got to structure -> menu -> [usually main menu] -> select the edit function for the page for which you want the node number.

WisTex’s picture

The following code worked for me when placed in a block:

<?php
    if(arg(0) == 'node' && is_numeric(arg(1))){
        $node = node_load(arg(1));
        $user = user_load(array('uid' => $node->uid));
        print "Author: " . l($user->name, 'user/' . $node->uid);
        print " | Creation date: " . date('m/d/y h:i:s', $node->created);
        print " | Publish Status: ";
        print ($node->status) ? "Published" : "Unpublished";
    }
?>
 | Node: <?php print $node->nid ?>