Table Table Implementation Click to modify PHP synchronization database sorting

Source: Internet
Author: User
Tags php foreach

In recent days to do a website, involved in a navigation management function! The leader said it was not used, but because of his own self-assertion, the 1-day function. The leader said that the navigation management is not to do! Easy to get involved with a lot of problems! Reckoned is the reason why customers are small!

No way to waste my 1 days of labor! Posted here, one is to communicate with you, the second, but also hope that we can make a hard shot of bricks, I believe that everyone's shot brick can enable me to better grow! I can learn more knowledge!

I am a beginner php bird! I hope you will advise!

On the code!

The first is the HTML code! The main realization! Table table, no CSS style added, because to do this step, to add style when the leader said no need! I didn't do it again!

I directly copy the data that I have made, the data is stored in the form! I used the CI framework to do it! So the data are all PHP code snippet output! $nav _array is a navigation array taken from the database

<Div>Here is the navigation management interface</Div><formMethod= "POST">    <Tableclass= "Table"Border= "1"style= "Border-collapse:collapse">        <thead><TR><th>Order</th><th>Navigation name</th><th>Management</th></TR></thead>        <tbody>        <?php foreach ($nav _array as $key + = $value):?>            <TRNum= "<?php echo $value [' id ']?>"Navid= "<?php echo $value [' id '];? > ">                <TD><?php echo $value [' sort ']?></TD>                <TD><?php echo $value [' Nav_name ']?></TD><!--<td><button class= "button" num= " -<?php//echo $value [' id ']?><!--"> Modify </button></td> -            </TR>        <?PHP Endforeach?>        </tbody>    </Table></form>

jquery code Implementation of the Click Change! The main idea is to erase the data from the original table with empty, add a section of input

This input HTML code should be able to use for a loop to get it done! I'm lazy! Be interested in your own research!

$ ('. Table '). On (' click ', '. Button '),function(){        varnum = $ ( This). attr (' num ');//button for current click event Flag        varCURRENT_TR = $ (' table '). Find (' tr[num= "' + num + '"] ');//TR Line for the current click event        varCurrent_tds = $ (current_tr). Find (' TD ');//Line of the current click event all TD        varNavid = $ (current_tr). attr (' Navid ');//This is the ID of the navigation is the primary key        //stores the original value of TD in the current click event Line, which is displayed in the HTML code of the form below        vararr = [];  for(vari = 0; i < current_tds.length; i++) {Arr[i]=$ (CURRENT_TDS). EQ (i). text (); }        //The HTML code to replace        vartd_html = ' <td><input type= ' hidden "name=" nav_id "value=" ' + Navid + ' "><input type=" text "name=" sort " Value= "' + arr[0] + '" ></td><td><input type= "text" name= "Nav_alter" value= "' + arr[1] + '" ></td ><td><input type= "Submit" Name= "" value= "OK" ></td> "; $('. Table '). Find (' tr[num= ' + num + ' "] '). Empty (). append (td_html); });

PHP code, here directly from my code copied over, not modified! Have shortcomings welcome to correct me! If you know, you should be able to change your code.

Index is mainly loaded in this page, using the CI input class, Post received data, from the database to extract a level of navigation, and then verify that the post data is present in the data in the database!

  Public functionindex () {$nav _id=$this->input->post (' nav_id ');//Navigation ID        $nav _name=$this->input->post (' Nav_alter ');//Modified navigation name        $sort=$this->input->post (' sort ');//Sort num        $data[' nav_array '] =$this->m_nav_manage->getnav ();//extract all the first level navigation, from the database//check whether the post data already exists in the database        if($nav _name||$sort) {            if( !$this->check_nav_in_array ($nav _name,$data[' Nav_array ']) || !$this->check_nav_in_array ($sort,$data[' Nav_array '])) {                $this->m_nav_manage->up_data_nav ($nav _name,$sort,$nav _id); $data[' nav_array '] =$this->m_nav_manage->Getnav (); //rewrite the database and extract the data            }        }        $this->load->view (' Nav_manage ',$data); }    /** Check if NAV exists in a two-dimensional array * @param $nav value to search * @param $array two-dimensional array searched * @return BOOL*/    Private functionCheck_nav_in_array ($value,$array) {         for($i= 0;$i<Count($array);$i++ ) {            foreach($array  as $key=$value ) {                if(In_array($value,$array[$i] ) ) {                    return TRUE; }            }        }    }

There is also a function in model that is used primarily to update the data and to change the sort

, the realization of the idea----is mainly used to pass the sort, $navid is the hidden input submitted in, $navname three variables are post data.

This realization of the comparison tangled! In short $nav _a obtains this Nav's ID $nav _b gets this nav's sort

The function realizes, I have not been recovered to come now! ~ ~ Leave yourself thinking slowly!

Get_one () The SELECT statement generated by the Select ID from $this->table WHERE sort = $sort; Other self-study!

     Public functionUp_data_nav ($navName,$sort,$navId) {    $nav _a=$this->m_common->get_one (' id ',$this->table,Array(' sort ' =$sort));//the ID of the NAV to be changed to $sort        $nav _b=$this->m_common->get_one (' sort ',$this->table,Array(' id ' = =$navId));//The sort//print_r of the Nav being modified ($nav _a); Print_r ($nav _b); Die ();        $nav _alter_array[' nav_name '] =$navName; $nav _alter_array[' sort '] =$sort; //Print_r ($nav _alter_array);d ie ();        $this->m_common->up_data ($this->table,$nav _alter_array,Array(' id ' = =$navId) ); $this->m_common->up_data ($this->table,Array(' sort ' =$nav _b[' sort ']),Array(' id ' = =$nav _a[' ID '] ) ); }
/** * Get a single data * @param $table table name * @param $where Condition array * @param string $fields query field default is ' * ' * * return Back an array*/     Public functionGet_one ($fields= ' * ',$table,$where ) {        if($where! = ") {            return $this->db->select ($fields)->from ($table)->where ($where)->get ()Row_array (); } Else {            return $this->db->select ($fields)->from ($table)->get ()Row_array (); }    }

Table Table Implementation Click to modify the PHP synchronization database sort

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.