我在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中实现这个?那么如果'关注者表'中没有行回显按钮吗?
答案 0 :(得分: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>";
答案 1 :(得分:0)
在'followers'循环的开头放置一个布尔值(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