Error with PHP's RETURN function - Post [updated]

1

I have a problem with a function that I'm doing since it only gives me the primary data of the table and not a list NOTA: if it works with the echo but it comes out at the top of the site and what I want is that it works with return since I'm using HOOKS for the good system I hope you can help me and thanks

function system_contact() {

    global $con,$db,$error,$lang_text;
    $userID = userdata('userID',true);//User in session
//->
    $sql = mysqli_query($con,"SELECT * FROM ".T_FRIENDS." WHERE user_one = '$userID' OR user_two = '$userID' ")or die(mysqli_error());
    $query = $sql;
    $num_rows  = mysqli_num_rows($query);
    if ($num_rows != 0 ){
    while($row = mysqli_fetch_array($sql)){
        $user_one = $row['user_one'];//user one

        if($user_one == $userID){

            $user_two = $row['user_two'];
            $friends = mysqli_query($con,"SELECT * FROM ".T_USERS." WHERE userID = '$user_two'")or die(mysqli_error());
            $fila = mysqli_fetch_array($friends);

            $result_list[] = $fila;

                foreach($result_list as $fila){
                    $ECHO_CODE = '
                    <div class="wall_chat_users">
                        <img id="image_user_chat" src="./assets/img/avatar_default.png"></img>
                        <p id="name_user_chat">'.$fila['user'].'<spam class="message_counter_user_spam">42</spam></p>
                    </div>
                    ';

                }
                return @$ECHO_CODE;         

            }else{

                $friends = mysqli_query($con,"SELECT * FROM ".T_USERS." WHERE userID = '$user_one'")or die(mysqli_error());
                $fila = mysqli_fetch_array($friends);



                $ECHO_CODE = '
                    <div class="wall_chat_users">
                        <img id="image_user_chat" src="./assets/img/avatar_default.png"></img>
                        <p id="name_user_chat">'.$fila['user'].'<spam class="message_counter_user_spam">42</spam></p>
                    </div>
                ';
                foreach($friends as $fila){

                }
                //return @$ECHO_CODE;   

            }
        }

    }else{            


        $ECHO_CODE = '

            <div id="user_no_contact">
                <img class="user_no_contact_img" src="{{CONFIG theme_url}}/img/no_contact.png"></img>
                <p class="user_no_contact_p" >{{ADD_TEXT You_have_no_contact}}</p>
            </div>  

                <!--You don\'t have friends-->
        ';

        return @$ECHO_CODE;

    }
}

1- The data if they are in the database.

2- In this function I receive the data of the function that is above in the question as I do it I am using HOOKS for that, for those who do not know what the HOOKS is something like a plugin as well as use wordpress.

3- With this function I look for the selected text that is in the frame and thus I put the function inside the DIV .

4- When I use the function echo if you give me the list with the data but give it to me in the TOP and not within the DIV error.

5- if I use the return function if it gives me the data inside the DIV but only the primaro gives me not a list.

What can I do with it or where is the problem?

    
asked by sode 26.05.2018 в 23:34
source

4 answers

4

Beyond the problem with your $ ECHO_CODE, you are never concatenating the string, therefore, it always returns the last one you find inside your for each

your code should be:

$ECHO_CODE = ''
foreach($result_list as $fila){
    $ECHO_CODE .= '
    <div class="wall_chat_users">
    <img id="image_user_chat" src="./assets/img/avatar_default.png"></img>
    <p id="name_user_chat">'.$fila['user'].'<spam class="message_counter_user_spam">42</spam></p>
    </div>
    ';
}

Note the . to concatenate the string several times against the same variable.

    
answered by 27.05.2018 в 08:21
1

Proof that there were several technical failures ...

$ECHO_CODE = '';
foreach($result_list as $fila){
    $ECHO_CODE . = '<div class="wall_chat_users"><img id="image_user_chat" src="./assets/img/avatar_default.png" /><p id="name_user_chat">'.$fila['user'] . '<spam class="message_counter_user_spam">42</spam></p></div>';
}
    
answered by 27.05.2018 в 17:56
1

function system_contact() {

    global $con,$db,$error,$lang_text;
    $userID = userdata('userID',true);
    $sql = mysqli_query($con,"SELECT * FROM ".T_FRIENDS." WHERE user_one = '$userID' OR user_two = '$userID' ")or die(mysqli_error());
    $query = $sql;
    $num_rows  = mysqli_num_rows($query);
    if ($num_rows != 0 ){
    while($row = mysqli_fetch_array($sql)){
        $user_one = $row['user_one'];//user one

        if($user_one == $userID){

            $user_two = $row['user_two'];
            $friends = mysqli_query($con,"SELECT * FROM ".T_USERS." WHERE userID = '$user_two'")or die(mysqli_error());
            $fila = mysqli_fetch_array($friends);

            $result_list[] = $fila;

                foreach($result_list as $fila){
                    $ECHO_CODE = '<div class="wall_chat_users"><img id="image_user_chat" src="./assets/img/avatar_default.png" /><p id="name_user_chat">' . $fila['user'] . '<spam  class="message_counter_user_spam">42</spam></p></div>';
                }
                return @$ECHO_CODE;         
            }else{
                $friends = mysqli_query($con,"SELECT * FROM ".T_USERS." WHERE userID = '$user_one'")or die(mysqli_error());
                $fila = mysqli_fetch_array($friends);



                $ECHO_CODE = '<div class="wall_chat_users"><img id="image_user_chat" src="./assets/img/avatar_default.png" /> <p id="name_user_chat">' . $fila['user'] . '<spam class="message_counter_user_spam">42</spam></p></div>';
                foreach($friends as $fila){

                }
                //return @$ECHO_CODE;   

            }
        }

    }else{            


        $ECHO_CODE = '<div id="user_no_contact"><img  class="user_no_contact_img" src="{{CONFIG theme_url}}/img/no_contact.png" /> <p class="user_no_contact_p" >{{ADD_TEXT You_have_no_contact}}</p></div>';

        return @$ECHO_CODE;

    }
}
    
answered by 27.05.2018 в 18:08
1

Is it solved with this?

In the line that inserts the array you have to establish it from the beginning.

// Fijate en la Variable has de tener las claves
// de este tipo [] para que sea un array la variable


    $fila[] = mysqli_fetch_array($friends);
    
answered by 27.05.2018 в 03:13