Jump to content

username as comment message author - Help Me Please


Jordanxl

Recommended Posts

Hello I'm new to the forum, I need your help. I want to make a review place. but i have a problem. The name of the logged in user does not appear in the comment. can you examine the code and help me. user registration, login, done everything. this is my only problem. I tried everything, it didn't work. or I'm new. I just started. I don't know php very well.but I think I've come here so well: D

<?php 
    session_start();
    include('header.php'); 
    include_once("db_connect.php");

    ?>

    <script src="rating.js"></script>
    <script type="text/javascript" src="script/ajax.js"></script>
    <link rel="stylesheet" href="style.css">
    <?php include('container.php');?>
    <div class="container">     
    // User Register Sign Place
                <div class="collapse navbar-collapse" id="navbar1">
                <ul class="nav navbar-nav navbar-left">
                    <?php if (isset($_SESSION['user_id'])) { ?>
                    <li><p class="navbar-text"><strong>Welcome!</strong> You're signed in as <strong><?php echo $_SESSION['user_name']; ?></strong></p></li>
                    <li><a href="logout.php">Log Out</a></li>
                    <?php } else { ?>
                    <li><a href="login.php">Login</a></li>
                    <li><a href="register.php">Sign Up</a></li>
                    <?php } ?>
                </ul>
            </div>
    </div>  
    // Rating Star Place
        <?php
        include_once("db_connect.php");
        $ratingDetails = "SELECT ratingNumber FROM item_rating";
        $rateResult = mysqli_query($conn, $ratingDetails) or die("database error:". mysqli_error($conn));
        $ratingNumber = 0;
        $count = 0;
        $fiveStarRating = 0;
        $fourStarRating = 0;
        $threeStarRating = 0;
        $twoStarRating = 0;
        $oneStarRating = 0;
        while($rate = mysqli_fetch_assoc($rateResult)) {
            $ratingNumber+= $rate['ratingNumber'];
            $count += 1;
            if($rate['ratingNumber'] == 5) {
                $fiveStarRating +=1;
            } else if($rate['ratingNumber'] == 4) {
                $fourStarRating +=1;
            } else if($rate['ratingNumber'] == 3) {
                $threeStarRating +=1;
            } else if($rate['ratingNumber'] == 2) {
                $twoStarRating +=1;
            } else if($rate['ratingNumber'] == 1) {
                $oneStarRating +=1;
            }
        }
        $average = 0;
        if($ratingNumber && $count) {
            $average = $ratingNumber/$count;
        }   
        ?>      
        <br>
    // Rating Details   
        <div id="ratingDetails">        
            <div class="row">           
                <div class="col-sm-3">              
                    <h4>Rating and Reviews</h4>
                    <h2 class="bold padding-bottom-7"><?php printf('%.1f', $average); ?> <small>/ 5</small></h2>                
                    <?php
                    $averageRating = round($average, 0);
                    for ($i = 1; $i <= 5; $i++) {
                        $ratingClass = "btn-default btn-grey";
                        if($i <= $averageRating) {
                            $ratingClass = "btn-warning";
                        }
                    ?>
                    <button type="button" class="btn btn-sm <?php echo $ratingClass; ?>" aria-label="Left Align">
                      <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                    </button>   
                    <?php } ?>              
                </div>
                <div class="col-sm-3">
                    <?php
                    $fiveStarRatingPercent = round(($fiveStarRating/5)*100);
                    $fiveStarRatingPercent = !empty($fiveStarRatingPercent)?$fiveStarRatingPercent.'%':'0%';    

                    $fourStarRatingPercent = round(($fourStarRating/5)*100);
                    $fourStarRatingPercent = !empty($fourStarRatingPercent)?$fourStarRatingPercent.'%':'0%';

                    $threeStarRatingPercent = round(($threeStarRating/5)*100);
                    $threeStarRatingPercent = !empty($threeStarRatingPercent)?$threeStarRatingPercent.'%':'0%';

                    $twoStarRatingPercent = round(($twoStarRating/5)*100);
                    $twoStarRatingPercent = !empty($twoStarRatingPercent)?$twoStarRatingPercent.'%':'0%';

                    $oneStarRatingPercent = round(($oneStarRating/5)*100);
                    $oneStarRatingPercent = !empty($oneStarRatingPercent)?$oneStarRatingPercent.'%':'0%';

                    ?>
                    <div class="pull-left">
                        <div class="pull-left" style="width:35px; line-height:1;">
                            <div style="height:9px; margin:5px 0;">5 <span class="glyphicon glyphicon-star"></span></div>
                        </div>
                        <div class="pull-left" style="width:180px;">
                            <div class="progress" style="height:9px; margin:8px 0;">
                              <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="5" aria-valuemin="0" aria-valuemax="5" style="width: <?php echo $fiveStarRatingPercent; ?>">
                                <span class="sr-only"><?php echo $fiveStarRatingPercent; ?></span>
                              </div>
                            </div>
                        </div>
                        <div class="pull-right" style="margin-left:10px;"><?php echo $fiveStarRating; ?></div>
                    </div>

                    <div class="pull-left">
                        <div class="pull-left" style="width:35px; line-height:1;">
                            <div style="height:9px; margin:5px 0;">4 <span class="glyphicon glyphicon-star"></span></div>
                        </div>
                        <div class="pull-left" style="width:180px;">
                            <div class="progress" style="height:9px; margin:8px 0;">
                              <div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="4" aria-valuemin="0" aria-valuemax="5" style="width: <?php echo $fourStarRatingPercent; ?>">
                                <span class="sr-only"><?php echo $fourStarRatingPercent; ?></span>
                              </div>
                            </div>
                        </div>
                        <div class="pull-right" style="margin-left:10px;"><?php echo $fourStarRating; ?></div>
                    </div>
                    <div class="pull-left">
                        <div class="pull-left" style="width:35px; line-height:1;">
                            <div style="height:9px; margin:5px 0;">3 <span class="glyphicon glyphicon-star"></span></div>
                        </div>
                        <div class="pull-left" style="width:180px;">
                            <div class="progress" style="height:9px; margin:8px 0;">
                              <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="3" aria-valuemin="0" aria-valuemax="5" style="width: <?php echo $threeStarRatingPercent; ?>">
                                <span class="sr-only"><?php echo $threeStarRatingPercent; ?></span>
                              </div>
                            </div>
                        </div>
                        <div class="pull-right" style="margin-left:10px;"><?php echo $threeStarRating; ?></div>
                    </div>
                    <div class="pull-left">
                        <div class="pull-left" style="width:35px; line-height:1;">
                            <div style="height:9px; margin:5px 0;">2 <span class="glyphicon glyphicon-star"></span></div>
                        </div>
                        <div class="pull-left" style="width:180px;">
                            <div class="progress" style="height:9px; margin:8px 0;">
                              <div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="2" aria-valuemin="0" aria-valuemax="5" style="width: <?php echo $twoStarRatingPercent; ?>">
                                <span class="sr-only"><?php echo $twoStarRatingPercent; ?></span>
                              </div>
                            </div>
                        </div>
                        <div class="pull-right" style="margin-left:10px;"><?php echo $twoStarRating; ?></div>
                    </div>
                    <div class="pull-left">
                        <div class="pull-left" style="width:35px; line-height:1;">
                            <div style="height:9px; margin:5px 0;">1 <span class="glyphicon glyphicon-star"></span></div>
                        </div>
                        <div class="pull-left" style="width:180px;">
                            <div class="progress" style="height:9px; margin:8px 0;">
                              <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="1" aria-valuemin="0" aria-valuemax="5" style="width: <?php echo $oneStarRatingPercent; ?>">
                                <span class="sr-only"><?php echo $oneStarRatingPercent; ?></span>
                              </div>
                            </div>
                        </div>
                        <div class="pull-right" style="margin-left:10px;"><?php echo $oneStarRating; ?></div>
                    </div>
                </div>      
                <div class="col-sm-3">
                    <button type="button" id="rateProduct" class="btn btn-default">Rate this product</button>
                </div>      
            </div>
            <div class="row">
                <div class="col-sm-7">
                    <hr/>
                    // Review Place
                    <div class="review-block">      
                    <?php
                    $ratinguery = "SELECT ratingId, itemId, userId, ratingNumber, title, comments, created, modified FROM item_rating";
                    $ratingResult = mysqli_query($conn, $ratinguery) or die("database error:". mysqli_error($conn));
                    while($rating = mysqli_fetch_assoc($ratingResult)){
                        $date=date_create($rating['created']);
                        $reviewDate = date_format($date,"M d, Y");          
                    ?>              
                        <div class="row">
                            <div class="col-sm-3">
                                <img src="image/profile.png" class="img-rounded">
                                <div class="review-block-name">By <a href="#">phpcode</a></div>
                                <div class="review-block-date"><?php echo $reviewDate; ?></div>
                            </div>
                            <div class="col-sm-9">
                                <div class="review-block-rate">
                                    <?php
                                    for ($i = 1; $i <= 5; $i++) {
                                        $ratingClass = "btn-default btn-grey";
                                        if($i <= $rating['ratingNumber']) {
                                            $ratingClass = "btn-warning";
                                        }
                                    ?>
                                    <button type="button" class="btn btn-xs <?php echo $ratingClass; ?>" aria-label="Left Align">
                                      <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                                    </button>                               
                                    <?php } ?>
                                </div>
                                <div class="review-block-title"><?php echo $rating['title']; ?></div>
                                <div class="review-block-description"><?php echo $rating['comments']; ?></div>
                            </div>
                        </div>
                        <hr/>                   
                    <?php } ?>
                    </div>
                </div>
            </div>  
        </div>
        <div id="ratingSection" style="display:none;">
            <div class="row">
                <div class="col-sm-12">
                    <form id="ratingForm" method="POST">                    
                        <div class="form-group">
                            <h4>Rate this product</h4>
                            <button type="button" class="btn btn-warning btn-sm rateButton" aria-label="Left Align">
                              <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                            </button>
                            <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">
                              <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                            </button>
                            <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">
                              <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                            </button>
                            <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">
                              <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                            </button>
                            <button type="button" class="btn btn-default btn-grey btn-sm rateButton" aria-label="Left Align">
                              <span class="glyphicon glyphicon-star" aria-hidden="true"></span>
                            </button>
                            <input type="hidden" class="form-control" id="rating" name="rating" value="1">
                            <input type="hidden" class="form-control" id="itemId" name="itemId" value="12345678">
                        </div>              
                        <div class="form-group">
                            <label for="usr">Title*</label>
                            <input type="text" class="form-control" id="title" name="title" required>
                        </div>
                        <div class="form-group">
                            <label for="comment">Comment*</label>
                            <textarea class="form-control" rows="5" id="comment" name="comment" required></textarea>
                        </div>
                        <?php if (isset($_SESSION['user_id'])) { ?>
                        <div class="form-group">
                            <button type="submit" class="btn btn-info" id="saveReview">Save Review</button> <button type="button" class="btn btn-info" id="cancelReview">Cancel</button>
                        </div>  
                        <?php }
                        else { ?>
                            <div class="well" style="margin-top: 20px;">
                        <h4 class="text-center"><a href="login.php">Sign in</a> to post a comment</h4>
                    </div>
                    <?php } ?>              
                    </form>
                </div>
            </div>
        </div>              
    </div>  
    <?php include('footer.php');?>

in this line of code

<div class="row">
                        <div class="col-sm-3">
                            <img src="image/profile.png" class="img-rounded">
                            <div class="review-block-name">By <a href="#">phpcode</a></div>
                            <div class="review-block-date"><?php echo $reviewDate; ?></div>
                        </div>

How can I show the name of the user logging in this field?

<div class="review-block-name">By <a href="#">phpcode</a></div>

she will comment and her name will appear. When someone else logs in, if they comment in the same way, their name will appear. Whatever I tried did not happen.

Can you help me ?

 

 

Ekran Alıntısı.PNG

Link to comment
Share on other sites

21 minutes ago, gw1500se said:

Perhaps I do not understand what you want but why does using $_SESSION['user_id'] not work?

                            <div class="review-block-name"><?php echo $_SESSION['user_name']; ?></div>

not working. It doesn't work that way anyway

Ekran Alıntısı.PNG

Link to comment
Share on other sites

14 minutes ago, gw1500se said:

What is the error? Are you setting that session variable correctly? Did you output $_SESSION to make sure it is what you expect?


echo "<pre>";
print_r($_SESSION);
echo "</pre>";

unfortunately it doesn't work in this area. do you understand what i mean? please examine my problem above. I would be glad if you examine the code.

$_SESSION works smoothly at the login system. but what I mean is another. By phpcode instead - By "logged in user" . does not write the name of the commenter.

 

Edited by Jordanxl
Link to comment
Share on other sites

The error message is quite clear. You are not setting 'user_id' in the session variable at least in that case. Where is the code that sets that variable. If you printed $_SESSION, like I suggested, you would see the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.