dsgffz2579 2015-11-04 18:20
浏览 69
已采纳

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

I am still learning and any help would be appreciated. HTML is pretty bare bones. No styles or anything like that yet, all on just localhost for testing. So I do not have a example set up live.

I am using just PHP and a table in phpmyadmin. The files I have are index.php(form is here), search.php(search code stored here), provider1.php, provider2.php, provider3.php, provider4.php.

So far I have a table that is stored with these columns:Provider,state full name, city, state, zip code. I have a search form that users enter a zip code and if the zip code matches a row in the table, it looks at the provider, checks which one it is (provider1, provider2, provider3, provider4).On the search.php it echos out how many choices they have, where the city state is from the zip code and then also echos the zip code that they entered.

My question is how can I tell it so if that zip code has a provider, to use that provider.php. Example:

92804 = provider1, so then use provider1.php and also still echo out they city and state that goes with that zip code.

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>

Is this possible to do? or do I need to use something like laravel to get it started or some type of framework.

  • 写回答

1条回答 默认 最新

  • dougu1990 2015-11-04 22:50
    关注

    What are the data sets you are dealing with? Is it just pure integers between a set range?

    If so you could just do something like this:

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

    Alternatively you may be able to use something such as the preg_match function which would probably be the better scalable solution when dealing with large data sets.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程