Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOLVED Hand over array to another component
#1
Hello,

I've got 2 components and I call get_component('b') within component a.
Component b, creates an array, i.e. $features.

Now, I want to use the array $features in component a.

How can I do that? The debug says that $features in component "a" is not defined.
Setting the array to global $features = array(); didn't work.
I defined the code in component b a function (like "function xy()"), that returns the array, but since I have to call the component several times, I got an error, that I can not call the function again (function gets executed only once, then not more).

What am I doing wrong?
If I copy the whole code of component "b" into component "a", everything works fine.
However, I'd rather split things up in different components but have no idea how to "communicate" between them.

I'd be glad if somebody could help me cause I haven't found any solutions while searching the forums.
Thank you very much in advance.
Shasaar
Reply
#2
Did you put global $features in comp b ?
Since components get called as anonymous exec functions it still might not work.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#3
(2017-08-15, 11:35:20)shawn_a Wrote: Did you put global $features in comp b ?
Since components get called as anonymous exec functions it still might not work.

Hello Shawn,

thank you for your message.

Yes, the global is defined in component b.
I did for testing the following in component b:

PHP Code:
$b "Test";
global 
$b

In component a, I do the following:
PHP Code:
get_component('b');
echo 
$b

The error message is:
Notice: Undefined variable: b in /inc/theme_functions.php(544) : eval()'d code on line 5

Cheers.
Shasaar
Reply
#4
Hi

component "b" should be:
PHP Code:
global $b
$b "Test"

component "a":
PHP Code:
get_component('b');
global 
$b;
echo 
$b 
But I doubt that this approach is necessarily correct
Reply
#5
Hi Bigin,

thank you for your message, I'll try it out as soon as possible.
The reason for the handover is, that I let do components sort of operations, which results can be used by other components, too.
Like, you have the calculation of a special price in component b and use the result of that calculation by calling the component in any other component too. If I'd just echo the price, that wouldn't be a prob, but I want to use the result and work with it.
Otherwise I'd have to copy the code to any other component, only to get the result.

Would there be a better approach?

Thanks in advance.
Shasaar
Reply
#6
Hello Shasaar,

yes, a better method is to outsource your process logic in a real (physical) file and only use these functions in your components.
Reply
#7
https://github.com/GetSimpleCMS/guides/b...-13-54.png

[Image: chrome_2017-08-15_10-13-54.png]
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#8
Hello Shawn and Bigin,

thank you very much, it works, even with an array.
That's really great to have.

Putting the whole code in a file would've been possible, but the handling is really awkward.
I have to admit that I am not a programmer and all I do is fiddling with the code until the results are what I need.
The components are amazing to fiddle with things and get an immediate feedback if things work as intended, by just hitting the save button.

I'm sure that a lot of my code and the solutions on how to get things done would be a professionals nightmare, but I have to admit, when I started working with GetSimple, I would have never ever dreamed that I'd be able to get such amazing results with it.
As soon as I'm done with the site, I'll post it here.
Thanks again! You made my day Big Grin.

Cheers.
Shasaar
Reply




Users browsing this thread: 1 Guest(s)
Free HDTV Channels Residential Solar Panels