-1

i am trying to display and update read/unread messages. I have a column in database msg_read, that is by default has a value of 0. 0 means read, and 1 means unread. Need guidance to update the status when the message is read. With the guidance of Mr. Andre, the unread messages are showing Bold now, but when read, messages become normal (not bold).

<!-- Display User Messages Details Starts Here -->
<div class="tab-pane userprof-tab" id="tab-10">
    <div class="table-responsive border-top">
        <table class="table table-bordered table-hover mb-0 text-nowrap">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Jetski</th>
                    <th>Sender Email</th>
                    <th>Phone</th>
                    <th>Post Code</th>
                    <th>Message Date</th>
                    <th>Message</th>
                </tr>
            </thead>
            <tbody>

                <?php

                $SelectBoat = mysqli_query($con, "SELECT * FROM seller_contact WHERE seller_id='$user_id'");
                while($row=mysqli_fetch_assoc($SelectBoat)){

                    $full_name          = $row['full_name'];
                    $boatname           = $row['boatname'];
                    $sender_email       = $row['sender_email'];
                    $phone              = $row['phone'];
                    $post_code          = $row['post_code'];
                    $message            = $row['message'];
                    $msg_date           = $row['msg_date'];     
                    $seller_id          = $row['seller_id'];
                    $msg_read           = $row['msg_read'];

                ?>

                <?php 
                if(msg_read=="0"){

                echo '<tr style="font-weight:900">
                    <td><?php echo $row['full_name'];?></td>
                    <td><?php echo $row['boat_name'];?></td>
                    <td><?php echo $row['sender_email'];?></td>
                    <td><?php echo $row['phone'];?></td>
                    <td><?php echo $row['post_code'];?></td>
                    <td><?php echo $row['msg_date'];?></td>

                    <td>
                        <!-- View Message -->
                            <a href="#view<?php echo $row['id'];?>" class="btn btn-purple btn-sm text-white" data-toggle="modal" data-original-title="View"><i class="fa fa-eye"></i></a>
                        <!-- View Message -->

                        <!--****** View Message Modal ******-->
                            <div class="modal fade" id="view<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                    <div class="modal-dialog modal-md" role="document">
                                      <div class="modal-content">
                                        <div class="modal-header">
                                          <h5 class="modal-title"><strong>From <?php echo $row['sender_email'];?></strong> </h5>
                                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                          </button>
                                        </div>
                                            <div class="modal-body">
                                                <p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message'];?></p>
                                            </div>
                                      </div>
                                    </div>
                            </div>
                        <!--****** End View Message Modal ******-->

                    </td>

                </tr>'
                    } ?>else {
                        echo '<tr>
                    <td><?php echo $row['full_name'];?></td>
                    <td><?php echo $row['boat_name'];?></td>
                    <td><?php echo $row['sender_email'];?></td>
                    <td><?php echo $row['phone'];?></td>
                    <td><?php echo $row['post_code'];?></td>
                    <td><?php echo $row['msg_date'];?></td>

                    <td>
                        <!-- View Message -->
                            <a href="#view<?php echo $row['id'];?>" class="btn btn-purple btn-sm text-white" data-toggle="modal" data-original-title="View"><i class="fa fa-eye"></i></a>
                        <!-- View Message -->

                        <!--****** View Message Modal ******-->
                            <div class="modal fade" id="view<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                    <div class="modal-dialog modal-md" role="document">
                                      <div class="modal-content">
                                        <div class="modal-header">
                                          <h5 class="modal-title"><strong>From <?php echo $row['sender_email'];?></strong> </h5>
                                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                          </button>
                                        </div>
                                            <div class="modal-body">
                                                <p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message'];?></p>
                                            </div>
                                      </div>
                                    </div>
                            </div>
                        <!--****** End View Message Modal ******-->

                    </td>

                </tr>'
                    } ?>
        <?php } ?>
            </tbody>
        </table>
    </div>
</div>
Ali akbar
  • 1
  • 5

1 Answers1

0

I found some errors in your code and some structural failures.

  1. if you are working with PHP in a HTML page, use alternative syntax see: https://www.php.net/manual/en/control-structures.alternative-syntax.php
  2. you don't need to rewrite the complete row again for the read message, only put a condicional on the style attribute.
  3. you was calling read_msg instead of $read_msg

new code:

<div class="tab-pane userprof-tab" id="tab-10">
    <div class="table-responsive border-top">
        <table class="table table-bordered table-hover mb-0 text-nowrap">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Jetski</th>
                    <th>Sender Email</th>
                    <th>Phone</th>
                    <th>Post Code</th>
                    <th>Message Date</th>
                    <th>Message</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = mysqli_fetch_assoc($SelectBoat)) : ?>
                    <?php
                    $full_name          = $row['full_name'];
                    $boatname           = $row['boatname'];
                    $sender_email       = $row['sender_email'];
                    $phone              = $row['phone'];
                    $post_code          = $row['post_code'];
                    $message            = $row['message'];
                    $msg_date           = $row['msg_date'];
                    $seller_id          = $row['seller_id'];
                    $msg_read           = $row['msg_read'];

                    ?>
                    <tr <?= ($msg_read == "0") ? 'style="font-weight:900"' : '' ?>>
                        <td><?php echo $row['full_name']; ?></td>
                        <td><?php echo $row['boat_name']; ?></td>
                        <td><?php echo $row['sender_email']; ?></td>
                        <td><?php echo $row['phone']; ?></td>
                        <td><?php echo $row['post_code']; ?></td>
                        <td><?php echo $row['msg_date']; ?></td>

                        <td>
                            <!-- View Message -->
                            <a href="#view<?php echo $row['id']; ?>" class="btn btn-purple btn-sm text-white" data-toggle="modal" data-original-title="View"><i class="fa fa-eye"></i></a>
                            <!-- View Message -->

                            <!--****** View Message Modal ******-->
                            <div class="modal fade" id="view<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div class="modal-dialog modal-md" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title"><strong>From <?php echo $row['sender_email']; ?></strong> </h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body">
                                            <p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message']; ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!--****** End View Message Modal ******-->
                        </td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>

UPDATE:

to update the status of the message just like i told you in the commnent. add this to your code:

add this sttribute to your view button:

onclick="readMSG(this, '<?php echo $row['id']; ?>')"

add this script and modify to work in your case:

<script>
        function readMSG(attribute, id) {
            $(attribute).parent().parent().css('font-weight', 'normal')
            $.ajax({
                type: "GET",
                url: "readmsg.php",
                data: {
                    id: id

                },
                success: function (){
                    $(attribute).parent().parent().css('font-weight', 'normal')
                }
            });
        }
</script>

the php controller file readmsg.php:

$update = mysqli_query($con, "UPDATE `seller_contact` SET `read_msg`=1 WHERE `id` =" . $_GET['id']);
if ($update) {
    return ['success' => 'success'];
} else {
    return ['error' => 'error'];
}

try to understand the code and adapt to work in your case. if work, don't forgot to accept the answer ;)

André Walker
  • 588
  • 10
  • 30
  • Thanks Andre. Can you please also guide me to update the msg_read status to 1. I mean when a user read his message, then 0 needed to be updated to 1 – Ali akbar Aug 13 '20 at 02:55
  • I think to the best way to update is bring the id os the messages and put a `onclick = readMSG ([ID])` in your view message button. after this you create the function `readMSG` to make ajax call to your php controller file that update the record with the respective id – André Walker Aug 13 '20 at 16:12
  • I edited the answer with a guide – André Walker Aug 13 '20 at 18:11
  • Wow. Thanks thanks Andre. It worked like a charm, though i modified it a little, but you are the man. No words to thank you. – Ali akbar Aug 13 '20 at 22:27
  • Glad to help you! :D – André Walker Aug 13 '20 at 22:52