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ループが正しい方法であるかどうかです。私が言おうとしているのは、「userfollowerstable」の「followerid」がusersテーブルの「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

'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
于 2012-12-24T13:34:03.353 に答える