Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to ask,

I have two tables , users and posts with column field

users : user_id, name, email

posts : post_id, user_id, post_title

I want to display all posts from all users,

but I want only logged_in user session to have another two extra button while other public posts only have two button

p/s : I used email column field in users table as login $_SESSION.

<?php 
    global $connect;
    global $user_id;

    $sql_post = "SELECT * FROM posts";
    $run_post = mysqli_query($connect, $sql_post);

    if($run_post && mysqli_num_rows($run_post) > 0 )
    {
        while($row_post = mysqli_fetch_array($run_post))
        {
            $post_id    = $row_post['post_id'];
            $user_id    = $row_post['user_id'];
            $post_title = $row_post['post_title'];

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

                    $user_id     = $check_user['user_id'];
                    $user_name   = $check_user['name'];
                    $user_email  = $check_user['email'];

                    $post_output = "<div id='posts_wrap'>
                                        <p>$user_name</p>
                                        <p>$user_email</p>
                                        <p>$post_title</p>
                                        <a href=''><button>Like</button></a>
                                        <a href=''><button>Comment</button></a> 

                                            // i want these two button (Edit and Delete) only available to logged in user
                                            <a href=''><button>Edit</button></a>
                                            <a href=''><button>Delete</button></a>  
                                    </div>
                                   ";
                    echo $post_output;
        }
        mysqli_free_result($run_post);          
    }
    else
    {
        echo "No post yet";
    }
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

After user loggine keep user detail in session and check condition if user logged in or not For example if you are trying to comment and like only for logged in user then you can do somethink like

<?php
session_start();
$_SESSION['email']='email@example.com';
$user_name='dd';
$user_email='ddd';
$post_title='gsdg';

$post_output = "<div id='posts_wrap'><p>$user_name</p><p>$user_email</p><p>$post_title</p>";
 if(isset($_SESSION['email'])){ 
        $post_output.="<a href=''><button>Like</button></a><a href=''><button>Comment</button></a> ";
    }  
    // i want these two button (Edit and Delete) only available to logged in user
    $post_output.= "<a href=''><button>Edit</button></a><a href=''><button>Delete</button></a> </div>";                                    
            print_r($post_output);      





?>

in the above code user is logged in so all buttons are visible .if not then its not visible to all .just try to destroy session.i think previous session email still there


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...