gpt4 book ai didi

php搜索,根据搜索返回显示php模板

转载 作者:行者123 更新时间:2023-11-29 21:52:12 24 4
gpt4 key购买 nike

我仍在学习中,任何帮助将不胜感激。 HTML 是相当简单的。还没有样式或类似的东西,全部在本地主机上进行测试。所以我没有实时设置示例。

我只使用 PHP 和 phpmyadmin 中的一个表。我的文件是index.php(表单在这里),search.php(搜索代码存储在这里),provider1.php,provider2.php,provider3.php,provider4.php。

到目前为止,我有一个存储有以下列的表:提供商、州全名、城市、州、邮政编码。我有一个搜索表单,用户输入邮政编码,如果邮政编码与表中的一行匹配,它会查看提供程序,检查它是哪一个(provider1、provider2、provider3、provider4)。在 search.php 上回显他们有多少个选择,其中城市州来自邮政编码,然后还回显他们输入的邮政编码。

我的问题是,如果该邮政编码有提供商,我该如何告诉它使用该provider.php。示例:

92804 =provider1,因此使用provider1.php,并且仍然回显与该邮政编码对应的城市和州。

index.php:

    <!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>search</title>
</head>

<body>
<form action="search.php" method="get">
<input type="text" name="search" placeholder="zipcode..."/>
<input type="submit" value=">>"/>

</form>
</body>
</html>

搜索.php:

 <?php

mysql_connect("localhost","root","root") or die("could not connect");
mysql_select_db("zipcodes") or die("could not find db");

//collect
if (isset($_GET['search'])) {
$searchq = $_GET['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$query = mysql_query("SELECT * FROM `TABLE 1` WHERE `Zip Code` = '$searchq'") or die("could not search");
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'There was no search results!!';
}else {
while($row = mysql_fetch_array($query)) {
$city = $row['City'];
$state = $row['State'];
$provider = $row['Provider Name'];

$output = '<div> '.$provider.' '.$city.' '.$state.' </div>';
}
}

}

switch ($provider) {
case "provider1":
//put code here that tells it to use provider1.php
break;
case "provider2":
//put code here that tells it to use provider2.php
break;
case "provider3":
//put code here that tells it to use provider3.php
break;
case "provider4":
//put code here that tells it to use provider4.php
break;
default:
echo "No provider, try again";
}



?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $provider ?> in <?php echo $city ?>, <?php echo $state ?></title>
</head>

<body>
This is just a results page for testing. This page needs to know what zip to match to a provider, then go to that page.
<p>You have <?php echo $count ?> choices</p>
<p>Your provider is <?php echo $provider ?></p>
<p>You are located in <?php echo $city ?>, <?php echo $state ?> </p>
<p>The zip code you searched was <?php echo $searchq ?></p>
</body>
</html>

provider1.php:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $provider ?> in <?php echo $city ?>, <?php echo $state ?></title>
</head>

<body>
This would be a template for provider1
<p>You have <?php echo $count ?> choices</p>
<p>You are located in <?php echo $city ?>, <?php echo $state ?> </p>
<p>The zip code you searched was <?php echo $searchq ?></p>
</body>
</html>

这可以做到吗?或者我是否需要使用像 laravel 这样的东西来启动它或某种类型的框架。

最佳答案

您正在处理哪些数据集?它只是设定范围内的纯整数吗?

如果是这样,你可以这样做:

if($provider > 92804 && < 92806){
include 'provider1.php';
}elseif($provider > 92807 && < 92809){
include 'provider2.php';
}else{
// No provider, try again
}

或者,您可以使用诸如 preg_match 函数之类的功能,在处理大型数据集时,这可能是更好的可扩展解决方案。

关于php搜索,根据搜索返回显示php模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33529214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com