-3

试图在我的 php 代码中实现分页脚本(采用我在网上找到的示例代码)页面返回 mysql 查询的结果。默认是一个空的“关键字”,它返回数据库中的所有数据。在返回所有数据时分页工作成功,但是当我通过关键字搜索过滤结果时,单击“下一页”会在未过滤的结果中返回下一页(即搜索的关键字在刷新中丢失)

这是相关代码(希望它有意义):

块引用

    $txt1='<A HREF="/memimomedia/music/128kb/';
    $txt2='" ><IMG src="/Save.png" width="24" height="24" align="middle" border="0"></A>';

    $txt9a='<a href="';
    $txt9b='" target="_blank">';
    $txt9c='</a>';

    $txt10a='<audio src="./128kb/';        
    $txt10d='" controls></audio>';


if(empty($_POST['searchkeywords']) && empty($_GET['searchkeywords']))     { $result = mysql_query("SELECT *

FROM ppl_tracks WHERE Mixed !='0' ORDER BY Track"); } if(!empty($_GET['searchkeywords'])) { $Searchword=$_GET['searchkeywords']; }

if(!empty($_POST['searchkeywords']))  { $Searchword=$_POST['searchkeywords']; }

$Totalresults = mysql_query("SELECT Track FROM (ppl_tracks LEFT JOIN TrackStyle ON ppl_tracks.RECNO = TrackStyle.TrackID LEFT JOIN StyleTable ON TrackStyle.StyleID = StyleTable.ID) LEFT JOIN TrackMood ON ppl_tracks.RECNO = TrackMood.TrackID LEFT JOIN MoodTable ON TrackMood .MoodID = MoodTable.ID WHERE (MoodChoices LIKE '%$Searchword%' OR Description LIKE '%$Searchword%' OR StyleChoices LIKE '%$Searchword%' OR Tempo LIKE '%$Searchword%' OR Track LIKE '%$Searchword %' ) AND Mixed =true GROUP BY Track ORDER BY Track" );

//这会检查是否有页码。如果没有,它会将其设置为第 1 页 if(isset($_GET['pagenum'])) { $pagenum = $_GET['pagenum']; } 其他 { $pagenum = 1; }

//这里我们统计结果数 $hits = mysql_num_rows($Totalresults);

//这是每页显示的结果数 $page_hits = 15;

//这告诉我们最后一页的页码 $last = ceil($hits/$page_hits);

//这确保页码不小于一,或大于我们的最大页数 if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; }

//这将设置在我们的查询中显示的范围 $max = 'limit ' .($pagenum - 1) * $page_hits .',' .$page_hits;

$result = mysql_query("SELECT * FROM (ppl_tracks LEFT JOIN TrackStyle ON ppl_tracks.RECNO = TrackStyle.TrackID LEFT JOIN StyleTable ON TrackStyle.StyleID = StyleTable.ID) LEFT JOIN TrackMood ON ppl_tracks.RECNO = TrackMood.TrackID LEFT JOIN MoodTable ON TrackMood .MoodID = MoodTable.ID WHERE (MoodChoices LIKE '%$Searchword%' OR Description LIKE '%$Searchword%' OR StyleChoices LIKE '%$Searchword%' OR Tempo LIKE '%$Searchword%' OR Track LIKE '%$Searchword %' ) AND Mixed =true GROUP BY Track ORDER BY Track $max" );

echo "曲目 - 艺术家描述下载";

$color="1";

while($row = mysql_fetch_array($result)) { $SelectedStyles = array(); $Track=$row['Track']; $SelectedStylesQuery = mysql_query(" SELECT StyleChoices FROM ppl_tracks LEFT JOIN TrackStyle ON ppl_tracks.RECNO = TrackStyle.TrackID LEFT JOIN StyleTable ON TrackStyle.StyleID = StyleTable.ID WHERE ppl_tracks.Track='$Track'"); while($row1 = mysql_fetch_array($SelectedStylesQuery)) { $SelectedStyles[] = $row1[StyleChoices]; } $SelectedMoods = 数组();$SelectedMoodsQuery = mysql_query("从 ppl_tracks 中选择 MoodChoices 左加入 TrackMood ON ppl_tracks.RECNO = TrackMood.TrackID 左加入 MoodTable ON TrackMood.MoodID = MoodTable.ID WHERE ppl_tracks.Track='$Track'"); 而($row2 = mysql_fetch_array($SelectedMoodsQuery)) { $SelectedMoods[] = $row2[MoodChoices]; } 排序($SelectedMoods);排序($选定样式);$stringS= implode(", ", $SelectedStyles); $stringM= implode(", ", $SelectedMoods);

if($color==1) { 回声 ""; 回声“”。$row['Track'] 。“——”。ucwords($row['Artist']) 。""; 回声“”。$字符串M 。"
".$stringS.""; 回声“”。$txt1.$row['FileName'].$txt2 。""; 回声“”。$txt10a.$row['FileName'].$txt10d.""; 回声“”;$color="2"; }

否则 { 回声 ""; 回声“”。$row['Track'] 。“——”。ucwords($row['Artist']) 。""; 回声“”。$字符串M 。"
".$stringS.""; 回声“”。$txt1.$row['FileName'].$txt2 。""; 回声“”。$txt10a.$row['FileName'].$txt10d.""; 回声“”;$color="1";}

} 回声“”;

// 这会向用户显示他们在哪个页面,以及总页面数

echo " --page $pagenum of $last--

"; // 首先我们检查我们是否在第一页。如果是,那么我们不需要指向上一页或第一页的链接,所以我们什么也不做。如果不是,那么我们生成到第一页的链接页,然后到上一页。

if ($pagenum == 1)

{

}

别的

{ 回声“ <<-第一”; 回声“-”;$previous = $pagenum-1; 回声“<-上一个”;}

$计数器=1;while ( $counter <= $last ) { if ($counter==$pagenum) { echo " - "; 回声“$计数器”;回声“-”;}

  else

    {
  echo " - ";
  echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$counter'>$counter</a> ";
  echo " - ";
    } $counter = $counter + 1; }

//这和上面一样,只检查我们是否在最后一页,然后生成Next和Last链接

if ($pagenum == $last)

{

}

否则 { $next = $pagenum+1; 回声“下一个->”;回声“-”;回声“最后->>”;} mysql_close($con); ?>

4

1 回答 1

3

每次进入新页面时都会重新加载页面,因此您需要再次将搜索词传递到页面中。最简单的方法是将分页链接更新为以下内容:

echo " <a href='{$_SERVER['PHP_SELF']}?searchkeywords=" . $_REQUEST["searchkeywords"] . "&pagenum=$counter'>$counter</a> ";

$_REQUEST 将处理 $_POST 和 $_GET 变量,但您可能希望以自己的方式执行此操作。简单地说,您只需将搜索关键字传递回您的网址

于 2013-02-08T10:07:40.310 回答