PDO分页 - 不显示结果

时间:2013-03-27 14:30:31

标签: pdo pagination

代码正确地提取了数据库中的结果数量,但它没有显示任何结果。它不显示结果,而是显示“其他”条件消息“无法显示结果。”

我无法弄清楚我在这里做错了什么......我看过很多文章而且找不到答案。

<font size="+3" face="Verdana">Database</font>
<br><br>
<form name="form" action="" method="get">
<input type="text" name="q" size="60" />
<input type="submit" name="Submit" value="Search">
</form>

<?php

include 'connect.php';

$var = $_GET['q'];
echo $var;

try {

// Find out how many items are in the table
$total = $conn->query('
SELECT
COUNT(*)
FROM
customers
')->fetchColumn();

// How many items to list per page
$limit = 20;

// How many pages will there be
$pages = ceil($total / $limit);

// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default'   => 1,
'min_range' => 1,
),
)));

// Calculate the offset for the query
$offset = ($page - 1)  * $limit;

// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);

// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a 
href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span   
class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next  
page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : 
'<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

// Display the paging information
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages,  
displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

// Prepare the paged query
$stmt = $conn->prepare('
SELECT * FROM customers 
WHERE 
statusstring LIKE :statusstring OR 
company LIKE :company OR 
city LIKE :city OR 
state LIKE :state OR 
zip LIKE :zip OR 
mainphone LIKE :mainphone OR 
contact LIKE :contact OR 
function LIKE :function OR 
cellphone LIKE :cellphone OR 
email LIKE :email OR 
nextcontactdate LIKE :nextcontactdate 
LIMIT :limit 
OFFSET :offset');

// Bind the query params

$stmt->execute(array(':statusstring' => '%'.$_GET['q'].'%',
                 ':company' => '%'.$_GET['q'].'%',
                 ':city' => '%'.$_GET['q'].'%',
                 ':state' => '%'.$_GET['q'].'%',
                 ':zip' => '%'.$_GET['q'].'%',
                 ':mainphone' => '%'.$_GET['q'].'%',
                 ':contact' => '%'.$_GET['q'].'%',
                 ':function' => '%'.$_GET['q'].'%',
                 ':cellphone' => '%'.$_GET['q'].'%',
                 ':email' => '%'.$_GET['q'].'%',
                 ':nextcontactdate' => '%'.$_GET['q'].'%',
                 ':limit' => $limit,
                 ':offset' => $offset));

// Do we have any results?
if ($stmt->rowCount() > 0) {
    // Define how we want to fetch the results
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $iterator = new IteratorIterator($stmt);

    // Display the results
    foreach ($iterator as $row) {
        echo "<tr>

  <td align=center>"; if($row['status']==1)
                        {
                        echo "Contacted";
                        }
                      elseif($row['status']==2)
                        {
                        echo "Uncontacted";
                        }
                      elseif($row['status']==3)
                        {
                        echo "Do Not Call";
                        }
                        echo "</td>
  <td align=center>". $row['company'] ."</td>
  <td align=center>". $row['city'] ."</td>
  <td align=center>". $row['state'] ."</td>
  <td align=center>". $row['zip'] . "</td>
  <td align=center>". $row['mainphone'] ."</td>
  <td align=center>". $row['contact'] ."</td>
  <td align=center>". $row['function'] ."</td>
  <td align=center>". $row['cellphone'] ."</td>
  <td align=center>". $row['email'] ."</td>
  <td align=center>". $row['lastcontactdate'] ."</td>
  <td align=center>". $row['nextcontactdate'] ."</td>
  <td align=center valign=middle width=100>
  <form action=customerdetails.php method=POST>
  <input type=hidden value=" . $row['id'] . " name=id>
  <input type=image src=images/details.png height=30>
  </form>
  </td></tr>";
    }

} else {
    echo '<p>No results could be displayed.</p>';
}

} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}
?> 

0 个答案:

没有答案