0

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?

5
  • Before the if($tables['followerid']!=$table['id']) statement, echo the two value and see what you get. Commented Dec 24, 2012 at 13:32
  • I only get the username - no buttons are displayed Commented Dec 24, 2012 at 13:35
  • what are two extra braces? Commented Dec 24, 2012 at 13:36
  • sorry, I think I included them by mistake off the previous loop Commented Dec 24, 2012 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. Commented Dec 24, 2012 at 14:06

2 Answers 2

1

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>";
Sign up to request clarification or add additional context in comments.

8 Comments

buttons ? what buttons do u mean ? u have links <a> u can do them in else
sorry - it still isn't showing the links - it shows the <td>username<td> but not the following <td>
what this variable $table?
$table is from the previous while loop getting the 'id' from mysql of users
replace this and see if the link appears or not <a href="#" data-userid="'.$table['id'].'" class="follow">gggggggg</a> replace it in the first link and in second link
|
0

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

7 Comments

this kinda works...but if a user is not following somebody it repeats it twice - so class="follow"
or if a user follows all of the users it shows both the follow class and following class
Add the $trip to the if{} clause also.
Add a 2nd type of $trip - or a counter.
is it something to do with nesting the while loop why it isnt working?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.