Implement Follow / Unfollow Feature

1

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:

asked by anonymous 27.12.2014 / 21:43

1 answer

1

I think there is a much easier way to solve the problem.

Solution 1:

Replace the line:

if(in_array($idatual, $consulta3)){

By:

if(in_array($idatual, $consulta3['idfollowed'])){
  • Idea: in_array should be for idfollowed, since it is it containing idsession.

Solution 2:

Step 1: First change the $ query3:

Come on, replace the line:

$consulta3 = select("u636623377_follows", "idfollowed", "WHERE idfollower = '$idsession'");

By:

$consulta3 = select("u636623377_follows", "idfollowed", "WHERE idfollower = '$idsession' AND idfollowed = '$consulta2['id']'");
  • Idea: It will select what has the idfollower equal to the current session and also has idfollowed equal to the id of the profile you are viewing.

So, in your SELECT() it returns false if the mysql_num_rows is equal to 0. So if you do not find $consulta3 it will false , perfect.

Step 2: Change if Unfollow:

Replace the line:

<?php if(in_array($idatual, $consulta3)){ ?>

By:

<?php if($consulta3){ ?>

- Idea: If the $ query is false it will display 'Follow', if it does not display 'Unfollow'.

Solution (post-var_dump):

Use loop:

<? $total = count($consulta3);
    for($i =0; $i < $total; $i++){
       if($consulta3[$i]['followed'] == $idatual){ ?>

           <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>
       <? } 
    } ?>

It would be a solution, or use foreach , but it would follow the same logic.

    
28.12.2014 / 02:19