Search code examples
htmlcssforeachpositionstylesheet

Foreach loop position not working correctly


I'm trying to make a list with people that have been stored in a mysql database, however only the first database item is styled properly and the others are out of place. I'm fairly new with css and I probably made a mistake in the while loop or positioning.

https://i.sstatic.net/NnYy1.png Image ^^

The code that I'm using is as followed:

   <div id="adminsummary">

<div id="admintitel">
    <p id="admintext">Admin Overview</p>
</div>
<?php
while ($row = mysqli_fetch_array($result)) {
    $steamid = $row["steamid"];
    $xml = simplexml_load_file("http://steamcommunity.com/profiles/$steamid/?xml=1");
    if (!empty($xml)) {
        $username = $xml->steamID;
    }
    ?>

    <div id="admin">
        <img id="adminimg" src="<?php echo $steamprofile['avatarmedium']; ?>">
        <p id="adminname"><?php echo $username; ?></p>
     <!--   <p id="adminname"> SteamID: <?php echo $row["steamid"]; ?></p> -->
        <p id="adminname"> Rank: <?php if ($row["rank"] == 1) {echo "SuperAdmin";} else {echo "Admin";} ?></p>
        <hr id="hr2">
    </div>
    </div>

The stylesheet:

#adminsummary {
    background: white;
    margin-top: 5%;
    height: 75%;
    width: 25%;
    margin-right: 5%;
    float:right;
    border-radius: 25px;
    box-shadow: 10px 10px 77px -6px rgba(0,0,0,0.59);
}

#admintitel{
    background: #343a40;
    border-top-left-radius: 25px;
    border-top-right-radius: 25px;


}

#admintext{
    color: white;
    font-family: sans-serif;
    font-weight: bold;
    text-align: center;
    padding: 25px;

}


#adminimg {

    border-radius: 25px;
    float: left;
    margin-left: 25px;
}


#hr2 {
    display: block;
    margin-top: 25px;
    margin-bottom: 0.5em;
    margin-left: auto;
    margin-right: auto;
    border-style: inset;
    border-width: 1px;
}

#adminname {
    text-align: left;
    font-weight: bold;
    margin-left: 25%;
    font-family: sans-serif;
}

Thanks for taking the time to help me


Solution

  • If you modify the HTML and remove the IDs within the loop you can use a combination of classes with child/sibling selectors to assign styles in a cleaner way

    <!-- these IDs are OK as they are unique ( presumably ) -->
    <div id="adminsummary">
    
        <div id="admintitel">
            <p id="admintext">Admin Overview</p>
        </div>
    
        <?php
    
            while ($row = mysqli_fetch_array($result)) {
                $steamid = $row["steamid"];
                $xml = simplexml_load_file("http://steamcommunity.com/profiles/$steamid/?xml=1");
    
                if (!empty($xml)) {
                    $username = $xml->steamID;
                }
    
        ?>
    
        <!-- use classes &/or sibling selectors for cleaner html ~ no duplicate IDs though -->
        <div class="admin">
            <img src="<?php echo $steamprofile['avatarmedium']; ?>">
            <p><?php echo $username; ?></p>
            <p> Rank: <?php if ($row["rank"] == 1) {echo "SuperAdmin";} else {echo "Admin";} ?></p>
            <hr />
        </div>
    
    
        <?php
            }//close loop
        ?>
    
    </div>
    

    the slightly modified CSS using child selectors to assign styles within the .admin code block

    #adminsummary {
        background: white;
        margin-top: 5%;
        height: 75%;
        width: 25%;
        margin-right: 5%;
        float:right;
        border-radius: 25px;
        box-shadow: 10px 10px 77px -6px rgba(0,0,0,0.59);
    }
    #admintitel{
        background: #343a40;
        border-top-left-radius: 25px;
        border-top-right-radius: 25px;
    }
    #admintext{
        color: white;
        font-family: sans-serif;
        font-weight: bold;
        text-align: center;
        padding: 25px;
    }
    
    
    #adminsummary .admin img {
        border-radius: 25px;
        float: left;
        margin-left: 25px;
    }
    
    #adminsummary .admin hr {
        display: block;
        margin-top: 25px;
        margin-bottom: 0.5em;
        margin-left: auto;
        margin-right: auto;
        border-style: inset;
        border-width: 1px;
    }
    #adminsummary .admin p {
        text-align: left;
        font-weight: bold;
        margin-left: 25%;
        font-family: sans-serif;
    }