After sorting php articles, move them forward and backward. if the written statements do not work, I don't know what went wrong?

Source: Internet
Author: User
After sorting php articles, move them forward and backward. if the written statements do not work, I don't know what went wrong? I manually move the document sort forward and backward, but the SQL statement does not seem to work. Which of the following can help you see it?
/* Forward */if ($ result = 'up') {$ sql0 = $ db-> query ("select sort from ". $ db-> table ('product '). "where id = '$ ID'"); $ row = $ db-> fetch_array ($ sql0); if (! $ Row) {$ oldpx = 1;} else {$ oldpx = $ row ['sort '];} if ($ oldpx> 1) {$ db-> query ("update ". $ db-> table ('product '). "set sort = sort + 1 where sort = ". $ oldpx-1); $ db-> query ("update ". $ db-> table ('product '). "set sort = sort-1 where id = ". $ id);} header ("Location: product_list.php \ n"); exit;}/* Move Back */if ($ result = 'drop ') {$ sql1 = $ db-> query ("select sort from ". $ db-> table ('product '). "where id = '$ ID'"); $ row = $ db-> fetch_array ($ s Ql1); if (! $ Row) {$ oldpx = 9999;} else {$ oldpx = $ row ['sort '];} $ sql2 = $ db-> query ("select sort from ". $ db-> table ('product '). "order by sort desc limit 1"); $ rows = $ db-> fetch_array ($ sql2); if (! $ Rows) {$ maxpx = 9999;} else {$ maxpx = $ rows ['sort '];} if ($ oldpx <$ maxpx) {$ db-> query ("update ". $ db-> table ('product '). "set sort = sort-1 where sort = ". $ oldpx + 1); $ db-> query ("update ". $ db-> table ('product '). "set sort = sort + 1 where id = ". $ id);} header ("Location: product_list.php \ n"); exit ;}


Reply to discussion (solution)

Will the sort of the article be not Continuous? if you move forward, you may not modify it to the database record by taking sort-1 as an instruction, then, even if sort + 1 is not larger than the sort in the next article

/* Forward */if ($ result = 'up') {$ sql0 = $ db-> query ("select sort from ". $ db-> table ('product '). "where id = '$ ID'"); $ row = $ db-> fetch_array ($ sql0); if (! $ Row) {$ oldpx = 1;} else {$ oldpx = $ row ['sort '];} if ($ oldpx> 1) {$ db-> query ("update ". $ db-> table ('product '). "set sort = sort + 1 where sort = ". $ oldpx-1); $ db-> query ("update ". $ db-> table ('product '). "set sort = sort-1 where id = ". $ id);} header ("Location: product_list.php \ n"); exit;}/* Move Back */if ($ result = 'drop ') {$ sql1 = $ db-> query ("select sort from ". $ db-> table ('product '). "where id = '$ ID'"); $ row = $ db-> fetch_array ($ s Ql1); if (! $ Row) {$ oldpx = 9999;} else {$ oldpx = $ row ['sort '];} $ sql2 = $ db-> query ("select sort from ". $ db-> table ('product '). "order by sort desc limit 1"); $ rows = $ db-> fetch_array ($ sql2); if (! $ Rows) {$ maxpx = 9999;} else {$ maxpx = $ rows ['sort '];} if ($ oldpx <$ maxpx) {$ db-> query ("update ". $ db-> table ('product '). "set sort = sort-1 where sort = ". $ oldpx + 1); $ db-> query ("update ". $ db-> table ('product '). "set sort = sort + 1 where id = ". $ id);} header ("Location: product_list.php \ n"); exit ;}

In red, replace it with where id = '". $ id ."'");

If ($ _ REQUEST ["action"] = "up") {uppx ($ _ REQUEST ['goods _ id']);} elseif ($ _ REQUEST ["action"] = "down") {downpx ($ _ REQUEST ['goods _ id']);} function uppx ($ goods_id) {if (! $ Goods_id) {$ goods_id = 0;} $ SQL = $ GLOBALS ['DB']-> query ("select px from ". $ GLOBALS ['DB']-> table ('product '). "where id = ". $ goods_id); $ rs = $ GLOBALS ['DB']-> fetch_array ($ SQL); if (! $ Rs) {$ oldpx = 1;} else {$ oldpx = $ rs ['px '];} if ($ oldpx> 1) {$ oldpx = $ oldpx-1; $ GLOBALS ['DB']-> query ("update ". $ GLOBALS ['DB']-> table ('product '). "set px = px + 1 where px = ". $ oldpx); $ GLOBALS ['DB']-> query ("update ". $ GLOBALS ['DB']-> table ('product '). "set px = px-1 where id = ". $ goods_id);} header ("Location: product_list.php"); exit;} function downpx ($ goods_id) {if (! $ Goods_id) {$ goods_id = 0;} $ SQL = $ GLOBALS ['DB']-> query ("select px from ". $ GLOBALS ['DB']-> table ('product '). "where id = ". $ goods_id); $ rs = $ GLOBALS ['DB']-> fetch_array ($ SQL); if (! $ Rs) {$ oldpx = 1;} else {$ oldpx = $ rs ['px '];} // if $ oldpx = 1, $ sql1 = $ GLOBALS ['DB']-> query ("select * from ". $ GLOBALS ['DB']-> table ('product '). "order by px desc limit 1"); $ row = $ GLOBALS ['DB']-> fetch_array ($ sql1); if (! $ Row) {$ maxpx = 999;} else {$ maxpx = $ row ['px '];} // here the largest $ maxpx = 4 if ($ oldpx <$ maxpx) {$ oldpx = $ oldpx + 1; $ GLOBALS ['DB']-> query ("update ". $ GLOBALS ['DB']-> table ('product '). "set pixels = px-1 where px = ". $ oldpx); $ GLOBALS ['DB']-> query ("update ". $ GLOBALS ['DB']-> table ('product '). "set px = px + 1 where id = ". $ goods_id);} header ("Location: product_list.php"); exit ;}

Based on your code, I have demonstrated it completely to achieve the desired effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.