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!
My suggestion:
P.S. code is untested, some errors may still occur 🙂