1
I’m developing a followers system, created a page that does a search of registered users. You type in an input a name, and it checks if there are any records with that name in the database.
I put on the side of each name that returns from the database, a button "Follow" in case the person wants to follow the user. However, I also want that if the person has already followed that user, do not appear the follow button, but yes, the "Cease to Follow".
I created the button and added it to the page, tried to implement it myself but could not, so I was told to make a SELECT of all users, save it in an array, and then in a loop, check with the in_array() If the person who is logged in, already followed the user who returned from the search, but could not make it work! The search query is working correctly, where I am missing?
Function SELECT:
<?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;
        }
    }
?>
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 -->
- $consulta2 is what searches users in the database.
- $consulta3 is what searches all users that the logged in person follows.
var_dump in the variable $consulta3:



- idfollower is the id of the person who followed.
- idfollowed is the id of the person who was followed.
Unfortunately, neither of them worked... :c
– HooDyrd
I updated after var_dump.
– Inkeliz