0

我在我的 PHP 的另一个 while 循环中有这个 while 循环:

$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>";

这更像是一个逻辑问题,以及嵌套的 while 循环是否是正确的方法。我想说的是,如果“用户关注者表”中的“followerid”与用户表中的“id”(来自上一个循环)不同 - 回显关注按钮,否则回显以下按钮.

这是工作文件,而我在关注者表中有数据但如果我没有显示(因为没有行) - 我如何在我的 PHP 中实现它?那么,如果“关注者表”中没有行,则回显关注按钮?

4

2 回答 2

1

你可以尝试这样做

$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>";

编辑

      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>";
于 2012-12-24T13:38:28.713 回答
0

在“追随者”循环的开头放置一个布尔值(FALSE),这样如果它被交叉,则使其为 TRUE。如果您超出循环并且它仍然是 FALSE,那么无论如何添加按钮。

$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
于 2012-12-24T13:34:03.353 回答