mardi 16 janvier 2018

PHP/MySQL - if/else statement not working

Here's two functions I use to list items on CMS page:

Function 1 - function to create tabs content

function throwCoinGroupTabs($mysqli){
if ($stmt = $mysqli->prepare("SELECT ID, GroupName FROM coin_groups ORDER BY GroupPos ASC")){
    $stmt->execute();
    $stmt->bind_result($ID, $name);
    $tabs ='';
    while($stmt->fetch()){

        $tabs .= '<div class="container-fluid tab-pane fade show active" id="nav-'.$name.'" role="tabpanel" aria-labelledby="nav-'.$name.'-tab"><div class="row">';
        $tabs .= throwCoinsByGroups($mysqli, $ID);  // INITIATE 2nd function
        $tabs .= '</div></div>';

    }
    return $tabs;
}
}

Function 2 - function to list items in choosen category

function throwCoinsByGroups($mysqli, $groupId){
if ($stmt = $mysqli->prepare("SELECT id, CoinName, CoinGroups, PhotoThumb FROM coins")) {
    $stmt->execute();
    $stmt->bind_result($c['id'], $c['name'], $c['group'], $c['photo']);
    $result = "";
    while ($stmt->fetch()){
        if ($groupId == 0) {
            $result .= throwCoin($c['id'], $c['name'], $c['photo']);
        } else {
            $group = explode(',', $c['group']);
            foreach ($group as $value) {
                $v = explode(':', $value);
                if ($groupId== $v[0] && $v[1] == "1") {
                    $res .= throwCoin($c['id'], $c['name'], $c['photo']);
                }
            }
        }
    }
    return $result;
}
}

How I initiate both of them?

<div class="tab-content" id="nav-tabContent">
        <div class="container-fluid tab-pane fade show active" id="nav-all" role="tabpanel" aria-labelledby="nav-all-tab" aria-expanded="true"><div class="row"><?php echo throwCoinsByGroups($mysqli, 0) ?></div></div>
        <?php echo throwCoinGroupTabs($mysqli); ?>
</div>

What's the problem?

1st tab is "ALL" tab which means it will contain all items regardless of category. That's why I created IF statement and initiated 2nd function with 0 as 2nd argument and that IF statement works! The problem is with ELSE statement which doesn't work. MySQL SELECT works properly, it returns correct values of ID's (checked it by using var_dump($ID) inside a fetch() loop)

I tried initiating 2nd function with 'manualy' putting other ID's, result: FAIL

Checked if any arguments are passed to ELSE statement, result: FAIL

It just ignores ELSE statement, why? T_T

#ELSE-STAHP-PLOX-T_T

Aucun commentaire:

Enregistrer un commentaire