2016-04-17 133 views
1

所以我有一個分頁劇本我一直在建設的分頁一些問題,但是當我加載使用下一個按鈕,一個新頁面正常工作,我的搜索詞$search_term = "%" . $_POST['searchBar'] . "%";丟失。有什麼辦法可以避免這種情況?或將搜索項設置爲設置值?

的URL下一頁是像這樣 - http://examplewebsite.com/user/Courses/SearchResultsPage.php?pn=2

任何幫助,這將不勝感激。

分頁腳本的其餘部分的下方;

<?php 
    $mysqli = new mysqli('localhost', 'user', 'password','db'); 
    if ($mysqli->connect_errno) 
    { 
     die('Database connection failed'); 
    } 
//$m->set_charset('utf8'); 
//here are my main changes 
//turn errors on to develop, back off when you go live 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

$search_term = "%" . $_POST['searchBar'] . "%"; 
$search_param = $_SESSION['$search_term']=$search_term; 
$stmt =$mysqli->prepare("SELECT title, summary, id FROM course WHERE title 
LIKE ?"); 
$stmt->bind_param("s", $search_param); //learn this 
$stmt->execute(); 
$result = $stmt->get_result(); 

//This gets the number of rows in a query result 
$rows = $result->num_rows; 

//number of results per page 
$rows_per_page = 10; 

//shows last page 
$last_page = ceil($rows/$rows_per_page); 
if($last_page < 1){ 
$last_page = 1; 
} 

if(isset($_GET['pn'])){ 
    $page_number = preg_replace('#[^0-9]#', '', $_GET['pn']); 
} else { 
$page_number = 1; 
} 

//makes sure page number is between limits of $page_number 
if($page_number < 1){ 
    $page_number = 1; 
} else if($page_number > $last_page){ 
    $page_number = $last_page; 
} 

// sets the value of items to view 
$limit = 'LIMIT ' .($page_number -1) * $rows_per_page .',' .$rows_per_page; 

//displays to the user the total number of results and the page numbers 
$total_number_of_results = "Search Results (<b>$rows</b>)"; 
$page_user_is_on = "Page <b>$page_number</b> of <b>$last_page</b>"; 

//query again only grabbing the set number of rows depending on page number 
$stmt = $mysqli->prepare("SELECT title, summary, id FROM course WHERE title LIKE ? ".$limit); 
$stmt->bind_param("s", $search_param); 
$stmt->execute(); 
$result = $stmt->get_result(); 


$list = ''; 
while($row = $result->fetch_assoc()){ 
$title = $row['title']; 
$id = $row['id']; 
//I'm assuming you want each link to be different here... 
$list.='<p><a href="Selectedcourse.php?id='.$id.'">' . $title . '</a></p>'; 
} 
mysqli_close($mysqli); 

//set up pagination 
$pagination_controls = ''; 
if($last_page != 1){ 
    if($page_number > 1){ 
     $previous = $page_number - 1; 
     $pagination_controls .='<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">previous</a> &nbsp; &nbsp; '; 
     for($i = $page_number - 4; $i < $page_number; $i++) 
     { 
      if($i > 0){ 
      $pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; '; 
     } 
    } 
} 
$pagination_controls.=''.$page_number.' &nbsp; '; 

//clickable links to the left 
for($i = $page_number+1; $i <= $last_page; $i++) 
    { 
     $pagination_controls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; '; 
     if($i >= $page_number+4){ 
      break; 
     } 
    } 
    if($page_number != $last_page){ 
     $next = $page_number + 1; 
     $pagination_controls.='&nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>'; 
    } 
} 
?> 
<!doctype html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <link rel='stylesheet' href='courses.css'> 
</head> 
<body> 
    <div class="header"> 
<h1>Search Results for - <?= $search_param ?></h1> 
</div> 
<div> 
<h3> <?php echo $page_user_is_on ?> </h3> 
<p><?php echo $list; ?></p> 
<p><?php /* echo $search_result['summary']; //Where was this coming from? */?> </p> 
</div> 
<div id="pagination_controls"><?php echo $pagination_controls; ?></div> 
</body> 
</html> 
+0

郵整版代碼 – Mihai

+0

@Mihai這就是完整的文檔現在有 – bdg

+0

的問題是,你爲什麼使用會話?你之前的問題並沒有使用它們,而是起作用。 –

回答

2

每次點擊下一步,頁面刷新,並因爲你不使用

<?php 
session_start(); 
.... 

替換這些2線

$search_term = "%" . $_POST['searchBar'] . "%"; 
$search_param = $_SESSION['$search_term']=$search_term; 

有:

if(isset($_POST['searchBar'])){ 
    $search_term = "%" . $_POST['searchBar'] . "%"; 
    $search_param = $_SESSION['search'] = $search_term ; 
} 
else { 
    $search_param = $_SESSION['search']; 
} 

在頂部,$_SESSION['$search_term'] i懵了

也會增加一個isset檢查您的文章

編輯

我覺得你的問題是,$ SEARCH_TERM只設置負載那麼接下來的頁面爲NULL和會話也設置爲上空值。

+0

好的,我如何將它嵌入到我的代碼中?就像把'session_start();'放在上面的行一樣簡單嗎? – bdg

+0

@bdg第一行 – Mihai

+0

我已經添加了此,它仍然拋出的錯誤'注意事項:未定義指數:搜索欄在SearchResultsPage.php在線15',從而失去了'$ search_param'值 – bdg