I'm developing a follower system, I created a page that searches for registered users. You type in an input a name, and it checks to see if you have any records with that name in the database.
I put a Follow button next to each name that returns from the database in case the user wants to follow the user. However, I also want that if the person has already followed that user, the following button does not appear, but rather the " Unfollow " button.
I created the button and added it to the page, I tried to implement it myself but I could not, so I was instructed to SELECT all users, save this in an array, and then in a loop, check with in_array()
if the person that is logged, already followed the user who returned from the search, but could not do it works! The search query is working correctly, where am I going wrong?
SELECT function:
<?php
include 'fconnect.php';
include 'fdesconnect.php';
function select($tabela,$coluna="*",$where=NULL,$ordem=NULL,$limite=NULL) {
// Query de consulta
$sql= "SELECT {$coluna} FROM {$tabela} {$where} {$ordem} {$limite}";
// conectou?
if($conexao= connect()) {
// Conseguiu consultar?
if($query= mysql_query($sql,$conexao)) {
// Encontrou algo?
if (mysql_num_rows($query)>0) {
$resultados_totais = array();
while ($resultado = mysql_fetch_assoc($query)) {
$resultados_totais[] = $resultado;
}
// Fecha conexão
desconnect($conexao);
return $resultados_totais;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}
?>
The page that performs the search and shows the buttons, I left only the essential parts.
$idsession = $_SESSION['ID'];
$consulta2 = select("u636623377_users", "*", "WHERE fname LIKE '%$q%'");
$consulta3 = select("u636623377_follows", "idfollowed", "WHERE idfollower = '$idsession'");
<div class="box">
<div class="box-header">
<h3 class="box-title">Resultado da Busca...</h3>
</div><!-- /.box-header -->
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
<th>Usuário</th>
<th></th>
<th>Cidade</th>
<th>Estado</th>
<th></th>
</tr>
<?php if($consulta2 == true){
for ($i=0;$i<count($consulta2);$i++) {
$idatual = $consulta2[$i]['id'];
?>
<tr>
<td><img class="img-circle" src="assets/img/img_profile/<?php echo $consulta2[$i]['photoperf'] ?>" style="width: 60px; height: 60px;"></td>
<td><h4 style="font-family:'Asap',sans-serif;"><?php echo $consulta2[$i]['fname'] ?></h4></td>
<td><h4 style="font-family:'Asap',sans-serif;"><?php echo $consulta2[$i]['city'] ?></h4></td>
<td><h4 style="font-family:'Asap',sans-serif;"><?php echo $consulta2[$i]['state'] ?></h4></td>
<td>
<h4 style="font-family:'Asap',sans-serif;">
<a href="profile.php?link=<?php echo $consulta2[$i]['profile'] ?>" class="label label-primary">Ver Perfil</a>
<?php if($consulta2[$i]['id'] == $_SESSION['ID']){}else{ ?>
<?php if(in_array($idatual, $consulta3)){ ?>
<a href="php/scripts/disfollow.php?id=<?php echo $consulta2[$i]['id'] ?>" class="label label-danger">Deixar de Seguir</a>
<?php }else{ ?>
<a href="php/scripts/follow.php?id=<?php echo $consulta2[$i]['id'] ?>" class="label label-success">Seguir</a>
<?php } } ?>
</h4>
</td>
</tr>
<?php } } ?>
</table>
</div><!-- /.box-body -->
</div><!-- /.box -->
- $ query2 is what users are looking for in the database.
- $ query3 is what all users that the logged in user searches for.
var_dump
in the $ query3 variable: