开发者

How can I get it so at the top of my search results page it says the amount of results found?

I have a results page and I would like there to be a bit at the top of the page where it says how many results were returned. How do I do this?

My code is

<?php
if(strlen(trim($_POST['search'])) > 0) {

$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";

mysql_connect ("3", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"])) 
{ 

}  

$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND 
location      LIKE '%$searchterm%'";

$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) { ?>
<center>
<table height="20" width="968" cellpadding="0" cellspacing="0">
<tr>
<td>

<table height="20" width="223" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="开发者_Go百科helvetica" size="2" color="#045FB4"><?php echo $row[0]; ?></font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
</td>
</tr>
</table>

</td>
<td>

<table height="20" width="745" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="helvetica" size="2" color="black"><?php echo $row[1]; ?>
<?php echo $row[2];     ?></font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
<td align="right">
<font face="helvetica" size="2" color="red">See More...</font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
}
}
?>
</center>

THANKS!

James


If you're going to keep it to one page then mysql_num_rows() will do the trick and you're good to go. If you use pagination on the other hand, then your SELECT query will have a LIMIT clause and a second query can be constructed using COUNT(*) on the same tables with the same WHERE clause.

$total_query = "SELECT COUNT(*) FROM contact WHERE name LIKE '%$search%' AND 
                location LIKE '%$searchterm%'"


I’m not trying to compete with the other fine answers. I’m posting this as an answer because it’s too big for a comment.

Since you asked in a comment what could be done to improve your HTML, I refactored your code a bit just to illustrate some things you could do. I also included mysql_num_rows($result) mentioned by the others for the sake of completion.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
<style type="text/css">
table {
    font-family: helvetica;
    font-size: small;
    width: 968px;
}
td {
    border-bottom: 1px solid #e6e6e6;
}
.name {
    color:#045FB4;
}
.msg {
    color:black;
}
.more {
    color:red;
}
</style>
</head>

<body>
<?php

if(strlen(trim($_POST['search'])) > 0):

$search = mysql_real_escape_string($_POST["search"]);
$searchterm = mysql_real_escape_string($_POST["searchterm"]);

mysql_connect ("localhost", "root", "");
mysql_select_db ("sample");
if (!empty($_POST["search_string"])) 
{
    $search_string = mysql_real_escape_string($_POST["search_string"]);
    // more code here
}  



$query = "SELECT name,location,msg FROM contact
          WHERE name LIKE '%$search%'
          AND location LIKE '%$searchterm%'";

$result = mysql_query ($query);
if ($result):

    $num_rows = mysql_num_rows($result);
?>
<p>Found <?php echo $num_rows; ?> results.</p>

<table>
<?php
    while ($row = mysql_fetch_array ($result)):
?>
    <tr>
        <td class="name"><?php echo $row['name']; ?></td>
        <td class="msg"><?php echo $row['location'], ' ', $row['msg']; ?></td>
        <td class="more">See More...</td>
    </tr>

<?php
    endwhile;
endif;
endif;
?>

</table>
</body>
</html>

Normally, I would put the CSS in a separate file, but this is only an example. Some things to notice:

  • There’s only one table
  • There’s no style information in the HTML
  • It uses mysql_real_escape_string. Ideally you'd also want to use prepared statements, but I’ll leave that as a personal exercise for you. :)

Even this can be improved quite a bit, but it's a start.


Try using mysql_num_rows($query). I am no PHP expert but, from memory, that should do the trick. Just echo this wherever you want it.


$num_rows = mysql_num_rows($result);

Check out the documentation: http://php.net/manual/en/function.mysql-num-rows.php


Also you can use SQL_CALC_FOUND_ROWS and FOUND_ROWS():

$query = "SELECT SQL_CALC_FOUND_ROWS name,location,msg FROM contact WHERE name LIKE '%$search%' AND location LIKE '%$searchterm%'";

$result = mysql_query($query);
if ($result)
{
    $rs_count = mysql_query("SELECT FOUND_ROWS();");
    $counted = (int)mysql_result($rs_count, 0);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜