Home Database Mysql Tutorial 实现MySQL书架的后台操作流程

实现MySQL书架的后台操作流程

Jun 07, 2016 pm 04:09 PM
mysql main bookshelf Backstage accomplish operate article process

以下的文章主要说明的是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>
Copy after login


下架书籍

 


Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles