无法获得分页结果

时间:2014-03-10 02:23:27

标签: php mysql pagination

美好的一天,我一直在反对这一点,除非我错过了应该如何工作,然后我最好回到绘图板。我上去找了一个脚本来搜索我的数据库和我拥有的50,000条记录。我在互联网上搜索了所有要知道的内容以及在哪里获得分页或它将如何工作,但我找到的只是编码。由于我是mysql和PHP的相对新手,我不知所措。

<?php

error_reporting(0);
include("config.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GT Entertainment Song Database</title>
<script type="text/javascript"     src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript"     src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"     rel="stylesheet" type="text/css"/>
<style>
BODY, TD {
        font-family:Arial, Helvetica, sans-serif;
        font-size:12px;
}
</style>
</head>


<body>
<form id="form1" name="form1" method="post" action="search.php">
<label>Date Added</label>
<select name="clock">
<option value="">--</option>

<?php
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY clock ORDER BY clock     LIMIT 1,40";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["clock"]."'".    ($row["clock"]==$_REQUEST["clock"] ? " selected" : "").">".$row["clock"]."</option>";
        }
?>
</select>
<label>Title or Artist:</label>
<input type="text" name="string" id="string" value="<?php echo     stripcslashes($_REQUEST["string"]); ?>" />
<label>Disk</label>
<select name="Disk">
<option value="">--</option>

<?php
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY Disk ORDER BY Disk     LIMIT 1,40";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["Disk"]."'".($row["Disk"]==$_REQUEST["Disk"]     ? " selected" : "").">".$row["Disk"]."</option>";
        }
?><BR>
</select><BR>
<label>Duet</label>
<select name="Duet">
<option value="">--</option>
<?php
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY Duet ORDER BY Duet     LIMIT 1,40";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["Duet"]."'".($row["Duet"]==$_REQUEST["Duet"]     ? " selected" : "").">".$row["Duet"]."</option>";
        }
?>
</select>
<label>Comment</label>
<select name="Comment">
<option value="">--</option>
<?php
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY Comment ORDER BY     Comment LIMIT 1,40";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["Comment"]."'".    ($row["Comment"]==$_REQUEST["Comment"] ? " selected" : "").">".$row["Comment"]."</option>";
        }
?>
</select>
<input type="submit" name="button" id="button" value="Filter" />
  </label>
  <a href="search.php">
  reset</a>
</form>
<br /><br />
<table width="700" border="1" cellspacing="0" cellpadding="4">
      <tr>
    <td width="90" bgcolor="#CCCCCC"><strong>Date Added</strong></td>
    <td width="95" bgcolor="#CCCCCC"><strong>Track</strong></td>
    <td width="159" bgcolor="#CCCCCC"><strong>Artist</strong></td>
    <td width="191" bgcolor="#CCCCCC"><strong>Disk</strong></td>
    <td width="113" bgcolor="#CCCCCC"><strong>Duet</strong></td>
    <td width="113" bgcolor="#CCCCCC"><strong>Comment</strong></td>
  </tr>
<?php

// Start query
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE 1=1 ";

if ($_REQUEST["string"]!='') {
        $sql .= " AND (Track LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'     OR Artist LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}
if ($_REQUEST["Disk"]!='') {
        $sql .= " AND Disk='".mysql_real_escape_string($_REQUEST["Disk"])."'"; 
}

if ($_REQUEST["Comment"]!='') {
        $sql .= " AND Comment='".mysql_real_escape_string($_REQUEST["Comment"])."'";
}
if ($_REQUEST["clock"]!='') {
        $sql .= " AND clock='".mysql_real_escape_string($_REQUEST["clock"])."'";
}
if ($_REQUEST["Duet"]!='') {
        $sql .= " AND Duet='".mysql_real_escape_string($_REQUEST["Duet"])."'";
}
$sql .= " LIMIT 40";

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL     query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
        while ($row = mysql_fetch_assoc($sql_result)) {

?>
  <tr>
    <td><?php echo $row["clock"]; ?></td>
    <td><?php echo $row["Track"]; ?></td>
    <td><?php echo $row["Artist"]; ?></td>
    <td><?php echo $row["Disk"]; ?></td>
    <td><?php echo $row["Duet"]; ?></td>
    <td><?php echo $row["Comment"]; ?></td>
  </tr>
<?php
        }
} else {
?>
<tr><td colspan="6">No results found.</td>
<?php  
}
?>

现在我已经查看了以下代码,我似乎无法将其合并到我当前正在运行的脚本中,以便在结果数量高于我设置的限制时将结果拆分为单独的页面。

我发现了这些信息Here

<?php 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
$sql = "SELECT * FROM students ORDER BY name ASC LIMIT $start_from, 20"; 
$rs_result = mysql_query ($sql,$connection); 
?> 
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><? echo $row["Name"]; ?></td>
            <td><? echo $row["PhoneNumber"]; ?></td>
            </tr>
<?php 
}; 
?> 
</table>
<?php 
$sql = "SELECT COUNT(Name) FROM students"; 
$rs_result = mysql_query($sql,$connection); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 20); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
}; 
?>

每当我尝试使其工作或将代码放在适合的位置时,我一直都会遇到错误而且我不知所措。

以下是我试图为我做分页工作的原因,我不确定我是否朝着正确的方向前进

<?php
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." ORDER BY clock ASC LIMIT     $start_from, 20";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["clock"]."'".    ($row["clock"]==$_REQUEST["clock"] ? " selected" : "").">".$row["clock"]."</option>";
        }
?>
</select>
<label>Title or Artist:</label>
<input type="text" name="string" id="string" value="<?php echo     stripcslashes($_REQUEST["string"]); ?>" />
<label>Disk</label>
<select name="Disk">
<option value="">--</option>

<?php
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." ORDER BY Disk ASC LIMIT     $start_from, 20";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["Disk"]."'".($row["Disk"]==$_REQUEST["Disk"]     ? " selected" : "").">".$row["Disk"]."</option>";
        }
?><BR>
</select><BR>
<label>Duet</label>
<select name="Duet">
<option value="">--</option>
<?php
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." ORDER BY Duet ASC LIMIT     $start_from, 20";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["Duet"]."'".($row["Duet"]==$_REQUEST["Duet"]     ? " selected" : "").">".$row["Duet"]."</option>";
        }
?>
</select>
<label>Comment</label>
<select name="Comment">
<option value="">--</option>
<?php
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." ORDER BY Comment ASC LIMIT     $start_from, 20";
        $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not     execute SQL query" '.$sql);
        while ($row = mysql_fetch_assoc($sql_result)) {
                echo "<option value='".$row["Comment"]."'".    ($row["Comment"]==$_REQUEST["Comment"] ? " selected" : "").">".$row["Comment"]."</option>";
        }
?>
</select>
<input type="submit" name="button" id="button" value="Filter" />
  </label>
  <a href="search.php">
  reset</a>
</form>
<br /><br />
<table width="700" border="1" cellspacing="0" cellpadding="4">
  <tr>
    <td width="90" bgcolor="#CCCCCC"><strong>Date Added</strong></td>
    <td width="95" bgcolor="#CCCCCC"><strong>Track</strong></td>
    <td width="159" bgcolor="#CCCCCC"><strong>Artist</strong></td>
    <td width="191" bgcolor="#CCCCCC"><strong>Disk</strong></td>
    <td width="113" bgcolor="#CCCCCC"><strong>Duet</strong></td>
    <td width="113" bgcolor="#CCCCCC"><strong>Comment</strong></td>
  </tr>
<?php

// Start query
$sql = "SELECT COUNT * FROM ".$SETTINGS["data_table"].";

if ($_REQUEST["string"]!='') {
        $sql .= " AND (Track LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'     OR Artist LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}
if ($_REQUEST["Disk"]!='') {
        $sql .= " AND Disk='".mysql_real_escape_string($_REQUEST["Disk"])."'"; 
}

if ($_REQUEST["Comment"]!='') {
        $sql .= " AND Comment='".mysql_real_escape_string($_REQUEST["Comment"])."'";
}
if ($_REQUEST["clock"]!='') {
        $sql .= " AND clock='".mysql_real_escape_string($_REQUEST["clock"])."'";
}
if ($_REQUEST["Duet"]!='') {
        $sql .= " AND Duet='".mysql_real_escape_string($_REQUEST["Duet"])."'";
}

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL     query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
        while ($row = mysql_fetch_assoc($sql_result)) {
$total_records = $row[0]; 
$total_pages = ceil($total_records / 20); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='search.php?page=".$i."'>".$i."</a> "; 
}; 
?>
  <tr>
    <td><?php echo $row["clock"]; ?></td>
    <td><?php echo $row["Track"]; ?></td>
    <td><?php echo $row["Artist"]; ?></td>
    <td><?php echo $row["Disk"]; ?></td>
    <td><?php echo $row["Duet"]; ?></td>
    <td><?php echo $row["Comment"]; ?></td>
  </tr>
<?php
        }
} else {
?>
<tr><td colspan="6">No results found.</td>
<?php  
}
?>

</table>

任何帮助都会受到赞赏 提前谢谢大家

0 个答案:

没有答案