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;
}


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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.