PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How to divide one big function into small functions in php for best practices?

 August 29, 2022     csv, export-to-csv, php     No comments   

Issue

I have created a function exportFile that export file into .csv and .txt for different platforms Amazon, Magento and catch from the database. Since, the function is getting big because of long headers as well as some repeated loops how can I divide this function into separate functions for best practices?

function exportFile ($fileName, $platform, $option) {
    $test_db = new MysqliDb (TEST_DB_HOSTNAME, TEST_DB_USERNAME, TEST_DB_PASSWORD, TEST_DB_DATABASE);
    
    $file = fopen(DOWNLOAD_FILE_PATH.$fileName, 'wb') OR die(json_encode(array("status" => "error", "errMessage" => "Error while downloading file!")));
    if ($file) {
        $query = "SELECT * FROM database";
        $data = $test_db->rawQuery($query);

        switch ($platform) {
               case 'magento':
               $header = array(
                    #csv titles goes here
               );
               fputcsv($file, $header);
               foreach ($data as $rows) {
                  $csvData = array(
                      #csv records from database goes here
                  );
                  fputcsv($file, $csvData);
               }
               break;
               case 'catch':
               $header = array(
                    #csv titles goes here but not same as magento 
               );
               fputcsv($file, $header);
               foreach ($data as $rows) {
                  $csvData = array(
                      #csv records from database goes here but not same as magento
                  );
                  fputcsv($file, $csvData);
               }
               break;
            }
        }
 }

Solution

There are many ways to break things up. Here's a starter

function fileOpen {$filename}
{  return fopen(DOWNLOAD_FILE_PATH.$fileName, 'wb') OR
          die(json_encode(array("status" => "error", "errMessage" => "Error while downloading file!")));
}

function getData ()
{
  $test_db = new MysqliDb (TEST_DB_HOSTNAME, TEST_DB_USERNAME, TEST_DB_PASSWORD, TEST_DB_DATABASE);
  $query = "SELECT * FROM database";
  return $test_db->rawQuery($query);
}

function doMagento ($file, &$data)
{
   $header = array(
        #csv titles goes here
   );
   fputcsv($file, $header);
   foreach ($data as $rows) {
      $csvData = array(
          #csv records from database goes here
      );
      fputcsv($file, $csvData);
   }
}

function doCatch ($file, &$data)
{
   $header = array(
        #csv titles goes here but not same as magento
   );
   fputcsv($file, $header);
   foreach ($data as $rows) {
      $csvData = array(
          #csv records from database goes here but not same as magento
      );
      fputcsv($file, $csvData);
   }
}

function exportFile ($fileName, $platform, $option) {

    $file = fileOpen ($filename);
    if ($file) {
        $data = getData ();

        switch ($platform) {
               case 'magento': doMagento ($file, $data);
                               break;
               case 'catch':   doCatch ($file, $data);
                               break;
            }
        }
 }

One more iteration is required to make a function for csvData. That's for you to do !



Answered By - Rohit Gupta
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing