Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

mysql - How to create if statement inside echo in PHP MySQLi

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

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

1 Reply

0 votes
by (71.8m points)

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 protected]';
$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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...