1
<?php
$sun=mysql_query("SELECT * FROM `red_users` WHERE `role`='user' AND `userID` != '$userID' ORDER BY `id` DESC LIMIT 10 ") or die(mysql_error());

while($su[]=mysql_fetch_array($sun)){

}

for($i=0;$i<count($su);$i++){

    $sud=$su[$i]['userID'];
    $sname=$su[$i]['name'];
    $dop=$su[$i]['dp'];
    $sxx=$su[$i]['sex'];

    if($dop != ''){
        $sey=$dop;
    }

    if($dop =='' AND $sxx=='MALE'){
        $sey="../images/male.png";
    }

    if($dop =='' AND $sxx=='FEMALE'){
        $sey="../images/female.png";
    }
?>

<a href="profile.php?frnd=<?php echo $sud ?>">
    <div class="inbox-item">
        <div class="inbox-item-img"><img src="<?php echo $sey ?>" class="img-circle" alt=""></div>
        <p class="inbox-item-author"><?php echo $sname; ?></p>
    </div>
</a>
<?php } ?

In output an extra Html element (with no dynamic data) is displaying.
After the Real elements which are populated by mysql query an extra element is displaying which contain no data.

1 Answer 1

2

Instead of

while($su[]=mysql_fetch_array($sun)){

}

and further processing of $su

process every record in the same while:

while($su=mysql_fetch_array($sun)){
    // do stuff
}

Additional note by @mloureiro:

This because the while keeps running till it find a false value, so when the mysql_fetch_array doesn't have anything else to return, it will give false that will be passed to the $su[] array.

Sign up to request clarification or add additional context in comments.

9 Comments

it will run the while loop over and over again.so the page will be slow
You will run one while loop instead of two loops. Are you sure page will be slow?
@MuhammedAsif that doesn't make sense, I'm not an expert in mysql methods, but using the while + for just looks redundant.
@MuhammedAsif can you test this answer please?
I think I understand the question of @MuhammedAsif, the thing is instead of having the statements inside the for, move them to the while
|

Your Answer

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