首页 > Web开发 > 详细

夺命雷公狗jquery---63天气查询

时间:2015-11-04 20:57:45      阅读:311      评论:0      收藏:0      [点我收藏+]

先导入数据,我们可以通过以下PHP代码进行数据导入,

 

<?php
    header("Content-Type:text/html;charset=utf-8");
    //1.读取文件到数组
    $file = file(‘tianqi.txt‘);
    //print_r($file);
    //2.计算数组长度
    $count = count($file);
    //3.导入数据到数据库
    mysql_connect(‘localhost‘,‘root‘,‘‘);
    mysql_query(‘use tianqi‘);
    mysql_query(‘set names utf8‘);

    //
    for($i=0;$i<$count;$i++){
        $data = explode(‘=‘,$file[$i]);
        $code = $data[0];
        $name = $data[1];

        //组装sql语句
        $sql = "insert into city values(null,‘$code‘,‘$name‘)";
        mysql_query($sql);
    }

    mysql_close();

 

 

然后用浏览器打开即可导入,我们可以在数据库中看到导入的文件.

 

下一步就开始我们的HTML代码了,先创建一个index.html的文件

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="js/jquery.js"></script>
        <script>
            $(function(){
                //发送ajax请求
                $.post(city.php,function(msg){
                    $(msg).each(function(i,item){
                        var op = new Option(item.name,item.code);
                        $(#city)[0].options.add(op);

                    });
                },json);

                //编写ready方法实现页面载入,为按钮绑定实现查询操作
                $(#btnok).bind(click,function(){
                    var code = $(#city).val();
                    //alert(code);
                    var data = {
                        code:code
                    }
                    $.post(get.php,data,function(msg){
                        //alert(typeof msg); 查看下返回类型,如果是字符串类型,那么就需要转换下
                        //alert(typeof msg);
                        var json = eval((+msg+));
                        //alert(typeof json);
                        var city = json.weatherinfo.city;
                        var wendu = json.weatherinfo.temp1;
                        var qihou = json.weatherinfo.weather;

                        $(#result).append(city+<hr />+wendu+<hr />+qihou)
                    });
                });

            });
        </script>
    </head>
    <body>
        <h1>天气查询</h1>
        <select id="city">
            <option value="-1">请选择城市名称</option>
        </select>
        <hr />
        <input type="button" id="btnok" value="查询" />
        <hr />
        <div id="result"></div>
    </body>
</html>

 

 

然后又到我们的php代码让接口进行导入了

 

<?php
    header("Content-Type:text/html;charset=utf-8");
    $code = $_POST[‘code‘];
    $url = "http://www.weather.com.cn/adat/cityinfo/$code.html";  //这里是一个中国天气网的接口

    //模拟发送get请求
    $str = file_get_contents($url);
    echo $str;  //因为本身他就是json文件了,所以步需要转了

 

夺命雷公狗jquery---63天气查询

原文:http://www.cnblogs.com/leigood/p/4937024.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!