Home  >  Article  >  Backend Development  >  The ninth day of php practice_PHP tutorial

The ninth day of php practice_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:06723browse

1.jquery事件可以绑定N个,如果不进行取消就会重复调用绑定的事件。深感体会,搞了两小时终于发现其中奥妙。
以下代码不能单独运行的。需要 bootstrap和jquery
[javascript]  /**
* 用于显示对话框消息框
* 参数 title 消息标题
* 参数 content 消息内容
* 参数 buttomTitle 处理消息的按钮自定义的,比如确认删除
* 参数 fun 自定义按钮click事件
* 参数 passOnData 传递到自定义fun里的参数
*/ 
function show_Msg (title,content,buttomTitle,fun,passOnData) { 
    $("#msg #myModalLabel").html(title); 
    $("#msg .modal-body").html(content); 
    $('#msg #msg_c').html(buttomTitle).click(function(){ 
        fun(passOnData);//调用自定义的函数,以及传递自定义的数据  
        $('#msg').modal('hide');//点击完就把窗口隐藏了  
        $(this).unbind('click');//如果不取消事件,那么将重复调用。。  
    });; 
    $('#msg').modal('show'); 

/**
* 用于显示对话框消息框
* 参数 title 消息标题
* 参数 content 消息内容
* 参数 buttomTitle 处理消息的按钮自定义的,比如确认删除
* 参数 fun 自定义按钮click事件
* 参数 passOnData 传递到自定义fun里的参数
*/
function show_Msg (title,content,buttomTitle,fun,passOnData) {
 $("#msg #myModalLabel").html(title);
 $("#msg .modal-body").html(content);
 $('#msg #msg_c').html(buttomTitle).click(function(){
  fun(passOnData);//调用自定义的函数,以及传递自定义的数据
  $('#msg').modal('hide');//点击完就把窗口隐藏了
  $(this).unbind('click');//如果不取消事件,那么将重复调用。。
 });;
 $('#msg').modal('show');
}html消息框模板
[html]   

 


Call routine
[javascript] show_Msg('The title should be long', 'Here you can write HTML such as bold font', 'Delete', function(e){
alert(e);
}, 'Here is the data I passed after clicking delete');

show_Msg('The title should be long', 'You can write HTML here such as bold font', 'Delete', function(e){
alert(e);
},'Here is the data I passed after clicking delete');

[javascript] function admin_content_del (id) {
var data=listData[id];
Show_Msg('Confirm deletion',data.content,'Confirm deletion',function(delId){
         $.ajax({
​​​​​ url: 'http://localhost/l/index.php',
          type: 'get',
           dataType: 'json',
data: {
              m: 'admin',
a: 'delcontent',
            id: delId
          },
Complete: function(xhr, textStatus) {
                 //called when complete                                                   },
Success: function(data, textStatus, xhr) {
If(data.state='ok'){
                                                                     admin_content(1);                                           show_Msg_success('Deletion successful');
                                                                                            }else{
                    show_Msg_success('Deletion failed');
                                                                                                                                                },
​​​​​ error: function(xhr, textStatus, errorThrown) {
                   //called when there is an error                                                                                                                                                    });
                                   
},data.id);
}

function admin_content_del (id) {
var data=listData[id];
show_Msg('Confirm deletion',data.content,'Confirm deletion',function(delId){
$.ajax({
URL: 'http://localhost/l/index.php',

Type: 'get',

DataType: 'json',
Data: {
m: 'admin',
a: 'delcontent',
​ id: delId
},
Complete: function(xhr, textStatus) {
//called when complete
},
Success: function(data, textStatus, xhr) {
If(data.state='ok'){
        admin_content(1);
Show_Msg_success('Deletion successful');
         
}else{
Show_Msg_success('Deletion failed');
}
},
​ error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});

},data.id);
}


2. setTimeout delays the execution of events. This message is really good and very commonly used, but this method of clearing messages is simple and crude.


[javascript]  /**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/ 
function show_Msg_success(content,face){ 
    if (face==null) { 
        face=true; 
    } 
 
    face = face?'success':'error'; 
     
    strTag='

'+content+'
'; 
 
    $(strTag).prependTo('#main'); 
 
    setTimeout(function(){ 
        $(".alert").alert('close'); 
    },3000); 

/**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/
function show_Msg_success(content,face){
 if (face==null) {
  face=true;
 }

 face = face?'success':'error';
 
    strTag='

'+content+'
';

$(strTag).prependTo('#main');

setTimeout(function(){
$(".alert").alert('close');
},3000);
}


3. It is better to use pure DOM to select checkbox. The arrt() method of jquery is cheating
checkbox.attr('checked',$(this).get()[0].checked);

The checkbox is checked, but it works for the first time and also for the second time. After the third time, it strangely fails. The problem is not the truth.
[javascript] function click_tr() {
var checkbox = $("#mainData tr input");

Checkbox.eq(0).click(function() {
//checkbox.attr('checked',$(this).get()[0].checked);

         bool = $(this).get()[0].checked;

for (var i = 1; i < checkbox.length; i++) {
               checkbox[i].checked = bool;
         };

});

$("#mainData tr").each(function(index) {
          $(this).click(function() {

If (index == 0) {

                                                                          var val = checkbox.eq(index);
                      val = val.get()[0];
              var bool = val.checked;

                               //alert(index);                               If (bool) {
                        val.checked = false;
                                                                                                                           val.checked = true;
                                                                                                                                                                                                                                                                                                                                       });

});

function click_tr() {
var checkbox = $("#mainData tr input");

checkbox.eq(0).click(function() {
//checkbox.attr('checked',$(this).get()[0].checked);

bool = $(this).get()[0].checked;

for (var i = 1; i < checkbox.length; i++) {

Checkbox[i].checked = bool;
};

});

$("#mainData tr").each(function(index) {
$(this).click(function() {

if (index == 0) {

} else {

var val = checkbox.eq(index);
          val = val.get()[0];

var bool = val.checked;

//alert(index);

If (bool) {
val.checked = false;
} else {
val.checked = true;

}

}
 
});

}); I did some research and wrote a test code, but still to no avail. Those who don’t know the truth can’t be selected.
[html]


       












Select all
Uncheck



















Select all
Uncheck



4. After talking so much, there are no renderings.

5. The title says it’s about PHP practice, but the previous posts are all PHP codes. . Posted below is the php code for batch deletion
[php] /**
* Used to delete messages in batches
*/
function batchDelContent(){
$json['state']="no";

$data=Array();

$arr=$_POST['delcontent'];
If (count($arr)>0) {
          $json['state']="ok";
}  

foreach ($arr as $value) {

          $result=$this->db
->where("id=".$value)
->table("data")
->delete();

If ($result) {
                $arr_data['state']="ok";
         }else{
                $arr_data['state']="no";
         } 
         $arr_data['id']=$value;

         $data[]=$arr_data;
}  

$json['data']=$data;
echo json_encode($json);
}

/**
* Used to delete messages in batches
​*/
function batchDelContent(){
$json['state']="no";

$data=Array();

$arr=$_POST['delcontent'];
if (count($arr)>0) {
$json['state']="ok";
}

foreach ($arr as $value) {

$result=$this->db
->where("id=".$value)
->table("data")
->delete();

if ($result) {
$arr_data['state']="ok";
}else{
$arr_data['state']="no";
}
$arr_data['id']=$value;

$data[]=$arr_data;
}

$json['data']=$data;
echo json_encode($json);
}That’s all the php code I wrote today. . Most of it is javascript


I posted admin.js for everyone to watch.
[javascript] // JavaScript Document
$(document).ready(function(e) {

$("#menu a").click(function() {

         switch ($(this).text()) {
case 'All messages':
                                                                     admin_content(1);                      break;

             case 'Basic settings':
                   $("#main #mainData").load('admin_config.html?r='+Math.random());
break;

                   default:
break;
         } 



});

$("#main #mainData").load('admin_config.html?r='+Math.random());
//$("#start").click();
});


/**
*Message management
*/
listData=null;
function admin_content(page) {

$.ajax({
​​ ​ url: 'http://localhost/l/admin.php?m=admin&a=content&page=' + page + '&rand=' + Math.random(),
        type: 'get',
        dataType: 'json',
data: {},
complete: function(xhr, textStatus) {
                  //called when complete                                               },
Success: function(json, textStatus, xhr) {
If (json['state'] == 'ok') {
                    var page_start = json['start'];//Start paging
                  var page_end = json['end'];//End of paging
                                                                         var page_page = json['page'];//Paging the current page                       listData = json['data'];//Paging data

                table_html='';
              table_html+='Batch delete';
/**                                                                 * Generate table content
*/                                                                table_html+= '

';
for (i = 0; i < listData.length; i++) {
                    var trClass = (i % 2 == 0) ? 'class="info"' : '';

//var tr_html = '';  
                    tr_html = ''; 
                    tr_html+=''; 
                    //删除  
 
                    tr_html+=''; 
                    tr_html+=''; 
                    tr_html+=''; 
 
                    table_html += tr_html; 
                } 
                table_html += '
OperationUsernameMessage contentPublication time
删除' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
'+listData[i].id+'' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
'; 
 
/**
*Generate pagination
*/ 
                var page_html = '
';


              var mainData = $("#main #mainData");
                       mainData.html(table_html);
                       mainData.append(page_html);

                                                                                                                                   admin_content_page(page_page,page_end); //Hook paging click event Click_tr (); // Hang the click event;
                                                                                                                                     


                  //alert(json.data); 
        },
​​​​ error: function(xhr, textStatus, errorThrown) {
                   //called when there is an error                                                   } 
});
}

/**
* On-hook paging event
* Parameter page_page current page
* Parameter page_end Number of pages
*/
function admin_content_page(page_page,page_end) {
$("#mainPage a").click(function() {
        var charStr = $(this).text();
var num = charStr;
If (charStr == "»") {
              num = parseInt(page_page) + 1;
If (page_end < num) {
Return;
                                                                                                                                     
           } else if (charStr == "«") {
              num = parseInt(page_page) - 1;
If (num <= 0) {
Return;
                                                                                                                                               } 

        admin_content(num);

});
}



function getLocalTime(nS) {
Return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/, ' ');
}

function admin_content_del (id) {
var data=listData[id];
Show_Msg('Confirm deletion',data.content,'Confirm deletion',function(delId){
         $.ajax({
​​​​​ url: 'http://localhost/l/index.php',
          type: 'get',
           dataType: 'json',
data: {
              m: 'admin',
a: 'delcontent',
            id: delId
          },
Complete: function(xhr, textStatus) {
                  //called when complete                                                 },
Success: function(data, textStatus, xhr) {
If(data.state='ok'){
                                                                     admin_content(1);                                     show_Msg_success('删除成功'); 
                 
            }else{ 
                show_Msg_success('删除失败'); 
            } 
          }, 
          error: function(xhr, textStatus, errorThrown) { 
            //called when there is an error  
          } 
        }); 
         
    },data.id); 

 
function admin_content_del_pl() { 
 
    var checkbox = $("#mainData :checked"); 
 
    var listId = Array(); 
 
    checkbox.each(function() { 
        //alert($(this).get()[0].name);  
        if ($(this).get()[0].name == 'delcontent[]') { 
            listId.unshift($(this).val()); 
        } 
    }); 
 
    $.ajax({ 
      url: 'http://localhost/l/admin.php?m=admin&a=batchDelContent', 
      type: 'POST', 
      dataType: 'json', 
      data: {delcontent: listId 
      }, 
      complete: function(xhr, textStatus) { 
        //called when complete  
      }, 
      success: function(json, textStatus, xhr) { 
        if(json.state=='ok'){ 
            var data = json.data; 
            for (var i = 0; i < data.length; i++) { 
                if(data[i]['state']=='ok'){ 
                    show_Msg_success(data[i].id + '删除成功'); 
                }else{ 
                    show_Msg_success(data[i].id + '删除失败',false); 
                } 
            }; 
 
        }else{ 
 
        } 
        admin_content(1); 
      }, 
      error: function(xhr, textStatus, errorThrown) { 
        //called when there is an error  
      } 
    }); 
     
//  alert(listId);  

 
 
/**
* Used to display dialog message box
* Parameter title message title
* Parameter content message content
* Parameter buttomTitle is a customized button for processing messages, such as confirm deletion
* Parameter fun custom button click event
* The parameter passOnData is passed to the parameter in the custom fun
*/ 
function show_Msg (title,content,buttomTitle,fun,passOnData) { 
    $("#msg #myModalLabel").html(title); 
    $("#msg .modal-body").html(content); 
    $('#msg #msg_c').html(buttomTitle).click(function(){ 
        fun(passOnData);//调用自定义的函数,以及传递自定义的数据  
        $('#msg').modal('hide');//点击完就把窗口隐藏了  
        $(this).unbind('click');//如果不取消事件,那么将重复调用。。  
    });; 
    $('#msg').modal('show'); 

/**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/ 
function show_Msg_success(content,face){ 
    if (face==null) { 
        face=true; 
    } 
 
    face = face?'success':'error'; 
     
    strTag='
'+content+'
';

$(strTag).prependTo('#main');

setTimeout(function(){
         $(".alert").alert('close');
},3000);
}

//show_Msg_content('Ahhhhhhhhhhhhhhhhhhhhhhhh income tax')
/*
show_Msg('The title should be long', 'You can write HTML here such as bold font', 'Delete', function(e){
alert(e);
},'Here is the data I passed after clicking delete');
*/

function click_tr() {
var checkbox = $("#mainData tr input");

Checkbox.eq(0).click(function() {
//checkbox.attr('checked',$(this).get()[0].checked);

         bool = $(this).get()[0].checked;

for (var i = 1; i < checkbox.length; i++) {
               checkbox[i].checked = bool;
         };

});

$("#mainData tr").each(function(index) {
          $(this).click(function() {

If (index == 0) {

                                                                          var val = checkbox.eq(index);
                      val = val.get()[0];
              var bool = val.checked;

                               //alert(index);                               If (bool) {
                        val.checked = false;
                                                                                                                           val.checked = true;
                                                                                                                                                                                                                                                         
        });

});
}

// JavaScript Document
$(document).ready(function(e) {

$("#menu a").click(function() {

switch ($(this).text()) {
case 'All messages':

​ admin_content(1);

Break;

case 'Basic settings':
$("#main #mainData").load('admin_config.html?r='+Math.random());
Break;

default:

Break;
}


});

$("#main #mainData").load('admin_config.html?r='+Math.random());

//$("#start").click();

});

/**
*Message management
*/

listData=null;

function admin_content(page) {

$.ajax({
url: 'http://localhost/l/admin.php?m=admin&a=content&page=' + page + '&rand=' + Math.random(),
type: 'get',
dataType: 'json',
data: {},
complete: function(xhr, textStatus) {
//called when complete
},
success: function(json, textStatus, xhr) {
if (json['state'] == 'ok') {
var page_start = json['start'];//分页开始
var page_end = json['end'];//分页结束
var page_page = json['page'];//分页当前页面
listData = json['data'];//分页数据

table_html='';
table_html+='批量删除';
/**    
* 生成 表格内容
*/    
    table_html+= '

';
    for (i = 0; i < listData.length; i++) {
     var trClass = (i % 2 == 0) ? 'class="info"' : '';

     //var tr_html = '

';
     tr_html = '';
     tr_html+='';
     //删除

     tr_html+='

';
     tr_html+='';
     tr_html+='';

     table_html += tr_html;
    }
    table_html += '

操作用户名留言内容发表时间
删除' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
'+listData[i].id+' ' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
';

/**
*生成分页
*/
    var page_html = '

';


    var mainData = $("#main #mainData");
    mainData.html(table_html);
    mainData.append(page_html);

    admin_content_page(page_page,page_end); //挂接分页点击事件
    click_tr();//挂接行点击事件;
   }

 

   //alert(json.data);
  },
  error: function(xhr, textStatus, errorThrown) {
   //called when there is an error
  }
 });
}

/**
* On-hook paging event
* Parameter page_page current page
* Parameter page_end Number of pages
*/
function admin_content_page(page_page,page_end) {
 $("#mainPage a").click(function() {
  var charStr = $(this).text();
  var num = charStr;
  if (charStr == "»") {
   num = parseInt(page_page) + 1;
   if (page_end < num) {
    return;
   }

  } else if (charStr == "«") {
   num = parseInt(page_page) - 1;
   if (num <= 0) {
    return;
   }
  }

  admin_content(num);

 });
}

 

function getLocalTime(nS) {
 return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/, ' ');
}

function admin_content_del (id) {
 var data=listData[id];
 show_Msg('确认删除',data.content,'确认删除',function(delId){
  $.ajax({
    url: 'http://localhost/l/index.php',
    type: 'get',
    dataType: 'json',
    data: {
     m: 'admin',
     a: 'delcontent',
     id: delId
    },
    complete: function(xhr, textStatus) {
      //called when complete
    },
    success: function(data, textStatus, xhr) {
     if(data.state='ok'){
      admin_content(1);
      show_Msg_success('删除成功');
      
     }else{
      show_Msg_success('删除失败');
     }
    },
    error: function(xhr, textStatus, errorThrown) {
      //called when there is an error
    }
  });
  
 },data.id);
}

function admin_content_del_pl() {

 var checkbox = $("#mainData :checked");

 var listId = Array();

 checkbox.each(function() {
  //alert($(this).get()[0].name);
  if ($(this).get()[0].name == 'delcontent[]') {
   listId.unshift($(this).val());
  }
 });

 $.ajax({
   url: 'http://localhost/l/admin.php?m=admin&a=batchDelContent',
   type: 'POST',
   dataType: 'json',
   data: {delcontent: listId
   },
   complete: function(xhr, textStatus) {
     //called when complete
   },
   success: function(json, textStatus, xhr) {
     if(json.state=='ok'){
      var data = json.data;
      for (var i = 0; i < data.length; i++) {
       if(data[i]['state']=='ok'){
        show_Msg_success(data[i].id + '删除成功');
       }else{
     show_Msg_success(data[i].id + '删除失败',false);
       }
      };

     }else{

     }
     admin_content(1);
   },
   error: function(xhr, textStatus, errorThrown) {
     //called when there is an error
   }
 });
 
// alert(listId);
}


/**
* 用于显示对话框消息框
* 参数 title 消息标题
* 参数 content 消息内容
* 参数 buttomTitle 处理消息的按钮自定义的,比如确认删除
* 参数 fun 自定义按钮click事件
* 参数 passOnData 传递到自定义fun里的参数
*/
function show_Msg (title,content,buttomTitle,fun,passOnData) {
 $("#msg #myModalLabel").html(title);
 $("#msg .modal-body").html(content);
 $('#msg #msg_c').html(buttomTitle).click(function(){
  fun(passOnData);//调用自定义的函数,以及传递自定义的数据
  $('#msg').modal('hide');//点击完就把窗口隐藏了
  $(this).unbind('click');//如果不取消事件,那么将重复调用。。
 });;
 $('#msg').modal('show');
}
/**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/
function show_Msg_success(content,face){
 if (face==null) {
  face=true;
 }

 face = face?'success':'error';
 
    strTag='

'+content+'
';

 $(strTag).prependTo('#main');

 setTimeout(function(){
  $(".alert").alert('close');
 },3000);
}

//show_Msg_content('啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊所得税')
/*
show_Msg('标题要长长长长的','这里可以写html比如加粗的的字体噢','删除',function(e){
 alert(e);
},'这里是点击删除后我传递过去的数据');
*/

function click_tr() {
 var checkbox = $("#mainData tr input");

 checkbox.eq(0).click(function() {
  //checkbox.attr('checked',$(this).get()[0].checked);

  bool = $(this).get()[0].checked;

  for (var i = 1; i < checkbox.length; i++) {
checkbox[i].checked = bool;
};

});

$("#mainData tr").each(function(index) {
$(this).click(function() {

if (index == 0) {

} else {
var val = checkbox.eq(index);
val = val.get()[0];
var bool = val.checked;

//alert(index);
if (bool) {
val.checked = false;
} else {
val.checked = true;
}
}

});

});
}index.html代码
[html]  
 
 
   
  无标题文档 
   
 
 
 
 

 
   
 
     
 
       

瀑布流留言板管理系统