0

How to make the if statement, if the the post belongs to the user, then only the owner of the post has a like/view/edit/delete button available. If the post does not belong to the user, it only displays like and view buttons.

<?php
$get_post = "SELECT * FROM posts";
$run_post = mysqli_query($connect, $get_post);
if($run_post && mysqli_num_rows($run_post) > 0)
{
    while($row_post = mysqli_fetch_array($run_post))
    {
        $post_title  = $row_post['post_title'];
        $get_user    = "SELECT * FROM users WHERE user_id='$user_id'";
        $run_user    = mysqli_query($connect, $get_user);
        $check_user  = mysqli_fetch_array($run_user);
        $user_name   = $check_user['name'];
        $user_images = $check_user['images'];

        echo "<div id='post_wrap'>
            <p>$post_title</p>
            <a href=''><button>Like</button></a>
            <a href=''><button>View</button></a>
            <a href=''><button>Delete</button></a>
            <a href=''><button>Edit</button></a>
            </div>";
    }
    mysqli_free_result($run_post);
}
else
{
    echo "No post yet";
}
?>
2
  • Please post database details. (column names) Commented Oct 18, 2016 at 4:04
  • users table (user_id, name, email) , posts (post_id, user_id, title) Commented Oct 18, 2016 at 4:06

3 Answers 3

2

You have to store user_id as a session value. Then you can checked current user posts this way, $_SESSION['user_id'] == $row_post['user_id']

 echo "<div id='post_wrap'>
           <p>$post_title</p>
           <a href=''><button>Like</button></a>
           <a href=''><button>View</button></a>";
           if($_SESSION['user_id'] == $row_post['user_id']){
                  echo "<a href=''><button>Delete</button></a>
                        <a href=''><button>Edit</button></a>";
            }            

     echo "</div>";
Sign up to request clarification or add additional context in comments.

5 Comments

how you get current user id, have you used sessions?
i use 'email' column field in the table as session
you have to store user_id also as session.
i can echo the value of 'email' session . but when i try to add if statement inside echo "" . it didnt work at all . :(
0

Please check following answer.

                 $html = "<a href=''><button>Like</button></a>
                            <a href=''><button>View</button></a>";
                if($user_id == $row_post['user_id']){
                   $html .= "<a href=''><button>Delete</button></a>
                         <a href=''><button>Edit</button></a>"
                }            
  echo $html;

1 Comment

thanks . let me try the code first :) . i'll let you know
0

Use Following, check if user id and database id are same so that will add extra HTML in $login_user else will be empty. And echo $login_user in your View.

<?php

    $get_post = "SELECT * FROM posts";
    $run_post = mysqli_query($connect, $get_post);
        if($run_post && mysqli_num_rows($run_post) > 0 )
        {
            while($row_post = mysqli_fetch_array($run_post))
            {
                $post_title      = $row_post['post_title'];

                $get_user   =  "SELECT * FROM users WHERE user_id='$user_id'";
                $run_user   = mysqli_query($connect, $get_user);
                $check_user = mysqli_fetch_array($run_user);

                    $user_name  = $check_user['name'];
                    $user_images = $check_user['images'];   

                    $login_user = ($user_id == $row_post['user_id'])? "<a href=''><button>Delete</button></a>
                                    <a href=''><button>Edit</button></a>": "";
                        echo "<div id='post_wrap'>
                                    <p>$post_title</p>
                                    <a href=''><button>Like</button></a>
                                    <a href=''><button>View</button></a>".
                                $login_user . "
                                </div>";
            }
            mysqli_free_result($run_post);          
        }
    else
    {
        echo "No post yet";
    }
?>  

2 Comments

thanks for the reply :) . but i use 'email' field column in database as $session . but thanks. i'll try your suggestion first .. see if its work or not .
hye . thanks for the response . :) . it's ok . i already found the solutions . stackoverflow.com/questions/40120634/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.