Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:43:56+00:00 2026-06-08T23:43:56+00:00

I want to minimize a repeated php code using function, I am a designer

  • 0

I want to minimize a repeated php code using function, I am a designer and now I am trying to learn PHP.
below code check if a Module block is active, and count blocks,

$TopCol1 = (int)($this->countModules('top-col-1') > 0);
$TopCol2 = (int)($this->countModules('top-col-2') > 0);
$TopCol3 = (int)($this->countModules('top-col-3') > 0);

$topColCount = $TopCol1 + $TopCol2 + $TopCol3;
if ($topColCount) : $TopColClass = 'count-' . $topColCount; 
endif;

Then below code will be processed

    <?php if ($topColCount) : ?>
    <div class="row">
        <?php if ($this->countModules('top-col-1')) : ?>    
        <div id="top-col" class="<?php echo $TopColClass ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="top-col-1" style="html5" />
            </div>
        </div>
        <?php endif ?>
        <?php if ($this->countModules('top-col-2')) : ?>    
        <div id="top-col" class="<?php echo $TopColClass ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="top-col-2" style="html5" />
            </div>
        </div>
        <?php endif ?>
        <?php if ($this->countModules('top-col-3')) : ?>    
        <div id="top-col" class="<?php echo $TopColClass ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="top-col-3" style="html5" />
            </div>
        </div>
        <?php endif ?>
    </div>
<?php endif ?>

I need to repeat this type of code many time in my template, please help me make a function that will create the blocks without repeating this codes again and again

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:43:57+00:00Added an answer on June 8, 2026 at 11:43 pm

    While I don’t recommend the syntax, you can put raw HTML code inline inside a php function.

    <?php
    function code_block( $jdoc_name ) {
    global $TopColClass;  //// for variables created outside the function to be visible inside it, we have to include them with "global"
    ?>
    <div id="top-col" class="<?php echo $TopColClass; ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="<?php echo $jdoc_name; ?>" style="html5" />
            </div>
        </div>
    <?php
    }
    
    $MODULES = array(
      'top-col-1',
      'top-col-2',
      'top-col-3',
    );
    
    
    //// ....
    
    
    //// in the page body a foreach loop would also be a good idea:
    foreach( (array)$MODULES as $module_name ) {
        if ($this->countModules( $module_name )) {
            code_block( $module_name );
        }
    }
    
    
    ?>
    

    EDIT, to solve as requested:

    <?php
    function code_block( $jdoc_name ) {
    global $TopColClass;  //// for variables created outside the function to be visible inside it, we have to include them with "global"
    ?>
    <div id="top-col" class="<?php echo $TopColClass; ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="<?php echo $jdoc_name; ?>" style="html5" />
            </div>
        </div>
    <?php
    }
    
    function module( $prefix, $comma_seperated_suffixes ) {
        foreach( (array)explode( ",", $comma_seperated_suffixes ) as $suffix ) {
            $module_name = $prefix.trim($suffix);
            code_block( $module_name );
        }
    }
    
    
    //// shorter call, as requested :)
    module("top-col-", "1,2,3");
    
    
    ?>
    

    Final Edit, transformed into a class

    <?php
    class tovolt{
        function tovolt() {
            //// constructor function - used to setup default variable states, etc. - if this is omitted PHP may have a fit ( depending on version and config )
        }
    
        public static $TopColClass = 'default-value';
    
        function code_block( $jdoc_name ) {
    ?>
    <div id="top-col" class="<?php echo self::$TopColClass; ?> columns">
            <div class="panel">
                <jdoc:include type="modules" name="<?php echo $jdoc_name; ?>" style="html5" />
            </div>
        </div>
    <?php
        }
    
        function module( $prefix, $comma_seperated_suffixes ) {
            foreach( (array)explode( ",", $comma_seperated_suffixes ) as $suffix ) {
                $module_name = $prefix.trim($suffix);
                self::code_block( $module_name );
            }
        }
    }
    
    
    //// calling the class
    tovolt::$TopColClass = 'new-value';                //// if you need to change: $TopColClass
    tovolt::module("top-col-", "1,2,3");
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to minimize a simple linear function Y = x1 + x2 +
I'm new to PHP/MySQL. I want to minimize the number of tables I have
I am trying to minimize my objective function by running iterations and it could
I want to minimize the amount of code i have to write for this
I'm mixing Objective-C and C++. However I want to minimize using of Objective-C++. Because
the last thing I want to minimize today is code that contains while and
I'm trying to build a bookmarklette. I want to minimize the size of the
I want to minimize the amount of code I have to write, and use
To speed up some unit tests that minimize and maximize windows, I want to
Want to run javascript function from parent window in child window Example I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.