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

时间:2015-11-04 18:20:17

标签: php html mysql

我还在学习,任何帮助都会受到赞赏。 HTML非常简单。没有样式或类似的东西,只在localhost上进行测试。所以我没有现成的例子。

我只使用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>

的search.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的东西来启动它或某种类型的框架。

1 个答案:

答案 0 :(得分:0)

您正在处理的数据集是什么?它只是设定范围之间的纯整数吗?

如果是这样,你可以做这样的事情:

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

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