Hello. Help with this code. The point is to take a sample from the database and issue it in php page by page. There is a code that works when the sample is complete. When I add a search form, he considers the page correctly, but on going to another page, the variable gets lost, and I have an empty screen. I understand this variable as it should be saved when moving to the next page, but how. Here is the code itself (at the top in the Html search box):

include('adm/connectdb.php'); $num = 8; if (isset($_POST['search'])) { $page = $_GET['page']; $result = mysql_query("SELECT COUNT(*) FROM `db1` WHERE `name`LIKE \"%$search%\""); $posts = mysql_result($result, 0); $total = intval(($posts - 1) / $num) + 1; $page = intval($page); if (empty($page) or $page < 0) $page = 1; if ($page > $total) $page = $total; $start = $page * $num - $num; $result = mysql_query("SELECT * FROM `db1` WHERE `name`LIKE \"%$search%\" LIMIT $start, $num"); while ($postrow[] = mysql_fetch_array($result)) 
  • Obviously, in the hidden input, display the value and then take it at the next request - Alexey Shimansky
  • It turns out in php code to add input, in which also pass the search variable? - Sergey
  • No need to do a search through the $_POST variable, use $_GET . By the way, what's in the $search variable? - Egor Smolyakov
  • There is some text in the variable according to which the user is looking for info. I changed to $ _GET, but now I’m not looking for anything at all, do I need to change something in the SQL query? - Sergey
  • I remade the get request, the word is written in the browser string (although it is incorrect in Russian), but displays the entire sample, and also the second and next page is empty - Sergey

1 answer 1

Record the search term in the session

 session_start(); if (isset($_POST['search'])) { $search = $_POST['search']; $_SESSION['search'] = $search; } else if (isset($_SESSION['search'])) $search = $_SESSION['search']; if (isset($search)) { $page = $_GET['page']; ......... 

When you need to reset the filter, run

 unset($_SESSION['search']); 
  • with this, I completely fell into the error Warning: session_start () [function.session-start]: Cannot send session cookie and having already searched the Internet, it has already been saved to UTF format without BOM and removed all spaces in the code - Sergey
  • @ Sergey session_start() should be called before any information is displayed in the browser. - Anton Shchyrov
  • Thank you, I brought the php script to the very top, before the html code, but now the form that does not show on the page as html code of millet, how to deal with it? - Sergey
  • @ Sergey The most trivial is to execute session_start() in the first line of the script and everything else is where it is needed - Anton Shchyrov
  • I'm totally confused, I created a blank page, just to not get lost in the code, so it looks to me. Or, first you just need to make the session start string, and close the php tag and continue through the list? <? php include ('adm / connectdb.php'); $ num = 8; session_start (); .............. while ($ postrow [] = mysql_fetch_array ($ result))?> <form method = "post" class = "search"> <input type = "submit"> </ form> - Sergey