0
if($parent_id_current > 0) {
        $parent_id = $parent_id_current;
        $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$parent_id_current'";
        $parent_id_current = $GLOBALS['db']->getOne($sql);
        if($parent_id_current > 0) {
            $parent_id = $parent_id_current;
            $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$parent_id_current'";
            $parent_id_current = $GLOBALS['db']->getOne($sql);
            if($parent_id_current > 0) {
                $parent_id = $parent_id_current;
                $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$parent_id_current'";
                $parent_id_current = $GLOBALS['db']->getOne($sql);
                if($parent_id_current > 0) {
                    $parent_id = $parent_id_current;
                    $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$parent_id_current'";
                    $parent_id_current = $GLOBALS['db']->getOne($sql);
                    if($parent_id_current > 0) {
                        $parent_id = $parent_id_current;
                        $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$parent_id_current'";
                        $parent_id_current = $GLOBALS['db']->getOne($sql);
                        if($parent_id_current > 0) {
                            $parent_id = $parent_id_current;
                        }
                    }
                }
            }
        }
    }
} else {
    $parent_id = 0;
}

I know this is bad practice .. but how to rewrite to a good function structure. I know the return will make this code more clean.. but I just don't know where to put it.

2 Answers 2

1

Unless I missed something, this looks like a good use of the Do/While loop. Although you might need to adjust functions to make sure you progress through the result set as anticipated.

do {
    $parent_id = $parent_id_current;
    $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . "WHERE cat_id = '$parent_id_current'";
    $parent_id_current = $GLOBALS['db']->getOne($sql);
} while ( $parent_id_current > 0);

Also, when debugging nested IF statements, if you loose your place, start adding comment flags right of the open curly brace and count them down as you close them.

IF (test) { // 1
echo $something;
IF (test) { // 2
echo $something_else;
IF (test) { // 3
die;
} // 3
} // 2
} // 1
Sign up to request clarification or add additional context in comments.

Comments

0

Move your code to function and call it in while loop. As break condition set your if condition:

$parent_id = 0;
while ($parent_id_current > 0) {
    $parent_id = $parent_id_current;
    $parent_id_current = getParentId($parent_id_current);
}

function getParentId($id) {
    $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$id'";
    return $GLOBALS['db']->getOne($sql);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.