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

  • SEARCH
  • Home
  • 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 6033977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:33:43+00:00 2026-05-23T05:33:43+00:00

I am by no means asking for anyone to re-write my code, but rather

  • 0

I am by no means asking for anyone to re-write my code, but rather find places where I can improve it, or implement better practices. This is how the function briefly works.

Function $Class->getTaxClass() accepts a minimum of 1 argument, which can either be a single ID, or an array of ID’s. It can also than accept values for specific rows which it wants to grab like such $Class->getTaxClass($array, 'name','tid')

So I am really just looking for ways to improve the code structure / best practices / logic of the function, which is as follows:

public function getTaxClass()
{
  $arg = func_get_args();
  $or = 'pa.pid = ?';
  if(is_array($arg[0]))
  {
    $i = 1; 
    while($i < count($arg[0]))
    {
      $or .= " OR pa.pid = ?";
      $i ++;      
    }
  }
  if(count($arg) == 1)
  {
    $pid = $arg[0];
    $row = "a.*";
  }
  else if(count($arg > 1))
  {
    $pid = array_shift($arg); 
    $prepared_args = array();
    foreach($arg as $a) {
      $prepared_args[] = "a." . $a;
    }
  $row = implode(',', $prepared_args);
  }

  $stmt = _DB::init()->prepare("SELECT $row
                                FROM tax_class a
                                INNER JOIN products_to_tax_class pa 
                                ON a.tid = pa.tid
                                WHERE ($or)"
                              );     
  if(is_array($arg[0]))
  {
    if($stmt->execute($arg[0])) 
      return $stmt->fetch(PDO::FETCH_ASSOC);
  }
  else
  {
    if($stmt->execute(array($pid))) 
      return $stmt->fetch(PDO::FETCH_ASSOC);
  }                 
}

Much appreciated!

  • 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-05-23T05:33:44+00:00Added an answer on May 23, 2026 at 5:33 am

    My suggestion:

    public function getTaxClass() {
        $args = func_get_args();
    
        // columns to select:
        $cols = array();
        for ($i=1; $i<=count($args); $i++) {
            $cols[] = "a.{$args[$i]}";
        }
        $cols = count($cols) ? join(',', $cols) : 'a.*';
    
        // IDs to filter and their placeholders:
        $ids = (array) $args[0];
        $phs = join(',', array_fill(0, count($ids), '?'));
    
        $stmt = _DB::init()->prepare(
            "SELECT {$cols}
            FROM tax_class a
                INNER JOIN products_to_tax_class pa 
                ON a.tid = pa.tid
            WHERE pa.pid IN ({$phs})"
        );
    
        if ($stmt->execute($ids)) {
            return $stmt->fetch(PDO::FETCH_ASSOC);
        }
    }
    

    P.S. code is untested, some errors may still occur 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm asking more about what this means to my code. I understand the concepts
I'm not asking for anyone to do this homework for me, but I bring
Protected Means, we can access this member only in a deriving class, and internal
What does conform to means in IPhone Development? This word is extensively used but
I feel super fail asking this... but I've been trying to make sure my
means, do sth, then request using jQuery, like a hooker. How can i do
Uhm, so I had problems with the title, but this was my best shot!
Please note: I am asking the question I want answered. I know this question
I received this code from one of those nice people here who are willing
Can anyone recommend any good material that seeks to provide a real world perspective

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.