【淘宝客】根据淘客联盟精选清单(淘宝天猫内部优惠券)搜索淘宝天猫优惠券dome

随机显示的版本我们在上一篇文章中做了,现在让我们来做一个搜索版本吧,哈哈哈哈哈哈,也是一个简单的dome,需要你们自己美化,当然这个版本也很简单,有能力的自己优化程序和添加分页功能(本博主能力有限啊,欢迎有兴趣的兄弟,评论区给出建议)

本次程序就3个文件,如下图:

其中index.php就是一个简单的搜索框页面,taobao.dat文件我就不说了,上篇文章里面有提到,taobao.php文件就是根据index.php搜索框中的搜索关键词来处理的文件。

 

不说了,上代码

index.php文件代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>搜索显示淘宝天猫优惠券dome</title>
</head>

<body>
<form class="navbar-form navbar-left" action="taobao.php" method="post">
    <div class="form-group">
        <input type="text" name="key" class="form-control" placeholder="Search">
    </div>
    <button type="submit" class="btn btn-default">搜索</button>
</form>

</body>
</html>

 

taobao.php文件代码如下:

<?php
ini_set("error_reporting","E_ALL & ~E_NOTICE");
function webalert($Key){
    $html="<script>\r\n";
    $html.="alert('".$Key."');\r\n";
    $html.="history.go(-1);\r\n";
    $html.="</script>";
    exit($html);
}

function charaget($data){
    if(!empty($data) ){    
        $fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;   
        if( $fileType != 'UTF-8'){   
            $data = mb_convert_encoding($data ,'UTF-8' , $fileType);   
        }   
    }   
    return $data;    
}


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $trimmed = array_map('trim', $_POST);
    $key=$trimmed['key'];
    if(empty($key)) {
        webalert('亲,您确认下输入!');
    } 
    
    $keyword = explode(" ", $key); //拆分关键字为数组
    $keycount = count($keyword); //统计关键字个数

    
    
    
    $files = "taobao.dat";
    
    $filer = fopen($files, "r") or webalert('无法打开文件!');
    
    while (!feof($filer)) {//检测是否已到达文件末尾

        $row = fgets($filer);//从文件指针中读取一行
        $rows = trim(charaget($row));//转UTF-8,并移除字符串两侧的空白字符或其他预定义字符
        //var_dump($rows);
        if ($rows) {
            $e_rows = explode("\t", $rows);
            
            
            $a++;    //给数据编行号
            
            $b=0;    //初始化关键字,比对次数
            
            $tmp_str="";
            
            
            //echo "$a===$e_rows[0]==$key == <br>\r\n"; 
            foreach($keyword as $val){                 
                if (strstr( $e_rows[0] , $val ) !== false ){    //搜索框中的关键字和数据库中的标题匹配            
                    //echo "$a <br>\r\n";
                    $b++;
                    if ($b == $keycount) {//筛选出多个关键字出现在同一个标题中
                        $tmp_str=$a;
                        if($tmp_str!="" && !empty($tmp_str)){    
                            $c++;//筛选后,查询结果进行编号
                            echo "
                                <table>
                                <tbody>
                                <tr>
                                    <td>查询结果 $c </td>
                                    <td>$e_rows[0]</td>
                                    <td><img src=\"$e_rows[1]\" width=\"100\" /></td>
                                    <td><a href=\"$e_rows[2]\">购买链接</a></td>
                                    <td>商品价格(单位:元):$e_rows[3]</td>
                                    <td>商品月销量:$e_rows[4]</td>
                                    <td>平台类型:$e_rows[5]</td>
                                    <td>优惠券面额:$e_rows[6]</td>
                                    <td>优惠券结束时间:$e_rows[7]</td>
                                    <td><a href=\"$e_rows[8]\">先领优惠券</a></td>
                                </tr></tbody></table>"; 

                        }                        
                    }                         
                }    
            }                        
        }
    }
    //echo "$c"; 
    if ($c < 1) {
        echo '<table cellspacing="0"><tr>';
        echo "<td colspan=2>没有查询到相关信息哦。<br>";
        echo "<strong>可能原因及建议:</strong><br>";
        echo "1. 如果没有结果请尝试减少或更换查询关键词。<br>";
        echo "2. 多关键词用空格隔开,参考:毛衣 加厚 男。<br>";
        echo "3. 只涵盖部分类目:有些类目的商品查询不到。</td>";
        echo '</tr></table>';
    } else {
    }    
    
    fclose($filer);
}
?>

 

posted @ 2018-05-15 15:57  huige728  阅读(1244)  评论(0编辑  收藏  举报