Heim > php教程 > php手册 > PHP+jQuery+Ajax+Mysql实现发表心情功能

PHP+jQuery+Ajax+Mysql实现发表心情功能

WBOY
Freigeben: 2016-06-21 08:51:19
Original
1170 Leute haben es durchsucht

 

我们在浏览网站文章或帖子后,需要表达自己浏览后的心情感受,在很多网站都提供了给用户发表心情的功能,通过这个功能可以直观统计分析文章或帖子的浏览者的心情感受数据。在本文中,您将了解到,如何实现通过点击心情图标,即刻发表自己的心情。

下载 http://bbs.php100.com/read-htm-tid-391083-ds-1.html 

本文通过实例讲解使用PHP+jQuery+Ajax+Mysql相结合,实现了用户发表心情的功能,操作简单,实用性强,是一篇将WEB知识进行综合应用的文章,因此读者需要具备PHP、Mysql、jQuery以及ajax相关知识。

本示例的大致原理和流程是这样的:主页面index.html通过ajax获取心情图标及柱状图相关数据,当用户点击其中的一个心情图标时,向后台php发送请求,PHP验证用户cookie防止重复提交,然后将mysql中对应的数据心情字段内容加1,成功后返回前端页面,告诉index.html发表成功,并调整柱状图和统计数据。

HTML

先看HTML,我们在index.html中放置一个#msg,用来显示操作结果信息,#mood是操作主区域,其中ul通过javascript异步加载心情图标、说明、柱状图以及统计信息。

<code class="html" style="margin: 0px; padding: 0px; "> 
<span class="html__tag_start" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; "><div> <span class="html__attr_name" style="margin: 0px; padding: 0px; color: green; ">id</span>=<span class="html__attr_value" style="margin: 0px; padding: 0px; color: maroon; ">"msg"</span><span class="html__tag_start" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">></span><span class="html__tag_end" style="margin: 0px; padding: 0px; color: navy; "></span>
</div></span> 
    <span class="html__tag_start" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; "><div> id=mood<span class="html__tag_start" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">></span> 
    <span class="html__tag_start" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; "><ul>
<span class="html__tag_start" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">></span><span class="html__tag_end" style="margin: 0px; padding: 0px; color: navy; "></span>
</ul></span> 
<span class="html__tag_end" style="margin: 0px; padding: 0px; color: navy; "></span>
</div></span> 
</code>
Nach dem Login kopieren

PHP

首先我们在config.php配置文件中,配置数据库连接信息,以及示例相关参数。

<code class="php" style="margin: 0px; padding: 0px; "> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">host</span>=<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"localhost"</span>; 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">db_user</span>=<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"root"</span>; 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">db_pass</span>=<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">""</span>; 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">db_name</span>=<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"demo"</span>; 
 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">link</span>=mysql_connect(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">host</span>,<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">db_user</span>,<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">db_pass</span>); 
mysql_select_db(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">db_name</span>,<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">link</span>); 
mysql_query(<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"SET names UTF8"</span>); 
 
<span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//心情说明,用半角逗号隔开</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">moodname</span>=<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'震惊,不解,愤怒,杯具,无聊,高兴,支持,超赞'</span>; 
<span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//心情图标文件,用半角逗号隔开(template/images/目录)</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">moodpic</span>=<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'a1.gif,a2.gif,a3.gif,a4.gif,a5.gif,a6.gif,a7.gif,a8.gif'</span>; 
<span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//统计心情柱图标最大高度</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">moodpicheight</span>=<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">80</span>; 
</code>
Nach dem Login kopieren

接下来,我们在mood.php中准备分两部分,通过接收action参数,分为第一部分:发表心情,第二部分:获取心情相关信息。

<code class="php" style="margin: 0px; padding: 0px; "> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">include_once</span>(<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"config.php"</span>); 
 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">action</span> = <span class="php__global" style="margin: 0px; padding: 0px; color: red; ">$_GET</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'action'</span>]; 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">action</span>==<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'send'</span>){ <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//发表心情</span> 
    ... 
}<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">else</span>{ <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//获取心情</span> 
    ... 
} 
</code>
Nach dem Login kopieren

Part1:发表心情。

用户从前端通过post提交发表心情的参数,包括文章id,心情id。先验证文章是否存在,然后再验证用户是否已经对这篇文章发表过心情了,接着操作数据库,将对应的心情字段值+1,并计算出当前心情对应的柱状图的高度,返回给前端js接收。

<code class="php" style="margin: 0px; padding: 0px; "> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span> = (int)<span class="php__global" style="margin: 0px; padding: 0px; color: red; ">$_POST</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'id'</span>]; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//文章或帖子id</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mid</span> = (int)<span class="php__global" style="margin: 0px; padding: 0px; color: red; ">$_POST</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'moodid'</span>]; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//心情id(配置文件中提供8种心情)</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(!<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mid</span>  !<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span>){ 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">echo</span> <span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"此链接不存在"</span>;<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">exit</span>; 
} 
 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">havemood</span> = chk_mood(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span>); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//验证cookie</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">havemood</span>==<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">1</span>){ 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">echo</span> <span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"您已经表达过心情了,保持平常心有益身心健康!"</span>;<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">exit</span>; 
} 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">field</span> = <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood'</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mid</span>; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//数据表中的心情字段,分别用mood0,mood1,mood2...表示不同的心情字段</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">query</span> = mysql_query(<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"update mood set "</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">field</span>.<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"="</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">field</span>.<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"+1 where id="</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span>); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//对应的心情字段值+1</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">query</span>){ 
    setcookie(<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"mood"</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span>, <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mid</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span>, time()+<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">300</span>); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//设置cookie,为了测试我们设置cookie过期时间为300s</span> 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">query2</span> = mysql_query(<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"select * from mood where id=$id"</span>); 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span> = mysql_fetch_array(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">query2</span>);<span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//获取该文章的心情数据</span> 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">total</span> = <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood0'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood1'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood2'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood3'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood4'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood5'</span>]+ 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood6'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood7'</span>]; 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">height</span> = round((<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">field</span>]/<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">total</span>)*<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">moodpicheight</span>); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//得到总量,并计算当前对应心情的柱状图的高度</span> 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">echo</span> <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">height</span>; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//返回当前心情柱状的高度</span> 
}<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">else</span>{ 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">echo</span> -<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">1</span>; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//数据出错</span> 
} 
</code>
Nach dem Login kopieren

验证用户是否已发表过心情,我们通过函数chk_mood()来判断用户对应的cookie是否存在。

<code class="php" style="margin: 0px; padding: 0px; "> 
<span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//验证是否提交过</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">function</span> chk_mood(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span>){ 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">cookie</span> = <span class="php__global" style="margin: 0px; padding: 0px; color: red; ">$_COOKIE</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood'</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span>]; 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">cookie</span>){ 
        <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">doit</span> = <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">1</span>; 
    }<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">else</span>{ 
        <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">doit</span> = <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">0</span>; 
    } 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">return</span> <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">doit</span>; 
} 
</code>
Nach dem Login kopieren

Part2:获取心情

通过获取数据表中文章或帖子id对应的心情数据,得到每种心情对应的值(可以理解为发表心情的次数),并计算其柱状图高度,将每种心情对应的值、名称、图标、高度信息构造成数组,最终以JSON格式数据返回给前端。

<code class="php" style="margin: 0px; padding: 0px; "> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mname</span> = explode(<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">','</span>,<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">moodname</span>);<span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//心情说明</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">num</span> = count(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mname</span>); 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mpic</span> = explode(<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">','</span>,<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">moodpic</span>);<span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//心情图标</span> 
 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">id</span> = (int)<span class="php__global" style="margin: 0px; padding: 0px; color: red; ">$_GET</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'id'</span>]; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//文章或帖子id</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">query</span> = mysql_query(<span class="php__string2" style="margin: 0px; padding: 0px; color: fuchsia; ">"select * from mood where id=$id"</span>); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//查询对应的心情数据</span> 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span> = mysql_fetch_array(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">query</span>); 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>){ 
    <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//得到发表心情的总量</span> 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">total</span> = <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood0'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood1'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood2'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood3'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood4'</span>]+ 
<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood5'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood6'</span>]+<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood7'</span>]; 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">for</span>(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">i</span>=<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">0</span>;<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">i</span>$<span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">num</span>;<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">i</span>++){ 
        <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">field</span> = <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood'</span>.<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">i</span>; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//字段名</span> 
        <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">m_val</span> = intval(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">rs</span>[<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">field</span>]); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//心情对应的值(次数)</span> 
        <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">height</span> = <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">0</span>; <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//柱图高度</span> 
        <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">total</span> && <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">m_val</span>){ 
            <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">height</span>=round((<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">m_val</span>/<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">total</span>)*<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">moodpicheight</span>); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//计算高度</span> 
        } 
             
        <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">arr</span>[] = <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">array</span>( 
            <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mid'</span> => <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">i</span>, <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//对应心情id</span> 
            <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood_name'</span> => <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mname</span>[<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">i</span>], <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//心情名称</span> 
            <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood_pic'</span> => <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">mpic</span>[<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">i</span>], <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//图标</span> 
            <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'mood_val'</span> => <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">m_val</span>, <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//次数</span> 
            <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'height'</span> => <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">height</span> <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//柱状图高度</span> 
        ); 
    } 
    <span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">echo</span> json_encode(<span class="php__keyword" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">$</span><span class="php__variable" style="margin: 0px; padding: 0px; color: rgb(64, 64, 194); ">arr</span>); <span class="php__com" style="margin: 0px; padding: 0px; color: green; ">//返回JSON数据</span> 
} 
</code>
Nach dem Login kopieren

jQuery

我们使用强大的jQuery来完成本例中所有ajax的交互动作,因此在index.html中要先载入jquery.js库,目前1.8版本已经发布了哦,可以到官网http://jquery.com/下载。

接着我们向mood.php发送Ajax请求,获取心情列表信息,并展示在index.html页面中。

<code class="js" style="margin: 0px; padding: 0px; "> 
$(<span class="js__operator" style="margin: 0px; padding: 0px; color: rgb(65, 105, 225); font-weight: bold; ">function</span>()<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> 
    $.ajax(<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> 
        type: <span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'GET'</span>, <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//通过get方式发送请求</span> 
        url: <span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'mood.php'</span>, <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//目标地址</span> 
        cache: false, <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//不缓存数据,注意文明发表心情的数据是实时的,需将cache设置为false,默认是true</span> 
        data: <span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'id=1'</span>, <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//参数,对应文章或帖子的id,本例中固定为1,实际应用中是获取当前文章或帖子的id</span> 
        dataType: <span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'json'</span>, <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//数据类型为json</span> 
        error: <span class="js__operator" style="margin: 0px; padding: 0px; color: rgb(65, 105, 225); font-weight: bold; ">function</span>()<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> 
            alert(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'出错了!'</span>); 
        <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span>, 
        success: <span class="js__operator" style="margin: 0px; padding: 0px; color: rgb(65, 105, 225); font-weight: bold; ">function</span>(json)<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//请求成功后</span> 
            <span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(json)<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> 
                $.each(json,<span class="js__operator" style="margin: 0px; padding: 0px; color: rgb(65, 105, 225); font-weight: bold; ">function</span>(index,array)<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//遍历json数据列</span> 
                    <span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">var</span> str = <span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"<li>
<span>"</span>+array[<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'mood_val'</span>]+"</li></span><div style="\<span" class="js__string">"height:"+array[<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'height'</span>]+"px;\"></div>
<div rel="\<span" class="js__string" style="margin: 0px; padding: 0px; color: teal; ">""+array[<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'mid'</span>]+<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"\"><img  alt="PHP+jQuery+Ajax+Mysql实现发表心情功能" >+array[<span class="js__string"   style="max-width:90%">'mood_pic'</span>]+"\"> 
<br>"+array['mood_name']+"</span>
</div>"; 
                    $(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"#mood ul"</span>).append(str); <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//将数据加入到#mood ul列表中</span> 
                   <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span>);  
            <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span> 
        <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span> 
    <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span>); 
    ... 
<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span>); 
</code>
Nach dem Login kopieren

这样,我们在访问index.html后,页面会载入心情列表,当然要想看到最终排列效果,还需要CSS,本文不讲解相关CSS,请下载源码或查看demo了解。

接下来,我们有个交互动作,当点击对应的心情图标时,图标被标识为已发表,柱状图高度发生变化,并且上面的数字会+1,表示发表成功,如果继续点击心情图标,会提示已经发表过不能重复提交。请看代码:

<code class="js" style="margin: 0px; padding: 0px; "> 
$(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">".face"</span>).live(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">'click'</span>,<span class="js__operator" style="margin: 0px; padding: 0px; color: rgb(65, 105, 225); font-weight: bold; ">function</span>()<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//侦听点击事件</span> 
    <span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">var</span> face = $(<span class="js__operator" style="margin: 0px; padding: 0px; color: rgb(65, 105, 225); font-weight: bold; ">this</span>); 
    <span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">var</span> mid = face.attr(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"rel"</span>); <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//对应的心情id</span> 
    <span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">var</span> value = face.parent().find(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"span"</span>).html(); 
    <span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">var</span> val = <span class="js__function" style="margin: 0px; padding: 0px; color: olive; ">parseInt</span>(value)+<span class="js__num" style="margin: 0px; padding: 0px; color: red; ">1</span>; <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//数字加1</span> 
    <span class="js__sl_comment" style="margin: 0px; padding: 0px; color: green; ">//提交post请求</span> 
    $.post(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"mood.php?action=send"</span>,<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span>moodid:mid,id:<span class="js__num" style="margin: 0px; padding: 0px; color: red; ">1</span><span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span>,<span class="js__operator" style="margin: 0px; padding: 0px; color: rgb(65, 105, 225); font-weight: bold; ">function</span>(data)<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> 
        <span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">if</span>(data><span class="js__num" style="margin: 0px; padding: 0px; color: red; ">0</span>)<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> 
            face.prev().css(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"height"</span>,data+<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"px"</span>); 
            face.parent().find(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"span"</span>).html(val); 
            face.find(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"img"</span>).addClass(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"selected"</span>); 
            $(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"#msg"</span>).show().html(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"操作成功"</span>).fadeOut(<span class="js__num" style="margin: 0px; padding: 0px; color: red; ">2000</span>); 
        <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span><span class="js__statement" style="margin: 0px; padding: 0px; color: navy; font-weight: bold; ">else</span><span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">{</span> 
            $(<span class="js__string" style="margin: 0px; padding: 0px; color: teal; ">"#msg"</span>).show().html(data).fadeOut(<span class="js__num" style="margin: 0px; padding: 0px; color: red; ">2000</span>); 
        <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span> 
    <span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span>); 
<span class="js__brace" style="margin: 0px; padding: 0px; color: red; font-weight: bold; ">}</span>); 
</code>
Nach dem Login kopieren

没看明白的童鞋可以下载源码仔细研究,点击文章开头的Download按钮即可下载,最后附上本例所需的mysql数据表结构,谢谢您的关注。

<code class="php" style="margin: 0px; padding: 0px; "> 
CREATE TABLE IF NOT EXISTS `mood` ( 
  `id` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span>, 
  `mood0` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  `mood1` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  `mood2` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  `mood3` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  `mood4` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  `mood5` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  `mood6` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  `mood7` int(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">11</span>) NOT <span class="php__value" style="margin: 0px; padding: 0px; color: gray; font-weight: bold; ">NULL</span> DEFAULT <span class="php__string1" style="margin: 0px; padding: 0px; color: purple; ">'0'</span>, 
  PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
 
 
INSERT INTO `mood` (`id`, `mood0`, `mood1`, `mood2`, `mood3`, `mood4`, `mood5`, `mood6`, `mood7`) 
VALUES(<span class="php__number" style="margin: 0px; padding: 0px; color: red; ">1</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">8</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">6</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">20</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">16</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">6</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">9</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">15</span>, <span class="php__number" style="margin: 0px; padding: 0px; color: red; ">21</span>); </code>
Nach dem Login kopieren



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage