ホームページ >データベース >mysql チュートリアル >实现MySQL书架的后台操作流程
以下的文章主要说明的是MySQL书架的后台,其中主要包括的有其功能的具体描述,MySQL数据库的总体设计,MySQL数据库的实际操作,以下的文章就是对这些相关内容的主要描述。望你浏览之后会有所收获。 设计模式之迭代器 Iterator ,写了一个list类SortedArrayLi
以下的文章主要说明的是MySQL书架的后台,其中主要包括的有其功能的具体描述,MySQL数据库的总体设计,MySQL数据库的实际操作,以下的文章就是对这些相关内容的主要描述。望你浏览之后会有所收获。
设计模式之迭代器 Iterator ,写了一个list类SortedArrayList不知该叫什么名字,谁有在墙上挂一个MySQL书架的经验?
功能描述:
这只是项目中的小模块,项目中有几个栏目小说 期刊 综合,每个栏目都有书籍,在sns中,当登录用户,看到某本书的时候,可以点击放入我的书架,选择放入那个书架,每个书架可以放20本书,每个用户最多可以建10个书架。对放到书架上的书,可以在我的排序中自动拖动,而在其他的更新排序中按实际时间显示,用户不可以改变顺序,
数据库设计:
数据库主要用来保存放到MySQL书架中书籍的一些信息,封面 描述 地址 等,还有重要的 书架类别字段(用一个二维数组实现),每个书架有20个位置可以放书,可以设置一个字段,放书的时候是书的id ,没放书的就用0来表示,可以用一个二维数组,数组的key 用来放书架的类别,值用来放此类别中的信息,
数据库的操作:
数据库操作说明项目都是一样的CURL基本的操作,具体到本项目中,就是先添加,每个用户有一个默认数据,就是第一个数组,用户可以改变书架的名字就是数组的键值,对于每一个数组的操作,当加入此书架时,移动位置时 ,下架时,当向书架中添加书籍时还要判断此书架是否已经放满没有位置了,还有就是同一本书不能在一个书架中放入两次以上。
对于整个字段,就是在添加书架类别的时候,新建一个数组,初始里面的数组,就是初始一个空书架。当添加书架的时候判断书架是否以超过最大数目(可以在前面页面根据等级控制用户可以创建书架的个数);create table bookshelf (uid int(11) ,shelf text);//创建书架表bookshelf
Sql代码
$arr = array('默认数据'=>'0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0');//插入默认书架的数据,一个数字,key 为书架名字,value为数据上的位置默认为空,
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute">text</span><span> = </span><span class="attribute-value">serialize</span><span>($arr);//序列化该数组 </span></span></li> <li><span>insert into bookshelf (uid shelf) values( "$text" ,'$uid');//插入初始化数据 </span></li> <li class="alt"><span>//添加书架,书架'shelf1' 名字要唯一 </span></li> <li> <span>$</span><span class="attribute">shelfs</span><span>=</span><span class="attribute-value">select</span><span> shelf from bookshelf where </span><span class="attribute">uid</span><span> = </span><span class="attribute-value">'$uid'</span><span>;//查询出uid 的书架 </span> </li> <li class="alt"> <span>$</span><span class="attribute">a</span><span> = </span><span class="attribute-value">unserialize</span><span>($shelfs);//反序列化MySQL书架得到数组, </span> </li> <li> <span>for(int </span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;i</span><span class="tag"><span class="tag-name">count</span><span>($a);i++){//判断此书架是否存在 </span></span> </li> <li class="alt"><span>if($(a)[0]==shelf1){ </span></li> <li><span>return "此书架以存在"; </span></li> <li class="alt"><span>} </span></li> <li><span>return true; </span></li> <li class="alt"><span>} </span></li> <li> <span>$</span><span class="attribute">p</span><span> = </span><span class="attribute-value">array</span><span>('shelf1'=</span><span class="tag">></span><span>'0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0'); </span> </li> <li class="alt"> <span>$</span><span class="attribute">p</span><span> = </span><span class="attribute-value">serialize</span><span>($p);//插入新书架 </span> </li> <li> <span>insert into bookshelf shelf values ('$p') where </span><span class="attribute">uid</span><span> = </span><span class="attribute-value">'$uid'</span><span>; </span> </li> <li class="alt"><span>//添加书籍 book1 'bid1'到书架shelf1 </span></li> <li> <span>$</span><span class="attribute">shelfs1</span><span> = </span><span class="attribute-value">select</span><span> shelf from bookshelf where </span><span class="attribute">uid</span><span> = </span><span class="attribute-value">'$uid'</span><span>;// </span> </li> <li class="alt"> <span>$</span><span class="attribute">b</span><span> = </span><span class="attribute-value">unserialize</span><span>($shelfs1);// </span> </li> <li> <span>for(int </span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;i</span><span class="tag"><span class="tag-name">count</span><span>($b);i++){ </span></span> </li> <li class="alt"><span>if($b[i][0]=='$shelf1'){//得到要插入的MySQL书架 </span></li> <li> <span>$</span><span class="attribute">pk</span><span> = </span><span class="attribute-value">explode</span><span>('-','$b[i][1]); </span> </li> <li class="alt"> <span>for(int </span><span class="attribute">num</span><span>=</span><span class="attribute-value">1</span><span>;num</span><span class="tag"><span class="tag-name">count</span><span>($pk[1]);num++){;/书架是否放满 </span></span> </li> <li><span>if($pk[1][i]==0){ </span></li> <li class="alt"> <span>for(int </span><span class="attribute">k</span><span>=</span><span class="attribute-value">1</span><span>;k</span><span class="tag"><span class="tag-name">count</span><span>($pk);k++){//判断书架中是否有要插入的书 </span></span> </li> <li><span>if($pk[i][1]=='$bid1'){ </span></li> <li class="alt"><span>return '此书架已有此时'; </span></li> <li><span>}else{//书架中没有此书可以插入 默认插入到后面 </span></li> <li class="alt"><span>if($pk[i][1]==0){ </span></li> <li><span>$pk[i][1]==$bid1;//插入数组 </span></li> <li class="alt"><span>return implode("-",$pk[i]);//返回字符串 </span></li> <li><span>} </span></li> <li class="alt"><span>}else </span></li> <li><span>return "此书架以放满"; </span></li> <li class="alt"><span>} </span></li> <li><span>//读取书架 用于在加入书架时显示的列表 书籍数量 存入一个数组中 </span></li> <li class="alt"> <span>$</span><span class="attribute">arr</span><span> = </span><span class="attribute-value">new</span><span> array(); </span> </li> <li> <span>$</span><span class="attribute">shlefs</span><span> = </span><span class="attribute-value">select</span><span> shelf from bookshelf where </span><span class="attribute">uid</span><span> =</span><span class="attribute-value">'$uid'</span><span>;// </span> </li> <li class="alt"> <span>$</span><span class="attribute">ss</span><span> = </span><span class="attribute-value">unsersize</span><span>($shelfs);//反序列化MySQL书架信息 </span> </li> <li> <span>for(int </span><span class="attribute">nn</span><span>=</span><span class="attribute-value">1</span><span>;nn</span><span class="tag"><span class="tag-name">count</span><span>($ss);nn++){//把书架名字做key </span></span> </li> <li class="alt"> <span>$</span><span class="attribute">key</span><span> =$ss[i][0]; </span> </li> <li> <span>$</span><span class="attribute">value</span><span>=</span><span class="attribute-value">0</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">n</span><span> = </span><span class="attribute-value">explode</span><span>("-",$ss[i][1]); </span> </li> <li> <span>for(int </span><span class="attribute">mm</span><span>=</span><span class="attribute-value">1</span><span>;mm</span><span class="tag"><span class="tag-name">count</span><span>($n);mm++){//得到此书架中的书数量 </span></span> </li> <li class="alt"><span>if($n[1] !=0){ </span></li> <li><span>$value++; </span></li> <li class="alt"><span>} </span></li> <li><span>$arr[0]=$key; </span></li> <li class="alt"><span>$arr[1]=$value; </span></li> <li><span>} </span></li> <li class="alt"><span>//移动书籍 把bid2 从5位置移动到8位置 </span></li> <li> <span>$</span><span class="attribute">startpos</span><span> = </span><span class="attribute-value">2</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">endpos</span><span> = </span><span class="attribute-value">8</span><span>; </span> </li> <li> <span>if($startpos</span><span class="tag"><span class="tag-name">0</span><span> || $startpos</span><span class="tag">></span><span>20 || $endpos</span><span class="tag"><span class="tag-name">0</span><span> || $endpos</span><span class="tag">></span><span>20 || $</span><span class="attribute">startpos</span><span>==$endpos)exit; </span></span></span> </li> <li class="alt"> <span>$</span><span class="attribute">shelf</span><span> = $_db-</span><span class="tag">></span><span>fetch_first("select * from `sns_shelf` where `uid`={$uid}"); </span> </li> <li> <span>$shelf select shelf from bookshelf wher </span><span class="attribute">uid</span><span> =</span><span class="attribute-value">'$uid'</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">shelfs</span><span>=</span><span class="attribute-value">unserisize</span><span>($shelf);// </span> </li> <li><span>feach($shelfs as $key ,$value){ </span></li> <li class="alt"> <span>$</span><span class="attribute">array</span><span> = </span><span class="attribute-value">explode</span><span>("-",$value); </span> </li> <li><span>if($array[1] == 'bid2'){找到bid2所在的MySQL书架 </span></li> <li class="alt"><span>if($sarry[1][$startpos]==0)exit;//如果开始位置没有书不交换 </span></li> <li> <span>$</span><span class="attribute">temp</span><span> = $sarry[1][$endpos];//交换位置 </span> </li> <li class="alt"><span>$sarry[1][$endpos]=$sarry[1][$startpos]; </span></li> <li><span>$sarry[1][$startpos]=$temp; </span></li> <li class="alt"> <span>$</span><span class="attribute">val</span><span> = </span><span class="attribute-value">implode</span><span>("-",$value[1]); </span> </li> <li><span>$shelfs[1] = $val; </span></li> <li class="alt"><span>serialize($shelfsl);//序列化shelf </span></li> <li> <span>update bookshelf set </span><span class="attribute">shelf</span><span> = </span><span class="attribute-value">shelfs</span><span> where </span><span class="attribute">uid</span><span> = </span><span class="attribute-value">'$uid'</span><span>; </span> </li> <li class="alt"><span>更新 </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> </ol>
下架书籍