PHP中网页怎么实现查询,如何在html页面中实现查找功能

d07c6fe92e11669b5925bfdf325fa7f3.png

前台效果:

b9aab1204bfb8965562e7a8c30394d4c.png

html

相关教程推荐:html教程

js

var oldKey0 = "";

var index0 = -1;var oldCount0 = 0;

var newflag = 0;

var currentLength = 0;

function wordSearch(flg) {

var key = $("#key").val(); //取key值

if (!key) {

return; //key为空则退出

}

getArray();

focusNext(flg);

}

function focusNext(flg) {

if (newflag == 0) {//如果新搜索,index清零

index0 = 0;

}

if (!flg) {

if (oldCount0 != 0) {//如果还有搜索

if (index0 < oldCount0) {//左边如果没走完,走左边

focusMove(index0);

index0++;

} else if (index0 == oldCount0) {//都走完了

index0 = 0;

focusMove(index0);

index0++;

}

else {

index0 = 0;//没确定

focusMove(index0);

index0++;

}

}

} else {

if (oldCount0 != 0) {//如果还有搜索

if (index0 <= oldCount0 && index0 > 0) {//左边如果没走完,走左边

index0--;

focusMove(index0);

} else if (index0 == 0) {//都走完了

index0 = oldCount0;

index0--

focusMove(index0);

}

}

}

}

function getArray() {

newflag = 1;

$(".contrast .result").removeClass("res");

var key = $("#key").val(); //取key值

if (!key) {

oldKey0 = "";

return; //key为空则退出

}

if (oldKey0 != key || $(".current").length != currentLength) {

//重置

index0 = 0;

var index = 0;

$(".contrast .result").each(function () {

$(this).replaceWith($(this).html());

});

pos0 = new Array();

if ($(".contrast-wrap").hasClass("current")) {

currentLength = $(".current").length;

$(".current .contrast").each(function () {

$(this).html($(this).html().replace(new RegExp(key, "gm"), "" + key + "")); // 替换

});

} else {

$(".contrast-wrap").addClass('current');

currentLength = $(".current").length;

$(".contrast").each(function () {

$(this).html($(this).html().replace(new RegExp(key, "gm"), "" + key + "")); // 替换

});

}

//$("#key").val(key);

oldKey0 = key;

//$(".contrast .result").each(function () {

// $(this).parents('.contrast-wrap').addClass('current');

// pos0.push($(this).offset().top);

//});

// pos0.push($(".contrast .result:eq(2)").offset().top - $(".contrast .result:eq(2)").parents(".contrast").offset().top);

oldCount0 = $(".contrast .result").length;

newflag = 0;

}

}

function focusMove(index0) {

$(".contrast .result:eq(" + index0 + ")").parents('.contrast-wrap').addClass('current');

$(".contrast .result:eq(" + index0 + ")").addClass("res");

var top = $(".contrast .result:eq(" + index0 + ")").offset().top + $(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop();

var intop = top - $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top;

$(".contrast .result:eq(" + index0 + ")").parents(".contrast").animate({ scrollTop: intop }, 200);

if ($(".contrast .result:eq(" + index0 + ")").parents(".contrast").scrollTop() == 0) {

$("html, body").animate({ scrollTop: top - 200 }, 200);

} else {

$("html, body").animate({ scrollTop: $(".contrast .result:eq(" + index0 + ")").parents(".contrast").offset().top - 200 }, 200);

}

}

$('#key').change(function () {

if ($('#key').val() == "") {

index0 = 0;

$(".contrast .result").each(function () {

$(this).replaceWith($(this).html());

});

oldKey0 = "";

}

});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 PHP 搜索功能的一般步骤如下: 1. 创建搜索表单:使用 HTML 表单创建一个输入框和一个按钮,让用户输入搜索关键字。 2. 处理搜索请求:使用 PHP 处理表单数据,提取用户输入的搜索关键字。 3. 查询数据库:使用 SQL 查询语句在数据库查找与搜索关键字匹配的数据。 4. 显示查询结果:使用 PHP查询结果显示在页面上,可以使用 HTML 和 CSS 格式化结果。 下面是一个简单的 PHP 搜索功能的示例代码: ``` <!DOCTYPE html> <html> <head> <title>PHP Search</title> </head> <body> <h1>Search</h1> <form method="GET" action="search.php"> <input type="text" name="keyword" placeholder="Search..."> <button type="submit">Go</button> </form> <hr> <?php // 处理搜索请求 if(isset($_GET['keyword'])) { $keyword = $_GET['keyword']; // 查询数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); $sql = "SELECT * FROM products WHERE name LIKE '%$keyword%'"; $result = mysqli_query($conn, $sql); // 显示查询结果 if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "<p>{$row['name']}</p>"; } } else { echo "<p>No results found.</p>"; } mysqli_close($conn); } ?> </body> </html> ``` 在上面的代码,我们创建了一个搜索表单,使用 `$_GET` 方法获取用户输入的搜索关键字,并使用 SQL 查询语句在 `products` 表查找与搜索关键字匹配的数据,最后将查询结果显示在页面上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值