Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have this while loop within another while loop in my PHP:

$selecty = mysql_query("SELECT * FROM followers WHERE userid='".$_SESSION['id']."'");

$rowsy = mysql_num_rows($selecty);

echo '<td>'. $table["username"]. '</td>';
echo '<td>';

while ($tables = mysql_fetch_assoc($selecty)) {
    if($tables['followerid']!=$table['id']) {
        echo '<a href="#" data-userid="'.$table['id'].'" class="follow">'.'</a>'; 
    } else {
        echo '<a href="#" data-userid="'.$table['id'].'" class="following">'.'</a>';
    }
}
echo '</td>';
echo "<tr>";

This is more of a logic question and whether or not a nested while loop is the right way to do it. What I'm trying to say is if the 'followerid' from 'user followers table' is not the same as the 'id' from users table (which is from the previous loop) - echo the follow button, else echo the following button.

This is working file while I have data in the followers table but If I don't nothing shows (as there are no rows) - How could I implement this in my PHP? So also if there are no rows in 'followers table' echo follow button?

share|improve this question
    
Before the if($tables['followerid']!=$table['id']) statement, echo the two value and see what you get. –  Osiris Dec 24 '12 at 13:32
    
I only get the username - no buttons are displayed –  Andrew Charlton Dec 24 '12 at 13:35
    
what are two extra braces? –  user904550 Dec 24 '12 at 13:36
    
sorry, I think I included them by mistake off the previous loop –  Andrew Charlton Dec 24 '12 at 13:37
1  
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. –  Neal Dec 24 '12 at 14:06

2 Answers 2

you can try do it like that

$selecty = mysql_query("SELECT * FROM followers WHERE userid='".$_SESSION['id']."'");

$rowsy = mysql_num_rows($selecty);

echo '<td>'. $table["username"]. '</td>';
echo '<td>';

while ($tables = mysql_fetch_assoc($selecty)) {
    if($tables['followerid']!=$table['id'] and $tables['followerid'] != '') {
        echo '<a href="#" data-userid="'.$table['id'].'" class="follow"></a>'; 
    } else if($tables['followerid'] =$table['id'] and $tables['followerid'] !='') {
        echo '<a href="#" data-userid="'.$table['id'].'" class="following"></a>';
    } else {
        echo what you like here when $tables['followerid'] = ''
    }
}

echo '</td>';
echo "<tr>";

edit

      class="follow">'.'</a>'
                      ^------------you dont have to make point and single quotes here

   $selecty = mysql_query("SELECT * FROM followers WHERE    userid='".$_SESSION['id']."'");

 $rowsy = mysql_num_rows($selecty);
 echo '<table><tr>';
  echo '<td>'. $table["username"]. '</td></tr>';

 if ($tables['followerid'] !== ''){
 while ($tables = mysql_fetch_assoc($selecty)) {
 echo '<tr><td>';
if($tables['followerid']!=$table['id'] and $tables['followerid'] != '') {
    echo '<a href="#" data-userid="'.$table['id'].'" class="follow"></a></td></tr>'; 
} else if($tables['followerid'] =$table['id'] and $tables['followerid'] !='') {
    echo '<a href="#" data-userid="'.$table['id'].'" class="following"></a></td></tr>';
} else {
    echo "what you like here  </td></tr>";
}
}
}
else {

 echo "do your code here " ; 
}

echo "</table>";
share|improve this answer
    
hmm this still isn't showing any buttons? –  Andrew Charlton Dec 24 '12 at 13:41
    
buttons ? what buttons do u mean ? u have links <a> u can do them in else –  echo_Me Dec 24 '12 at 13:43
    
sorry - it still isn't showing the links - it shows the <td>username<td> but not the following <td> –  Andrew Charlton Dec 24 '12 at 13:44
    
look my edited answer –  echo_Me Dec 24 '12 at 13:47
    
still not showing dude –  Andrew Charlton Dec 24 '12 at 13:50

Put a boolean (FALSE) at the start of the 'followers' loop such that if it gets crossed make it TRUE. If you get outside the loop and it's still FALSE then add the button anyway.

$trip = FALSE;

while ($tables = mysql_fetch_assoc($selecty)) {
  if($tables['followerid']!=$table['id']) {
    echo '<a href="#" data-userid="'.$table['id'].'" class="follow">'.'</a>'; 
  } else {
    $trip = TRUE;
    echo '<a href="#" data-userid="'.$table['id'].'" class="following">'.'</a>';
  }
}

if( !$trip ) echo '<a href="#" data-userid="'.$_SESSION['id'].'" class="follow">'.'</a
share|improve this answer
    
Could you show how I would implement that please? –  Andrew Charlton Dec 24 '12 at 13:53
    
Edited to show example –  ethrbunny Dec 24 '12 at 13:58
    
this kinda works...but if a user is not following somebody it repeats it twice - so class="follow" –  Andrew Charlton Dec 24 '12 at 14:02
    
and class="follow" –  Andrew Charlton Dec 24 '12 at 14:04
    
or if a user follows all of the users it shows both the follow class and following class –  Andrew Charlton Dec 24 '12 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.