2

I need to pass some variables from one PHP module to another PHP module within a loop and at the same time display the module multiple times as well. I use Joomla sessions for passing variables and its giving me some trouble. Only the last value of the variable gets passed to the second module, meaning only the value attained by a variable when the loop is over is passed to the second module even though I use sessions within the loop.

First Module

for ($i=1;$i<5;$i++) 
{
    $session = JFactory::getSession();
    $session->set('number', $i);

    // calling second module by position

    $modules = JModuleHelper::getModules("second-position");
    foreach($modules as $mod)
      {
         echo JModuleHelper::renderModule($mod, $attribs);
      }
}

Second Module

$session = JFactory::getSession();
$x = $session->get('number');

echo $x;

Output, I get

4 4 4 4

Desired Output

1 2 3 4

What's wrong, and how to correct it? Thanks in advance.

4
  • Dont put JFactory::getSession inside the loop. Put it outside. Not the actual answer but will improve performance
    – Lodder
    Commented Aug 28, 2016 at 4:49
  • Alright. Any idea about how to fix this?
    – saibbyweb
    Commented Aug 28, 2016 at 5:50
  • Hmm, when setting the session value, try replacing $i with $i++, so you end up with $session->set('number', $i++);
    – Lodder
    Commented Aug 28, 2016 at 8:23
  • Now I get only three elements 4 4 4
    – saibbyweb
    Commented Aug 28, 2016 at 8:51

1 Answer 1

1

Ok I see the issue now. It's because you've overriding the session variable in the loop, each time.

Instead, try this:

First module

$session = JFactory::getSession();
$modules = JModuleHelper::getModules('second-position');

for ($i=1; $i<5; $i++) 
{
    $session->set('number', $i);

    foreach($modules as $mod)
    {
        echo JModuleHelper::renderModule($mod, $attribs);
    }
}

Second module

$session = JFactory::getSession();

for ($i=1; $i<5; $i++) 
{
    echo $session->get('number' . $i);
}

Update

Don't put the loop within the loop. You can simply acheive what you want using:

$session = JFactory::getSession();

for ($i=1; $i<5; $i++) 
{
    $session->set('number' . $i, $i);
    echo $session->get('number' . $i);
}
12
  • wooo! Output with this is : 4444 4444 4444 4444
    – saibbyweb
    Commented Aug 28, 2016 at 11:44
  • @Saibbyweb - rejected your edit cause it was incorrect. I've updated my answer. Try again now
    – Lodder
    Commented Aug 28, 2016 at 11:56
  • alright.. now output is : 13 13
    – saibbyweb
    Commented Aug 28, 2016 at 12:17
  • removing $i++ from both modules gives 1234 1234 1234 1234 but its four times my desired output.
    – saibbyweb
    Commented Aug 28, 2016 at 12:35
  • Ok let me do some testing and get back to you. I've never been brilliant with for loops
    – Lodder
    Commented Aug 28, 2016 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.