【问题标题】:How can I get it so at the top of my search results page it says the amount of results found?我怎样才能在我的搜索结果页面顶部显示找到的结果数量?
【发布时间】:2011-09-10 22:27:19
【问题描述】:

我有一个结果页面,我希望页面顶部有一个显示返回多少结果的位置。我该怎么做?

我的代码是

<?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="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>

谢谢!

詹姆斯

【问题讨论】:

  • @cypher:这可能是因为糟糕的 HTML 和糟糕的 PHP 实践。不过,这些应该只在答案中注明。这个问题仍然有效。我赞成帮助取消反对票。
  • 路过投票。显然他们太懦弱了,无法发表评论。
  • 我经常听到这样的话:“如果它有效(而且很容易),为什么不这样做呢?”答:更难维护。想换字体?现在你必须遍历 every 页面上的 every 字体标签。使用 CSS,您可以在 one 文件中进行 one 更改。
  • 基于表格的布局也不受欢迎。 centerfont 已弃用,您可能想在 Google 上搜索“SQL 注入攻击”。 @ClarkeyBoy:重要的是思想。 :)
  • 也附议了。表格应该只用于显示数据(这就是它们的设计目的);如果您以正确的结构使用屏幕阅读器,它们会使网站更容易被屏幕阅读器的用户理解 - 例如,使用标题行的标题行,标题单元格的 th 标签,封装所有正文行的 tbody 和 tfoot页脚。 th 标签可用于列标题和行标题。尝试使用&lt;div style='width: 968px; margin: 0 auto;'&gt;...&lt;/div&gt; 代替外部表(我知道那里有内联样式;我懒得在这里为它编写CSS)。

标签: php html search


【解决方案1】:

如果您要将其保留在一页上,那么 mysql_num_rows() 将起到作用,您就可以开始了。另一方面,如果您使用分页,那么您的 SELECT 查询将有一个 LIMIT 子句,并且可以在具有相同 WHERE 子句的相同表上使用 COUNT(*) 构造第二个查询。

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

【讨论】:

    【解决方案2】:

    我不想与其他优秀的答案竞争。我将其发布为答案,因为它太大而无法发表评论。

    由于您在评论中询问了可以做些什么来改进您的 HTML,我对您的代码进行了一些重构,只是为了说明您可以做的一些事情。为了完成,我还包括了其他人提到的mysql_num_rows($result)

    <!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>
    

    通常,我会将 CSS 放在单独的文件中,但这只是一个示例。 需要注意的一些事项:

    • 只有一张桌子
    • HTML 中没有样式信息
    • 它使用mysql_real_escape_string。理想情况下,您还希望使用准备好的陈述,但我会将其作为个人练习留给您。 :)

    即使这样也可以改进很多,但这只是一个开始。

    【讨论】:

      【解决方案3】:

      尝试使用mysql_num_rows($query)。我不是 PHP 专家,但从记忆中,这应该可以解决问题。随心所欲地回显它。

      【讨论】:

        【解决方案4】:
        $num_rows = mysql_num_rows($result);
        

        查看文档:http://php.net/manual/en/function.mysql-num-rows.php

        【讨论】:

          【解决方案5】:

          您也可以使用 SQL_CALC_FOUND_ROWS 和 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);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-03-26
            • 1970-01-01
            相关资源
            最近更新 更多