dongliao3450 2015-01-18 05:13
浏览 23
已采纳

如何在PHP中显示从MySQL获取的选定行时设置自定义顺序

i am stuck in a problem and hope that you can help.I need to save and display featured projects in my portfolio in my specified order.

Note:This order has be defined from the front-end by the user.

I have placed a bool value , If the user marks a project featured it will turn to True other wise by default it will be False.This way i am getting only featured projects saved in Database

but

I want them to be saved in a custom order.For example if i have 5 featured projects selcted, the resulting rows will be as follows:


ID  NAME    FEATURED        DATE

1   abc      true       18/jan/2015 
2   fgh      true       13/oct/2014
3   klm      true        7/sep/2014
4   qrs      true        2/aug/2014
5   xyz      true        1/feb/2014

Lets suppose i think that my project on 2/aug/2014 was the best and should be displayed on top in the portfolio; How can i re-order them or redefine their order from the front-end so that the projects will show according to that order

( My English is not good enough, so please bear with me )

Thanks

  • 写回答

1条回答 默认 最新

  • duanhao7786 2015-01-18 08:09
    关注

    Since you are new to things, so i would suggest you a very simple and easy way to handle this.

    Jquery sortable is the best answer to your question.

    http://jqueryui.com/sortable/

    Here is an example code that i have tested and it works great. I am giving you code for two files 1 This is index.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Sorting Items on the fly using jQuery UI, PHP & MySQL</title>
    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.10.1.custom.min.js"></script>
    <script>
    $(document).ready( 
    function() {
    $("#sortme").sortable({
    update : function () {
    serial = $('#sortme').sortable('serialize');
    $.ajax({
    url: "sort_menu.php",
    type: "post",
    data: serial,
    error: function(){
    alert("theres an error with AJAX");
    }
    });
    }
    });
    }
    );
    </script>
    </head>
    <body>
    <h1>Menu List</h1>
    
    <style>
    #sortme li{
    margin: 0 3px 3px 3px;
    padding: 0.4em;
    padding-left: 1.5em;
    font-size: 1.4em;
    height: 18px;
    list-style: none;
    background: bisque;
    width: 150px;
    }
    </style>
    <ul id="sortme">
    <?php
    
    
    
    // Connecting to Database
    mysql_connect('localhost', 'root', '') or die ('Cant Connect to MySQL');
    
    // Selecting Database
    mysql_select_db('sorting') or die ('Cant select Database');
    
    
    
    
    // Getting menu items from DB
    $result = mysql_query("SELECT * FROM `menu` ORDER BY `sort` ASC") or die(mysql_error());
    
    
    while($row = mysql_fetch_array($result)) {
    echo '<li id="menu_' . $row['id'] . '">' . $row['title'] . "</li>
    ";
    }
    ?>
    </ul>
    </body>
    </html>
    

    2 sort_menu.php

    <?php
    // Connecting to Database
    mysql_connect('localhost', 'root', '') or die ('Cant Connect to MySQL');
    
    // Selecting Database
    mysql_select_db('sorting') or die ('Cant select Database');
    
    $menu = $_POST['menu'];
    for ($i = 0; $i < count($menu); $i++) {
    mysql_query("UPDATE `menu` SET `sort`=" . $i . " WHERE `id`='" . $menu[$i] . "'") or die(mysql_error());
    }
    ?>
    

    Note: 1 Get the jquery UI and jquery from live server 2 Make sure you have a database having a column "menu" with three fields; id, title and sort.

    Enjoy :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。