tags:

views:

1473

answers:

6

I'm trying to use node_load to pass php variables (as variables!) from one node into another, and having no luck.

So in other words, in node/1 I have listed a bunch of variables that I want to be available for php parsing in node/2.

Is it possible?

edit:

Page 1 (www.something.com/node/1) has this text in the body:


$var1 = "Some text";

Page 2 (www.something.com/node/2) has this text in the body:

    
    $node = node_load(1); 
    print $node->body; 
    print $var1; 
  

I want to use node content (as an included file) as if I was using PHP's include function

A: 

I'm not sure that this is possible using node_load, but you can use variable_set and variable_get as follows:

Page 1:

variable_set('var1','Some text');

Page 2:

$getvar = variable_get('var1',NULL);
print "var1 is " . $getvar;
Dan U.
variable_get() and variable_set() are meant for storing system-wide settings, not things that are specific to a single user or page load.
Sean McSomething
Perfect!! Thank you for that Dan U.
Michael D
Just to keep in mind, as Sean points out in his comment and answer, this is more of a hack than a true application-architectural fix.
Dan U.
+1  A: 

It sounds like you're asking about using eval() on the contents of the first node.

I really have to ask -why- you want to do something like this, however. This sort of design really screams out "he doesn't know what he's doing & is completely missing the point". What is this data you wish to store in Node1? Why do you need it in another node? Is there a reason you can't store it as an attribute of that node?

Sean McSomething
This is another answer that bears investigation, and I'll do so when I get a moment to breathe... Thank you...
Michael D
A: 

If you've really got to do something like this, something I would do would be to create a CCK textfield field with unlimited values, perhaps called field_value, and go into the display to hide it from view. Then you could do something like:

$other_node = node_load(1);
print $other_node->field_value[0]['value'];

Although you really should be thinking of different ways of handling this, like through the theme. What are you trying to do with this trick?

John Fiala
Another great idea, and one to investigate soon...
Michael D
A: 

Since the site I'm working on is a non-community, one-off landing page situation, and I'm tremendously pressed for time to find a quick and dirty solution, I've opted to use Dan U's suggestion.

Page 1:

variable_set('var1','Some text');

Page 2:

$getvar = variable_get('var1',NULL);
 print "var1 is " . $getvar;

Briefly, I'm creating in drupal what should rightly be done in PHP stand-alone, but I have no choice about that...

60 nearly identical landing pages with URL specific info, 2 different graphical calendars with event signups, description popups and other bells and whistles. I wanted to do it in VIEWS with an argument, but the popups killed me. I need quite a few elements to load in each page as universal variables, so that editing will be manageable when the inevitable changes come down the line.

Sorry if this is unclear, it's complicated, hard to explain and I'm severely pressed for time...thanks again!

Michael D
Could you not just use blocks for what you want. They can be nativly loaded on every page and still easily editied.
Jeremy French
A: 

You can also use PHP session variables to get that effect.

On page 1:

$_SESSION['variableName'] = 'somevalue';

and on page 2:

<?php print $_SESSION['variableName'] ?>

Drupal does do some odd things when you try to set your own cookie, etc. but the session variables seem to work find. Just remember to clean up after yourself! (set the variable to '' after you print it out or something like that)

Jason

jpamental
A: 

I have a similar sort of issue, but the problem that I cannot insert php into node/1, as that page needs to be updated by a user without php privelages. I have a site-wide block that needs to display the date when node/1 was last updated. I've been contemplating the $node->changed variable, but I'm not sure how to get that variable from a different node. And I don't want to use CCK to add fields or do anything complicated, since this is a once-only temporary block/page, I don't want to waste too much time or effort on it. I'm betting I have to play with the database or something, though. Is there a simple way I can use php to specify which node I want that date from?

-Liisa

Liisa, I can think of several things that could be done, you will probably get a better response if you ask a seperate question for your specific needs.
Jeremy French

related questions