Jump to content

function only returning last pass of foreach


rich_traff

Recommended Posts

I have this function that does 3 things;

 

1) determines how many modules are published to a certain row in a joomla template layout.

2) sets the module width accordingly

3) outputs the html

 

The modules are labeled A through H, what i want returned is the html for every module position to be published, at the moment it only returns the last pass of the foreach loop - so it outputs the html for module H, if i unpublished module H it will only show module G etc…

 

I know it makes sense for that to be happening from looking at the code as it is now but i don't know how to change it so it does what i need…

 

So, i want $result, to return all the html from each pass of the second foreach loop, can anyone help?

 

<?php
function modWidth3($row, $siteWidth, $mod)
{   
   $published = 0;
   $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');
      
   // Calculates how many modules are published to row
   foreach($array as $position)
   {
      if($mod->countModules('topRow' . $row . '_' . $position)) 
      {
         $published = $published+1;
      }
   }
      
   // Sets module width according to number published
   $pixelsUsed = $published * 15;
   $pixelsLeft = $siteWidth - $pixelsUsed;
   $modWidth = $pixelsLeft / $published;
   $result ='';
   
   // Outputs published modules
   foreach ($array as $position) 
   {
  if($mod->countModules('topRow' . $row . '_' . $position)) 
      {
         $result ='
         <div id="topRow' . $row . '_' . $position.'" class="modRow" style="width:'.$modWidth.'px;">
         <jdoc:include type="modules" name="topRow' . $row . '_' . $position.'" style="xhtml" />
         </div>';
      }
   }
   return $result;
}


Link to comment
Share on other sites

each time you loop through the foreach you are effectively writing over the $ result variable.

 

instead of

$result ='         <div id="topRow' . $row . '_' . $position.'" class="modRow" style="width:'.$modWidth.'px;">         <jdoc:include type="modules" name="topRow' . $row . '_' . $position.'" style="xhtml" />         </div>';

 

try

 

$result .='         <div id="topRow' . $row . '_' . $position.'" class="modRow" style="width:'.$modWidth.'px;">         <jdoc:include type="modules" name="topRow' . $row . '_' . $position.'" style="xhtml" />         </div>';

 

This will concatenate the next instance in the loop onto the previous one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.