Implement Follow/Stop Following functionality

Asked

Viewed 231 times

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: inserir a descrição da imagem aqui

Essa é a estrutura da tabela dos usuários

Essa [é a estrutura da tabela das "seguidas"

  • idfollower is the id of the person who followed.
  • idfollowed is the id of the person who was followed.

1 answer

1

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

Solution 1:

Replace the line:

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

For:

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

Solution 2:

Step 1: First Change the $consulta3:

Come on, replace the line:

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

For:

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

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

Step 2: Change if Stop following:

Replace the line:

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

For:

<?php if($consulta3){ ?>

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

Solution (post-var_dump):

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 the foreach, but would follow the same logic.

  • Unfortunately, neither of them worked... :c

  • I updated after var_dump.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.