php信息不通过会话和分页

时间:2014-12-04 14:01:09

标签: javascript php jquery mysql

所以我一直在看这个页面几个小时,我似乎无法弄清楚这里的问题是什么。我之前的所有链接都有这个问题,我会舔过一个链接来过滤/排序数据库结果,点击页面链接转到下一页,它会显示第二页而没有任何过滤结果。我为#34; Most Wins"," Best Save%"和#34;最佳目标反对"通过使用会话,但由于某种原因,当我尝试通过我的范围滑块形式过滤结果时它仍然这样做。当我调整滑块并提交时,它可以正常工作,但是如果我再次点击另一个页面,它会在数据库中显示所有结果。任何人都可以看到它为什么这样做?我已尝试从表单中发布和获取方法,但它似乎没有工作,但如果有人可以提供一些建议我会非常感激!!

以下是代码:

<body>
    <header>
        <div class="header-container">
            <nav>
                <a href="index.php"><img src="images/header_img.png" alt="Golaie Gear Online"/></a>
                <ul>
                    <li><a href="listings.php?listing=default">Browse Goalies</a></li>
                    <li><a href="gearlist.php">Browse Gear</a></li>
                    <li><a href="admin/login.php" style="color:#9a9a9a;">Admin</a></li>
                </ul>
                <form method="get" action="<?php echo "generalsearch.php?q=$searchvalue"; ?>" class="search-field">
                        <input name="search" type="text" placeholder="Search">
                        <button type="submit" title="Search" id="submit"><img src="images/header_srch.png" alt="search"/></button>
                </form>
            </nav>
        </div>
        <div style="clear:both;"></div>


    </header>


<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">

<style type="text/css">

#win-range, #gaa-range, #sv-range{
    width: 160px;
    font-size: 10px;
    margin: 0 auto;     
}
#win-range a, #gaa-range a, #sv-range a{
    margin-top: 0px !important;
    padding: 0 !important;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script>
$(function(){

    $("#win-range").slider({
    range: true,
    min: 1,
    max: 1000,
    values: [1, 1000],
    slide: function(event, ui) {
        // in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
        $("#minwins").val(ui.values[0]);
        $("#maxwins").val(ui.values[1]);
        $("#winamount").val(ui.values[0] + " - " + ui.values[1]);
    }
});
$("#winamount").val($("#win-range").slider("values", 0) + " - " + $("#win-range").slider("values", 1));

    });
$(function(){

    $("#gaa-range").slider({
    range: true,
    min: 0,
    max: 10,
    values: [0, 10],
    slide: function(event, ui) {
        // in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
        $("#mingaa").val(ui.values[0]);
        $("#maxgaa").val(ui.values[1]);
        $("#gaaamount").val(ui.values[0] + " - " + ui.values[1]);
    }
});
$("#gaaamount").val($("#gaa-range").slider("values", 0) + " - " + $("#gaa-range").slider("values", 1));

    });
$(function(){

    $("#sv-range").slider({
    range: true,
    min: 750,
    max: 1000,
    values: [750, 1000],
    slide: function(event, ui) {
        // in order to pass the user selected values to your app, we will use jQuery to prepopulate certain hidden form elements, then grab those values from the $_POST
        $("#minsv").val(ui.values[0]);
        $("#maxsv").val(ui.values[1]);
        $("#svamount").val(ui.values[0] + " - " + ui.values[1]);
    }
});
$("#svamount").val($("#sv-range").slider("values", 0) + " - " + $("#sv-range").slider("values", 1));

    });
</script>

<?php 
include("includes/header.php");
include("includes/mysqli_connect.php");

$sortDesc = $_REQUEST['sortstats'];
$sortAsc = $_REQUEST['sortstatslow'];
$minwins = $_GET['minwins'];
$maxwins = $_GET['maxwins'];
$mingaa = $_GET['mingaa'];
$maxgaa = $_GET['maxgaa'];
$minsv = $_GET['minsv'];
$maxsv = $_GET['maxsv'];
// FILTERING YOUR DB
$sortstats = $_GET['sortstats'];
$sortstatslow = $_GET['sortstatslow'];
$getminwins = $_REQUEST['getminwins'];
$getmaxwins = $_REQUEST['getmaxwins'];
$getmingaa = $_REQUEST['getmingaa'];
$getmaxgaa = $_REQUEST['getmaxgaa'];
$getminsv = $_REQUEST['getminsv'];
$getmaxsv = $_REQUEST['getmaxsv'];
// paging
        $getcount = mysqli_query ($con,"SELECT COUNT(*) FROM Player");
        $postnum = mysqli_result($getcount,0);// this needs a fix for MySQLi upgrade; see custom function below
        $limit = 6; //how many blog posts per page you will see.
        if($postnum > $limit){
        $tagend = round($postnum % $limit,0);
        $splits = round(($postnum - $tagend)/$limit,0);

        if($tagend == 0){
        $num_pages = $splits;
        }else{
        $num_pages = $splits + 1;
        }

        if(isset($_GET['pg'])){
        $pg = $_GET['pg'];
        }else{
        $pg = 1;
        }
        $startpos = ($pg*$limit)-$limit;
        $limstring = "LIMIT $startpos,$limit";
        }else{
        $limstring = "LIMIT 0,$limit";
        }

        // MySQLi upgrade: we need this for mysql_result() equivalent
        function mysqli_result($res, $row, $field=0) { 
            $res->data_seek($row); 
            $datarow = $res->fetch_array(); 
            return $datarow[$field]; 
        }


?>
<div class="listingcontainer">
    <div class="sidebar">
        <h3>Sort By:</h3>
        <a href="listings.php?sortstats=Wins">Most Wins</a>
        <a href="listings.php?sortstatslow=GAA">Best Goals Against</a>
        <a href="listings.php?sortstats=SavePerc">Best Save %</a>
        <hr/>
        <h3>Custom Filter</h3>
        <br/>
        <div class="custom-filter">
            <form name="filters" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get" id="filters">
                <label for="winamount">Win Range:</label>
                <input type="text" id="winamount" />
                <div style="clear:both;"></div>
                <input type="hidden" id="minwins" name="minwins" value="0" />
                <input type="hidden" id="maxwins" name="maxwins" value="1000" />
               <div id="win-range"></div>
               <br/>
               <label for="gaaamount">GAA:</label>
                <input type="text" id="gaaamount" /><br />
                <div style="clear:both;"></div>
                <input type="hidden" id="mingaa" name="mingaa" value="0" />
                <input type="hidden" id="maxgaa" name="maxgaa" value="10" />
               <div id="gaa-range"></div>
                <br/>
               <label for="svamount">SV %:</label>
                <input type="text" id="svamount" /><br />
                <div style="clear:both;"></div>
                <input type="hidden" id="minsv" name="minsv" value="750" />
                <input type="hidden" id="maxsv" name="maxsv" value="1000" />
               <div id="sv-range"></div>
                <input type="submit" name="submit" id="submit"/>
            </form>
        </div>
    </div>
    <div class="main-listings">
    <h1>Current NHL Goaltenders</h1>
    <?php 
        session_start();


        if($_SESSION['allresults'])
        {
            $result = mysqli_query($con, "SELECT * FROM Player ORDER BY PlayerID ASC $limstring");

            if(isset($sortstats)){//THIS WORKS
            session_start();
            $_SESSION['sortStatsDesc'] = session_id();
            $result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstats DESC $limstring ");
            $filter = "sortstats={$sortDesc}";
            }

            if(isset($sortstatslow)) {//THIS WORKS
                session_start();
                $_SESSION['sortStatsAsc'] = session_id();
                $result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstatslow ASC $limstring ");
                $filter = "sortstatslow={$sortAsc}";
            }

            if(isset($minwins) || isset($maxwins) || isset($mingaa) || isset($maxgaa) || isset($minsv) || isset($maxsv))//THIS SEEMS TO WORK
            {
                session_start();
                $_SESSION['customFilter'] = session_id();
                $result = mysqli_query($con, "SELECT * FROM Player WHERE Wins BETWEEN '$minwins' AND '$maxwins' AND
                                                            GAA BETWEEN '$mingaa' AND '$maxgaa' AND SavePerc BETWEEN '$minsv' AND '$maxsv'
                                                            ORDER BY PlayerID ASC $limstring") or die (mysql_error());
                $filter = "getminwins={$minwins}&getmaxwins={$maxwins}&getmingaa={$mingaa}&getminsv={$minsv}&getmaxsv={$maxsv}";

            }
        }
        else if($_SESSION['sortStatsDesc'])//THIS WORKS
        {
            $result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstats DESC $limstring ");
        }
        else if($_SESSION['sortStatsAsc'])//THIS WORKS
        {
            $result = mysqli_query($con,"SELECT * FROM Player ORDER BY $sortstatslow ASC $limstring ");
        }
        else if($_SESSION['customFilter'])//DON'T KNOW IF THIS IS DOING ANYTHING
        {

            $result = mysqli_query($con, "SELECT * FROM Player WHERE Wins BETWEEN '$getminwins' AND '$getmaxwins' AND
                                                            GAA BETWEEN '$getmingaa' AND '$getmaxgaa' AND SavePerc BETWEEN '$getminsv' AND '$getmaxsv'
                                                            ORDER BY PlayerID ASC $limstring");
        }
        else{

        }

        while($row = mysqli_fetch_array($result)){
            $name = $row['LastName'] . ", " . $row['FirstName'];
            $wins = $row['Wins'];
            $pid = $row['PlayerID'];
            $image = $row['Picture'];
            $gaa = $row['GAA'];
            $sv = $row['SavePerc'];
            echo "<div class=\"player-listing\">";
            echo "<div class=\"image-holder\">";
            echo "<span class=\"helper\"></span>";
            echo "<a href=\"viewplayer.php?playerId=$pid\"><img src=\"admin/thumbs/$image\" alt=\"$name\"></a>";
            echo "</div>";
            echo "<div style=\"clear:both;\"></div>";
            echo "<a href=\"viewplayer.php?playerId=$pid\">$name</a>";
            echo "<table align=\"center\">";
            echo "<tr>";
            echo "<td style=\"border-bottom: 1px solid #212121;\">Wins</td>";
            echo "<td style=\"border-bottom: 1px solid #212121;\">GAA</td>";
            echo "<td style=\"border-bottom: 1px solid #212121;\">SV%</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td>$wins</td>";
            echo "<td>$gaa</td>";
            echo "<td>.$sv</td>";
            echo "</tr>";
            echo "</table>";
            echo "</div>";
        }

        // paging links:
        echo "<div class=\"paging\">";
        if($postnum > $limit){
            echo "<span class=\"page-numbers\"><strong>Pages:</strong> &nbsp;&nbsp;&nbsp;</span>";
            $n = $pg + 1;
            $p = $pg - 1;
            $thisroot = $_SERVER['PHP_SELF'];

            if($pg > 1){
                echo "<a href=\"$thisroot?pg=$p&$filter\"><< prev</a>&nbsp;&nbsp;";
            }
            for($i=1; $i<=$num_pages; $i++){
                if($i!= $pg){
                    echo "<a href=\"$thisroot?pg=$i&$filter\">$i</a>&nbsp;&nbsp;";
                }else{
                    echo "$i&nbsp;&nbsp;";
                }
            }
            if($pg < $num_pages){
                // INSERT QUERY STRING VARIBLE TO CARRY OVER DB QUERY
                echo "<a href=\"$thisroot?pg=$n&$filter\">next >></a>";
            }
            echo "&nbsp;&nbsp;";
        }
        // end paging
        echo "</div>";

    ?>
    <br/>
    </div>
    <div style="clear:both;"></div>
</div>

编辑:我通过将此添加到我的代码中修复了分页问题:

$getcount = mysqli_query ($con,"SELECT COUNT(*) FROM Player");

    if($_SESSION['customFilter']){
        $getcount = mysqli_query ($con,"SELECT COUNT(*) FROM Player WHERE Wins BETWEEN '$minwins' AND '$maxwins' AND
                                                        GAA BETWEEN '$mingaa' AND '$maxgaa' AND SavePerc BETWEEN '$minsv' AND '$maxsv'");
    }

但是当我点击下一页时,它仍然让我感到悲伤。我不认为过滤器值由于某种原因而存在。

2 个答案:

答案 0 :(得分:0)

删除所有现有的session_start()并在页面的第一条规则上添加一条,例如:

<?php
    session_start();
?>

//Your html/js/php
...
...

答案 1 :(得分:0)

要使会话正常工作,您必须拨打的第一条指令是&#34; session_start()&#34;。你应该只在脚本的最开头调用一次。

<?php session_start() ?>
// rest of code PHP/HTML/JS/CSS/anything