简体   繁体   中英

Foreach loop echo

The last foreach loop doesn't seem to echo the list I want, it doesn't print anything. How can I fix this? I know it's getting quite complicated but any help would be appreciated.

$allTroopsList = array();
$allMissionsList = array();
while ($mytroops = $alltroops->fetch_assoc()) 
{
    $allTroopsList []= $mytroops;
}

while ($mymissions = $allmissions->fetch_assoc()) 
{
    $allMissionsList []= $mymissions;
}

while($userintroop = $allUsersintroops->fetch_assoc()) 
{
    if($userintroop['userid'] == $_SESSION['userid']) 
    {
        echo "<ul class='troop'>";   
        echo "<li>" . $userintroop['troopid'].  " </li>";
        foreach($allTroopsList as $mytroops) 
        {
            if($userintroop['troopid'] == $mytroops['troopid']) 
            {
                echo "<li> Troop description: " . $mytroops['description'].  " </li>";

                foreach($allMissionsList as $mymissions) 
                {
                    if($mytroops['missionid'] == $mymissions['missionid']) 
                    {   
                        echo "<li> Missionname: " . $mymissions['missionname'].  " </li>";
                        echo "<br/>";
                        echo "</ul>";
                    }
                }
            }
        }
    }   
}           

foreach($allUsers as $myotherusers) 
{
    if($userintroop['userid'] == $myotherusers['userid']) 
    {

        echo "<li> other users: " . $myotherusers['username'].  " </li>";
        echo "<br/>";
    }
}

This line:

while($userintroop = $allUsersintroops->fetch_assoc()) 

is fetching results from a database, right? That'd mean at some point you run out of rows to fetch, and $userintroop will become a boolean FALSE.

Then later on you try to do

if($userintroop['userid'] == $myotherusers['userid']) 

which is wrong on two levels - $userintroop will NOT be an array at this point, so it could never have a ['userid'] parameter. And since it's the result of a failed fetch operation, never could have any value OTHER than a FALSE.

So that last loop does execute, but will never produce anything since the conditions for producing output are literally impossible to meet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM