How to Show All Categories on System Configuration Field?


One of the more powerful parts of the Magento System is the Admin’s System Config section. This part is essential for every Magento developer. From here learn Magento development step by step.

I hope you already know the system.xml file, please check below field code which we need to add in that file so it will show all store categories.


As you can see we used a frontend_type tag that determines the type of field that will be used to collect the configuration value.

Quick Tip:- How to Filter Out Of Stock Products?

In the source model class, we create one model file which returns all categories options and that model will extend the Mage_Adminhtml_Model_System_Config_Source_Category core class.

class Yournamesspace_Yourmodule_Model_System_Config_Source_Category extends Mage_Adminhtml_Model_System_Config_Source_Category
    public function toOptionArray($addEmpty = true)
        $collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');

        $options = array();        
		$options[] = array(
			'label' => Mage::helper('adminhtml')->__('-- Please Select a Category --'),
			'value' => ''
        foreach($collection as $category){
            if($category->getName() != 'Root Catalog'){
                $options[] = array(
                   'label' => $category->getName(),
                   'value' => $category->getId()
        return $options;

If you want to return the array options then you should need to use the to OptionArray() function.

In that function first, we have created the object of the category and get the collection of it then we just create one loop that job is to collect category labels and ids which are stored into the array which are called options and that options are showing in the system configuration field.

Many inbuilt functions are provided by PHP to get the desired output without additional lines of codes. Generate random numbers in PHP can also be done by using a predefined function of PHP named as RAND ().

This function requires the input of two integers, which should be a range, and it will generate random numbers. Mostly Magento certified developers prefer this to configure.

Syntax of the function:
Min stands for the lowest value of the range from which the system can start the generation.

Max stands for the highest value, which restricts the largest limit to generate a number.

Same is explained with an example:
// Random number generation
$randomNo= rand ();
$randomNo= rand (1,999);



This program will give different output every time the user executes it. It will generate a random number between 1 to 999.

Another inbuilt function is also available for the same work and defined as mt_rand( ) , it is faster than the rand () function.

To achieve the URL encode decode PHP, two inbuilt functions are defined in the PHP which can use directly with URL as a parameter.
It is primarily used to send the data to a web server and alphanumeric characters are replaced with percentage signs followed by two-digit codes.

Urlencode ( )

Both the functions use $ (String), here String indicates the URL, which the user wants to encode or decode.

Output of the Program:

urlencode() =
urldecode() = https:/%2www.

PHP curl get request is used to send a request to receive the data from the server. It could be an html-based web request, API interfaces or other resources.

To send the HTTP Get requests using PHP curl, we need to use the file_get_contents() method.
There are four steps to follow to use the GET request:

  • Initialize the CURL session
  • Options declaration for Curl Session
  • Execute the CURL Session
  • Close the CURL session

Below program will explain all the steps:
$new= curl_init();

curl_setopt($ch,CURLOPT_HEADER, false);





$new= curl_init();

Options definition for Session:
curl_setopt($ch,CURLOPT_URL,$url);- to get the URL
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);- to fetch the header or not
curl_setopt($ch,CURLOPT_HEADER, false); – if this parameter is TRUE , string will be returned instead of output on the screen.



Session closure


To get the desired output, all the steps should be written and executed in the right syntax.

Time Zone inbuilt function:
Inbuilt functions of the library are available to get timezone PHP which is used to get the time zone details from GMT.

The DateTime object is the parameter of the timezone_offset_get() function and returns the seconds in the output on the success or failure of the function.

Syntax of the function is:
Int timezone_offset_get( $object, $datetime)
Both the parameters are mandatory, and the object is the parameter, which defines the DateTimeZone object. Datetime, which specifies the date, or time from which the offset will be calculated.

Timezone_open is used to create the object of DateTimeZone. It is a single-parameter function, which indicates the success or failure and returns the DateTimeZone.

The output will be in seconds of the above program.

Image storage in the database can be done in two ways in PHP by encoding the image into a base64 format or to save the name or path of the image in base64 generated value.

A. Table structure creation:

A table named “Pictures’ ‘ in which two fields are defined as name1 and films1 to store the name of the file and storing the image respectively.
Create table ‘Pictures’ ( id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
‘name1’ varchar(200) NOT NULL,
‘films1’ longtext NOT NULL)Engine=InnoDB default charset=LATIN1

B. Configuration:
Create a new file “Configfile.php” file

$host = “residenthost”; (Host name)
$user = “source”; (User)
$password = “”; (Password)
$dbname = “testing”; (Database name)

$con = mysqli_connect($host, $user, $password,$dbname);
if (!$con) {
die(“Connection failed: ” . mysqli_connect_error());

C. Save path name or File:

An entire path can be saved in the database or the file name can be saved. An image source can be created to fetch the image path or the name of the image from the database.
Below code is written to store the file name in the database:


$name = $_FILES[‘file’][‘name’];
$target_dir = “upload/”;
$target_file = $target_dir . basename($_FILES[“file”][“name”]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$extensions_arr = array(“jpg”,”jpeg”,”png”,”gif”); allowed file extensions

if( in_array($imageFileType,$extensions_arr) ){
$query = “insert into images(name1) values(‘”.$name.”‘)”;

D. Retrieve:

By selecting the name or path of the image saved in the database and use it in image source.

E. Base64_encode:

The entire image can be stored in the database by converting it into the base64 format. Image reference such as name, the path is not required to be stored. Select the value stored in the database to use it in the image source.

About the author

I’m Magento Certified Developer having quite 5 years of commercial development expertise in Magento as well as in Shopify. I’ve worked primarily with the Magento and Shopify e-commerce platform, managing the complexities concerned in building e-commerce solutions tailored to a client’s specific desires.

Related Posts

Leave a Reply