search
Homephp教程PHP源码ThinkPHP3.2.3实现手机摇一摇随机推荐效果

手机摇一摇效果基于html5方向感应DeviceOrientation,它将底层的方向和运动传感器进行了高级封装,轻松的实现重力感应、指南针等有趣的功能。今天我们以实例展示THINKPHP3.2.3实现手机摇一摇随机推荐文章效果。可手机访问www.gouugoyin.cn首页,用手机摇一摇,即可在右侧看到摇一摇随机推荐效果。

[PHP]代码 

首先在需要摇一摇的模板页引入jquery.js和检测手机摇晃的shade.js,HTML结构如下

<script src="{JS_PATH}/jquery-1.7.1.min.js?1.1.5"></script>
<script src="{JS_PATH}/shake.js?1.1.5"></script>
<p class="sidebar-widget js_shadeBox">
    <h4>
       摇一摇随机推荐
    </h4>
    <ul>
 
    </ul>
</p>
使用shake.js检测到用户手机摇晃,当摇晃发生时调用函数shakeEventDidOccur(),向服务器发送Ajax请求,JS代码如下

<script>
$(function(){
	var myShakeEvent = new Shake({  
        threshold: 15  
    });  
  
    myShakeEvent.start();  
  
    window.addEventListener(&#39;shake&#39;, shakeEventDidOccur, false);  
  
    function shakeEventDidOccur () { 
    	var url = "{:U(&#39;home/article/get_rand_list&#39;)}";
    	$.post(url , { num:10,order:&#39;rand()&#39; }, function(data){
    		if(data.status == &#39;ok&#39;){
    			$(".js_shadeBox ul").append(data.html);
    		}else{
    			alert(data.msg);
    		}
    	}, &#39;json&#39;).error(function(){
    		alert(&#39;网络错误,请稍后再试&#39;);
    	});
    } 
});
</script>
服务器根据提交的请求参数生成随机文章列表数组并循环拼接好的html代码片段,我们将返回html片段追加到ul里,实现了摇一摇随机推荐的效果,服务器端代码如下


<?php
/**
 * 文章控制器 ArticleController.class.php
 * ============================================================================
 * 版权所有 (C) 2015-2016 够过瘾——挨踢男的葵花宝典。
 * 网站地址:   http://www.gouguoyin.cn
 * ----------------------------------------------------------------------------
 * 许可声明:这是一个开源程序,未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * Author: 勾国印 (phper@gouguoyin.cn) 
 * Date: 2015年9月27日 上午11:27:53  
*/
namespace Home\Controller;
use Think\Controller;

class ArticleController extends Controller {

    //ajax获取随机文章列表
    public function get_rand_list(){
        $model = &#39;article&#39;;
        if (!IS_AJAX) {
            $this->ajaxReturn(array(
                &#39;msg&#39; => &#39;非法登录方式&#39;
            ));
        }
        
        //接收参数
        $order       = I(&#39;order&#39;, &#39;id desc&#39;);
        $num         = I(&#39;num&#39;, 0);
        
        $filter = array();
        
        if ($category_id) {
            $filter[&#39;category_id&#39;] = $category_id;
        }
        
        $article_list = M($model)->where($filter)->limit($num)->order($order)->select();
        
        foreach ($rand_list as $k => $v) {
            $html .= "<li><a target=&#39;_blank&#39; href=&#39;".U("home/article/$v[id]")."&#39;>".$v[&#39;title&#39;]."</li>";
        }
        $this->ajaxReturn(array(
            &#39;status&#39; => &#39;ok&#39;,
            &#39;html&#39; => $html
        ));
        
    }

}备注:ThinkPHP3.2.3控制器里获取随机列表的方法是:M($model)->where($where)->order(&#39;rand()&#39;)->limit($limit)->select();


2. [文件] THINKPHP3.2.3实现手机摇一摇随机推荐文章效果

ThinkPHP3.2.3实现手机摇一摇随机推荐效果THINKPHP3.2.3实现手机摇一摇随机推荐文章效果.zip

       



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software