使用PHP Session变量

时间:2015-03-22 18:24:28

标签: php jquery html mysql session

我想让这个问题很容易理解:

我有一个搜索表单,当用户搜索某个项目时,该变量是使用session存储的,然后该变量将用于检索为该搜索定制的项目。问题是我不认为我正在使用会话变量。

以下是我想要实现的实例: http://testapplication220-env.elasticbeanstalk.com/testSearch.php

换句话说,我试图复制这个例子+搜索: http://www.sanwebe.com/2013/03/loading-more-results-from-database(底部演示) : 下面是testSearch页面(包含搜索表单和主页面的页面):

<?php
include("includes/db.php");

  if(isset($_GET['user_query']))
    {
session_start();

 $search_query = $_GET['user_query'];
 $_SESSION['userSearch'] = $search_query;
$results = mysqli_query($con,"SELECT COUNT(*) FROM courses where course_title like '%$search_query%'");
$get_total_rows = mysqli_fetch_array($results); //total records

//break total records into pages
$item_per_page = 10;
$total_pages = ceil($get_total_rows[0]/$item_per_page); 
}
?>
<html>
<head>

    <script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>

<script>
$(document).ready(function() {

    var track_click = 0; //track user click on "load more" button, righ now it is 0 click

    var total_pages = <?php echo $total_pages; ?>;

    $('#results').load("fetch_pages.php", {'page':track_click}, function() {track_click++;}); //initial data to load


    $(".load_more").click(function (e) { //user clicks on button

        $(this).hide(); //hide load more button on click
        $('.animation_image').show(); //show loading image

        if(track_click <= total_pages) //user click number is still less than total pages
        {
            //post page number and load returned data into result element
            $.post('fetch_pages.php',{'page': track_click}, function(data) {

                $(".load_more").show(); //bring back load more button

                $("#results").append(data); //append data received from server

                //scroll page smoothly to button id
                $("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500);

                //hide loading image
                $('.animation_image').hide(); //hide loading image once data is received

                track_click++; //user click increment on load button

            }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
                alert(thrownError); //alert with HTTP error
                $(".load_more").show(); //bring back load more button
                $('.animation_image').hide(); //hide loading image once data is received
            });


            if(track_click >= total_pages-1) //compare user click with page number
            {
                //reached end of the page yet? disable load button
                $(".load_more").attr("disabled", "disabled");
            }
         }

        });
});
</script>
</head>
<body>
    <center>
    <form method="get" action="testSearch.php" enctype="multipart/form-data" autocomplete="off">
<input type="text" class="search" id="searchid" name="user_query" id="searchBar" placeholder="Search for courses" />
          <input type="submit" id="searchButton"  name="search" value="search" class="btn btn-danger" autocomplete="off"/>
<div id="results"></div>
<div align="center">
<button class="load_more" id="load_more_button">load More</button>
<div class="animation_image" style="display:none;"><img src="ajax-loader.gif"> Loading...</div>
</div>

</body>
</html>

下面是fetch_page(包含该函数的页面以及我尝试使用会话变量检索用户搜索查询并根据其填充结果的位置):

<?php
include("includes/db.php"); 
session_start();
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
  header('HTTP/1.1 500 Invalid page number!');
  exit();
}
$item_per_page = 10;
//get current starting point of records
$position = ($page_number * $item_per_page);

//Limit our results within a specified range. 
$results = mysqli_query($con,"SELECT course_title, course_date1, course_provider,course_sdesc FROM courses ORDER BY course_date1 where course_title like '%$_SESSION['userSearch']%' DESC LIMIT $position, $item_per_page");

//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
  echo '<li id="item_'.$row["course_title"].'"><span class="page_name">'.$row["course_date1"].') '.$row["course_provider"].'</span><span class="page_message">'.$row["course_sdesc"].'</span></li>';
}
echo '</ul>';

?>

更新: 在testSearch页面上:

<?php
session_start();

include("includes/db.php");

  if(isset($_GET['user_query']))
    {

 $search_query = $_GET['user_query'];
 $_SESSION['userSearch'] = $search_query;
$results = mysqli_query($con,"SELECT COUNT(*) FROM courses where course_title like '%$search_query%'");
$get_total_rows = mysqli_fetch_array($results); //total records

//break total records into pages
$item_per_page = 10;
$total_pages = ceil($get_total_rows[0]/$item_per_page); 
}
?>
<html>
<head>

    <script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>

<script>
$(document).ready(function() {

    var track_click = 0; //track user click on "load more" button, righ now it is 0 click

    var total_pages = <?php echo $total_pages; ?>;

    $('#results').load("fetch_pages.php", {'page':track_click}, function() {track_click++;}); //initial data to load


    $(".load_more").click(function (e) { //user clicks on button

        $(this).hide(); //hide load more button on click
        $('.animation_image').show(); //show loading image

        if(track_click <= total_pages) //user click number is still less than total pages
        {
            //post page number and load returned data into result element
            $.post('fetch_pages.php',{'page': track_click}, function(data) {

                $(".load_more").show(); //bring back load more button

                $("#results").append(data); //append data received from server

                //scroll page smoothly to button id
                $("html, body").animate({scrollTop: $("#load_more_button").offset().top}, 500);

                //hide loading image
                $('.animation_image').hide(); //hide loading image once data is received

                track_click++; //user click increment on load button

            }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
                alert(thrownError); //alert with HTTP error
                $(".load_more").show(); //bring back load more button
                $('.animation_image').hide(); //hide loading image once data is received
            });


            if(track_click >= total_pages-1) //compare user click with page number
            {
                //reached end of the page yet? disable load button
                $(".load_more").attr("disabled", "disabled");
            }
         }

        });
});
</script>
</head>
<body>
    <center>
    <form method="get" action="testSearch.php" enctype="multipart/form-data" autocomplete="off">
<input type="text" class="search" id="searchid" name="user_query" id="searchBar" placeholder="Search for courses" />
          <input type="submit" id="searchButton"  name="search" value="search" class="btn btn-danger" autocomplete="off"/>
<div id="results"></div>
<div align="center">
<button class="load_more" id="load_more_button">load More</button>
<div class="animation_image" style="display:none;"><img src="ajax-loader.gif"> Loading...</div>
</div>

</body>
</html>

在fetch_page上     

//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
  header('HTTP/1.1 500 Invalid page number!');
  exit();
}
$item_per_page = 10;
//get current starting point of records
$position = ($page_number * $item_per_page);

//Limit our results within a specified range. 
$results = mysqli_query($con,"SELECT course_title, course_date1, course_provider,course_sdesc FROM courses ORDER BY course_date1 where course_title like '%$_SESSION['userSearch']%' DESC LIMIT $position, $item_per_page");

//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
  echo '<li id="item_'.$row["course_title"].'"><span class="page_name">'.$row["course_date1"].') '.$row["course_provider"].'</span><span class="page_message">'.$row["course_sdesc"].'</span></li>';
}
echo '</ul>';

?>

0 个答案:

没有答案