How to define and store events on first page into DB, to retrieve and processes them on second page?

时间:2018-03-25 19:59:55

标签: javascript php

Description of actual state: Status_system.php code below stores information about posts into notification table. If You check the last line where Mysqli_query is, you will see which variables are processed.

 $friends = array();
    $query = mysqli_query($db_conx, "SELECT user1 FROM friends WHERE user2='$log_username' AND accepted='1'");
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { array_push($friends, $row["user1"]); }
    $query = mysqli_query($db_conx, "SELECT user2 FROM friends WHERE user1='$log_username' AND accepted='1'");
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { array_push($friends, $row["user2"]); }
    for($i = 0; $i < count($friends); $i++){
        $friend = $friends[$i];
        $app = "Status Post";
        $note = $log_username.' posted on: <br /><a href="user.php?u='.$account_name.'#status_'.$id.'">'.$account_name.'&#39;s Profile</a>';
        mysqli_query($db_conx, "INSERT INTO notifications(username, initiator, app, note, date_time) VALUES('$friend','$log_username','$app','$note',now())");

Data from notivications table (table from previous code snippet) is then procesed in notification.php

$notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username'  ORDER BY date_time DESC";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
    $notification_list = "You do not have any notifications";
} else {
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $noteid = $row["id"];
        $initiator = $row["initiator"];
        $app = $row["app"];
        $note = $row["note"];
        $date_time = $row["date_time"];
        $date_time = strftime("%b %d, %Y", strtotime($date_time));
        $notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
    }

to display value of $notification_list (check last line of previous code snippet):

<div id="notesBox"><h2>Notifications</h2><?php echo $notification_list; ?></div>

On attached picture you may see how one notification looks enter image description here

Description of problem: All previous code works fine. But displayed notifications will accumulate, if i don't delete them once i did check the note from the list. I want notification.php to display notifications which have not been seen yet.

Solution: I think that adding a variable: did_read to notification table, and to set its value to "1" once the link in last row of notification is pressed (check attached picture), would solve the problem, if I add condition to check did_read variable in notification.php (in second line of second code snipet):

$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' AND did_read='0' ORDER BY date_time DESC";

If i am not mistaking I think that a link from last line of attached picture is formed in one line before the last line in Status_system.php. To proceed as i imagine, it would be then to target the line in notification table, where actual notification is by its notification id and set did_read to "1": Here i need help, i don't know how-when-where to execute next line of code

$sql = "UPDATE notifications SET did_read='1' WHERE id='$noteID' LIMIT 1";

I would appreciate a lot, if i could get help. Or pleas instruct me how to get to solution. I hope i did not made some mistakes in illustrating of this case. But for sure I am willing to give you any information to get to final result.

1 个答案:

答案 0 :(得分:0)

成功。

我接下来做了: 要 status_system.php ,我确实添加了onclick功能:

$note = $log_username.' posted on: <br /><a  href="user.php?u='.$account_name.'#status_'.$id.'" onclick=" return updateDid_read();">'.$account_name.'&#39;s Profile</a>';

notifications.php ,我确实添加了&#34; did_read =&#39; 0&#39;&#34;条件:

$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' AND did_read='0' ORDER BY date_time DESC";

和&#34; id = $ noteid&#34;有通知ID;

 $notification_list .= "<p id=$noteid><a  href='user.php?u=$initiator'>$initiator</a> | $app <br />$note</p>";

以及更新数据库表的所有下一个代码段:

<?php 
   if (isset($_POST["noteID"]) ) { 
      $noteID= mysqli_real_escape_string($db_conx, $_POST['noteID']);
     $sql = "UPDATE notifications SET did_read='1' WHERE id= '$noteID' LIMIT 1";
     $query = mysqli_query($db_conx, $sql);
       }
?>

以及所有下一个代码片段以获取符号ID并发布到php:

function updateDid_read(){

var noteID=document.activeElement.parentElement.id;

$.ajax({
  method:"POST",
  url: '/wp-content/themes/n4/n4/notifications.php',
  data:  {
      "noteID":noteID
   },
   datatype: "text",
  success: function(strdate){
     },
     error: function(error, txtStatus) {
      console.log(txtStatus);
      console.log('error');
    }
  });
}