设置cookie而不会丢失导航栏

时间:2018-11-04 21:32:56

标签: php html cookies header navbar

我要做的是使用Cookie在我的网站上设置upvote / downvote系统(尝试并允许匿名用户参与)。我遇到的问题是我必须在标题之前设置cookie,并且由于某种原因,每当我单击“ upvote”或“ downvote”按钮时,导航栏就会消失,然后重新出现。索引文件在这里:

<?php
include 'dbconnect.php';
include 'postbuttons.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Announcments</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="stylesheet.php">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<?php include '../header.html'; ?>
</head>
<body>
<?php
include 'pagination.php';
include 'content.php';
?>
</body>
</html>

我的dbconnect文件是:

<?php
$db_host = 'localhost';
$db_name = 'databasename';
$db_user = 'databaseuser';
$db_pass = 'databasepassword';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_SESSION['user'])) {
$res = $conn->query("SELECT * FROM users WHERE id=" . $_SESSION['user']);

if ($res != false) {
    $userRow = mysqli_fetch_array($res, MYSQLI_ASSOC);
}
}
?>

我的帖子按钮页面,这是我认为问题所在:

<?php 
$posts = $conn->query("SELECT * FROM blog");
//$posts = mysqli_fetch_array($postsraw, MYSQLI_ASSOC);
foreach ($posts as $ind) {
setcookie('test'.$ind['id'], 'heck you');
$id = $ind['id'];
$postraw = $conn->query("SELECT * FROM blog WHERE id=$id");
$post = mysqli_fetch_array($postraw, MYSQLI_ASSOC);
if (isset($_POST['vote'.$id])) {
    $postcookie = 'vote' . $id;
    if (isset($_COOKIE[$postcookie])) {
        $data = json_decode($_COOKIE[$postcookie]);
    } else {
        $data = ['neither', '#aaa'];
    }
    if ($_POST['votetype'.$id] == 'up') {
        if (!isset($_COOKIE[$postcookie]) || $data[0] == 'neither' || 
$data[0] == 'down') {
        if ($data[0] == 'down') {
            $change = $post['upvote'] + 1;
            $sql = "UPDATE blog SET upvote=$change WHERE id=$id";
            $query = $conn->query($sql);
            $changedown = $post['downvote'] - 1;
            $othersql = "UPDATE blog SET downvote=$changedown WHERE id=$id";
            $newquery = $conn->query($othersql);
        } else {
            $change = $post['upvote'] + 1;
            $sql = "UPDATE blog SET upvote=$change WHERE id=$id";
            $query = $conn->query($sql);
        }
        $cookievalue = ['up', 'green'];
        setcookie($postcookie, json_encode($cookievalue));
        } else {
            $change = $post['upvote'] - 1;
            $sql = "UPDATE blog SET upvote=$change WHERE id=$id";
            $query = $conn->query($sql);
            $cookievalue = ['neither', '#aaa'];
            setcookie($postcookie, json_encode($cookievalue));
        }
    }
    if ($_POST['votetype'.$id] == 'down') {
        if (!isset($_COOKIE[$postcookie]) || $data[0] == 'neither' || 
$data[0] == 'up') {
            if ($data[0] == 'up') {
                $change = $post['downvote'] + 1;
                $sql = "UPDATE blog SET downvote=$change WHERE id=$id";
                $query = $conn->query($sql);
                $changedown = $post['upvote'] - 1;
                $othersql = "UPDATE blog SET upvote=$changedown WHERE 
id=$id";
                $newquery = $conn->query($othersql);
            } else {
                $change = $post['downvote'] + 1;
                $sql = "UPDATE blog SET downvote=$change WHERE id=$id";
                $query = $conn->query($sql);
            }
            $cookievalue = ['down', 'red'];
            setcookie($postcookie, json_encode($cookievalue));
            } else {
                $change = $post['downvote'] - 1;
                $sql = "UPDATE blog SET downvote=$change WHERE id=$id";
                $query = $conn->query($sql);
                $cookievalue = ['neither', '#aaa'];
                setcookie($postcookie, json_encode($cookievalue));
            }
    }
    echo '<meta http-equiv="Refresh" content="0; url=#'.$id.'">';
}
if(isset($_POST['report'.$id])) {

    if(isset($_COOKIE['report'.$id])) {
        $cdata = json_decode($_COOKIE['report'.$id]);
    } else {
        $cdata = ['notset', 'black'];
    }
    if ($cdata[0] == 'notset') {
        $reportval = ['reported', 'red'];
        setcookie('report'.$id, json_encode($reportval));
        $change = $post['reports'] + 1;
        $sql = $conn->query("UPDATE blog SET reports=$change WHERE id=$id");
    } else {
        $reportval = ['notset', 'black'];
        setcookie('report'.$id, json_encode($reportval));
        $change = $post['reports'] - 1;
        $sql = $conn->query("UPDATE blog SET reports=$change WHERE id=$id");
    }
    echo '<meta http-equiv="Refresh" content="0; url=#'.$id.'">';
}
}
?>

如果有帮助,这是我的content.php页面:

<div class="container-fluid text-center">
<button type="button" class="btn btn-primary float-right" data- 
toggle="modal" 
data-target="#announcmentm">
Add
</button><h1>Announcments:</h1>
</div>
<div class="modal fade" id="announcmentm" tabindex="-1" role="dialog" aria- 
hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title">Add Announcment</h5>
    <button type="button" class="close" data-dismiss="modal" aria- 
label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <?php include 'create.php' ?>
  </div>
</div>
</div>
</div>
<?php
$sql = $conn->query("SELECT id FROM blog");
$pager = new Paginater();
$pager->paginate('blog', 5, $id);
?>

最后是我的header.html页面:

  <nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">STUCO</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
  <ul class="navbar-nav mr-auto">
    <li class="nav-item active">
      <a class="nav-link" href="/">Home</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="/suggestions.html">Suggestions</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="/tasks">Tasks</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="/blog">Blog</a>
    </li>
  </ul>
</div>

您可以在这里看到我要描述的问题:https://stuco.baccaw.host/blog 只需尝试对其中一篇文章进行投票,您就会看到导航栏消失了一点,然后再回来。

0 个答案:

没有答案