Question

I have a couple questions regarding PHP... please keep in mind I am pretty new to PHP and understand only the very basics.

I would like to know how to condense something this (the countModules is a Joomla function):

    if($this->countModules('Top2A and Top2B and Top2C') == 0) $modRow2 = " hidden";
    if($this->countModules('Top2A or Top2B or Top2C') >= 1) $modRow2 = "";

Would it be something like this?

    if($this->countModules('Top2A and Top2B and Top2C') == 0) {
    $modRow2 = " hidden";
    }else{
    $modRow2 = "";
    }

Or for that matter, could I combine (there are possible columns, and if only two are shown, I need $modCellRow to = "col2"):

    if($this->countModules('Top1A and Top1B') >= 1) $modCellRow1 = "col2";
    if($this->countModules('Top1B and Top1C') >= 1) $modCellRow1 = "col2";
    if($this->countModules('Top1A and Top1C') >= 1) $modCellRow1 = "col2";

into this:

     if($this->countModules('Top1A and Top1B') >= 1) or ($this->countModules('Top1B and Top1C') >= 1) or ($this->countModules('Top1A and Top1C') >= 1) {
     $modCellRow1 = "col2";
     }

Any advice? Am I doing this right?

////// UPDATE ///////

Would all this be correct?

$topModCont1="";
$modRow1="";
$modCellRow1="";
$modRow2="";
$modCellRow2="";
$bottomModCont1="";
$modRow3="";
$modCellRow3="";
$modRow4="";
$modCellRow4="";
$modRowFooter="";
$modCellRowFooter="";

$countModsCont1=$this->countModules('Top1A + Top1B + Top1C + Top2A + Top2B + Top2C');
$countModsRow1=$this->countModules('Top1A + Top1B + Top1C');
$countModsRow2=$this->countModules('Top2A + Top2B + Top2C');
$countModsCont2=$this->countModules('Bottom1A + Bottom1B + Bottom1C + Bottom2A + Bottom2B + Bottom2C');
$countModsRow3=$this->countModules('Bottom1A + Bottom1B + Bottom1C');
$countModsRow4=$this->countModules('Bottom2A + Bottom2B + Bottom2C');
$countModsFooter=$this->countModules('Footer1A + Footer1B + Footer1C');

if($countModsCont1 == 0) { $topModCont1=" hidden"; }
if($countModsCont2 == 0) { $bottomModCont1=" hidden"; }
if($countModsRow1 == 0) { $modRow1=" hidden"; }
if($countModsRow1 == 1) { $modCellRow1="col1"; }
if($countModsRow1 == 2) { $modCellRow1="col2"; }
if($countModsRow1 == 3) { $modCellRow1="col3"; }
if($countModsRow2 == 0) { $modRow2=" hidden"; }
if($countModsRow2 == 1) { $modCellRow2="col1"; }
if($countModsRow2 == 2) { $modCellRow2="col2"; }
if($countModsRow2 == 3) { $modCellRow2="col3"; }
if($countModsRow3 == 0) { $modRow3=" hidden"; }
if($countModsRow3 == 1) { $modCellRow3="col1"; }
if($countModsRow3 == 2) { $modCellRow3="col2"; }
if($countModsRow3 == 3) { $modCellRow3="col3"; }
if($countModsRow4 == 0) { $modRow4=" hidden"; }
if($countModsRow4 == 1) { $modCellRow4="col1"; }
if($countModsRow4 == 2) { $modCellRow4="col2"; }
if($countModsRow4 == 3) { $modCellRow4="col3"; }
if($countModsFooter == 0) { $modRowFooter=" hidden"; }
if($countModsFooter == 1) { $modCellRowFooter="col1"; }
if($countModsFooter == 2) { $modCellRowFooter="col2"; }
if($countModsFooter == 3) { $modCellRowFooter="col3"; }
Was it helpful?

Solution

You might want to assign $this->countModules('Top2A and Top2B and Top2C') to a variable.

That's just for some readability. As for the PHP or statement, we use a double pipe || for that, not the word "or".

Also, note that $this->countModules() using 'and'/'or' in the parameter will return true or false, not the number of modules counted. If you want to count the total number of modules, you should use the '+' operator. (Just read that on http://docs.joomla.org/JDocumentHTML/countModules, so correct me if I am wrong :) )

So at the end of the day...:

$modRow2=""; //give it an initial value, then you don't need an else branch to give it this value, only a single if-statement to change it.  

$modCellRow1="";  //also initialize this, for the same reason

$countMods=$this->countModules('Top2A + Top2B + Top2C'); //count total modules
if($countMods == 0) 
{
    $modRow2 = " hidden";
}
if ($countMods==2) 
{
    $modCellRow1="col2";
}

This code could be cleaner but I thought it might be better to keep it simple and readable instead.

Hope this helps! :)

EDIT:

Your update looks fine (after a quick read) :) Just a side note on the duplicate if-statements though, you could rather use switches instead - they make for cleaner code. For example:

instead of

if($countModsRow1 == 0) { $modRow1=" hidden"; }
if($countModsRow1 == 1) { $modCellRow1="col1"; }
if($countModsRow1 == 2) { $modCellRow1="col2"; }
if($countModsRow1 == 3) { $modCellRow1="col3"; }

use

switch($countModsRow1)
{
   case 0:
      $modRow1="hidden";
      break;
   case 1:
      $modCellRow1="col1";
      break;
   case 2:
      $modCellRow1="col2";
      break;
   case 3:
      $modCellRow1="col3";
      break;
   default:
      //do something if none of these cases are evalutated as being true
      break;
}

It might look like a ton of code, but if your if-statements were to grow for some reason, they might become difficult to manage in the future. Some resource on the switch if you feel the need to read up on it: PHP Switch

EDIT:

You were then correct initial post: to check if there are modules in each position (not worrying about how many there are), you would use something like $this->countModules('Top2A and Top2B and Top2C'). If that evaluates to 0 (false), then there are no modules in those positions, otherwise there is at least one module in EACH position.

If you want to check if there is a module in one or more of those positions, you would use $this->countModules('Top2A or Top2B or Top2C'). If that evaluates to 0 (false), then none of those positions contain modules, otherwise AT LEAST one of then does.

I hope that helps a bit more, sorry for the confusion

OTHER TIPS

if($this->countModules('Top2A and Top2B and Top2C') == 0) $modRow2 = " hidden";
if($this->countModules('Top2A or Top2B or Top2C') >= 1) $modRow2 = "";

you can use like this as well

$modRow2 = $this->countModules('Top2A and Top2B and Top2C') == 0 ? " hidden" : "";

Ternary Operator

You can use if($this->countModules('Top1A') >= 1 && $this->countModules('Top1B') >= 1)) { do what you want }else{}.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top