PHP/MySQL の初心者です。
多数の値を含む MySQL テーブルを照会する基本的な検索エンジンを作成しました。ただし、検索フォームには複数の検索ボタンがあり、単一の値のタイプに基づいて検索を制限します (この場合、地震を扱っているため、地域またはマグニチュードによって)。
最初の検索と結果の表示は問題なく終了しますが、ページネーション リンクを使用して結果の別のページに移動しようとすると、次の結果セットが表示されず、ブラウザでクリックすると (私は Firefox を使用していますが、他のブラウザーでこれをテストします)、セッションの有効期限が切れているため、問題をそこに絞り込むことができました (ここで完全に間違ったツリーを吠えている場合を除きます)。
ここで何が問題になっていますか?どうすれば修正できますか?
答えはとてもシンプルな気がしますが、私はそれを見ていません。結果ページのコードは次のとおりです (インターネットで見つけた複数のチュートリアルからハッキングされましたが、コード内で何が起こっているかは理解しています)、どの変数が渡されたかを確認するために var_dump($_SESSION) コマンドを追加しました。この問題が修正されると、セッションは削除されます。
<?php
include('db.php'); // include your code to connect to DB.
session_start();
var_dump($_SESSION);
if (mysql_real_escape_string($_POST['regbutton']) == submit||(!isset($_SESSION['submit1']))||!(isset($_SESSION['submit2']))){
$_SESSION['search']=mysql_real_escape_string($_POST['regbutton']);
$_SESSION['submit1']=mysql_real_escape_string($_POST['place']);
$_SESSION['submit2']=mysql_real_escape_string("empty");
$place =mysql_real_escape_string($_SESSION['submit1']);
$clicked=mysql_real_escape_string($_SESSION['search']);
var_dump($_SESSION);
}
elseif(mysql_real_escape_string($_POST['magbutton']) == submit|| (!isset($_SESSION['submit1']))||!(isset($_SESSION['submit2']))){
$_SESSION['search']=mysql_real_escape_string($_POST['magbutton']);
$_SESSION['submit1']=mysql_real_escape_string($_POST['mag1']);
$_SESSION['submit2']=mysql_real_escape_string($_POST['mag2']);
$mag1 = mysql_real_escape_string($_SESSION['submit1']);
$mag2 = mysql_real_escape_string($_SESSION['submit2']);
$clicked=mysql_real_escape_string($_SESSION['search']);
var_dump($_SESSION);
}
else{
var_dump($_SESSION);
echo "No records found. Session might be broken.";
exit;
}
$tbl_name="quake"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$result = mysql_query($query);
$total_pages = mysql_fetch_array($result);
$total_pages = $total_pages[num];
/* Setup vars for query. */
$targetpage = "resultz.php"; //your file name (the name of this file)
$limit = 30; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
if (mysql_real_escape_string($_POST['regbutton']) == submit){
$query = "SELECT * FROM quake WHERE region LIKE '%of%, $place%' LIMIT $start, $limit";
}
elseif (mysql_real_escape_string($_POST['magbutton']) == submit){
if ($mag2 >= $mag1) {
$query = "SELECT * FROM quake WHERE magnitude BETWEEN '$mag1' and '$mag2' LIMIT $start, $limit";
}
else{
$query = "SELECT * FROM quake WHERE magnitude BETWEEN '$mag2' and '$mag1' LIMIT $start, $limit";
}
}
else{
echo "No records found.";
exit;
}
$result = mysql_query($query) or die(mysql_error());;
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\"> previous</a>";
else
$pagination.= "<span class=\"disabled\"> previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next </a>";
else
$pagination.= "<span class=\"disabled\">next </span>";
$pagination.= "</div>\n";
}
?>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<link rel="stylesheet" media="print" type="text/css" href="print.css" />
<title>Recent Earthquakes</title>
</head>
<div id="header">
QUAKE SEARCH
</div>
<div id="header2">
Search the latest quakes
</div>
<table border=1>
<thead>
<td>Source</td>
<td>EqID</td>
<td>Version</td>
<td>Date/Time</td>
<td>Latitude</td>
<td>Longitude</td>
<td>Magnitude</td>
<td>Depth</td>
<td>NST</td>
<td>Region</td>
</thead>
<?php
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
?>
<?=$pagination?>
そして、ここに検索フォームがあります:
<?php
session_start();
include ('db.php');
?>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<link rel="stylesheet" media="print" type="text/css" href="print.css" />
<title>Recent Earthquakes</title>
</head>
<div id="header">
QUAKE SEARCH
</div>
<div id="header2">
Search the latest quakes
</div>
<div id="search">
<form name="magsearch" action="resultz.php" method="post">
<label>Search by Magnitude:</label>
<select name="mag1">
<option>1.0</option>
<option>2.0</option>
<option>3.0</option>
<option>4.0</option>
<option>5.0</option>
<option>6.0</option>
<option>7.0</option>
<option>8.0</option>
<option>9.0</option>
<option>10.0</option>
</select>
<select name="mag2">
<option value=1>1.0</option>
<option value=2>2.0</option>
<option value=3>3.0</option>
<option value=4>4.0</option>
<option value=5>5.0</option>
<option value=6>6.0</option>
<option value=7>7.0</option>
<option value=8>8.0</option>
<option value=9>9.0</option>
<option>10.0</option>
</select>
<input name="magbutton" type="submit" value="submit" />
<input type="hidden" name="content" value="search">
</form>
<form name ="regsearch" action="resultz.php" method="post">
<label>Search by Region:</label>
<input name="place" type="text" size="14" />
<input name="regbutton" type="submit" value="submit" />
<input type="hidden" name="content" value="search">
</form>
</div>