提高jquery / ajax / php调用的速度/效率的方法


Ways to improve the speed / efficiency of my jquery / ajax / php call?

我有一个包含80篇文章的表格,每一篇文章都有一个复选框。我有代码(AJAX, PHP)激活/停用散装的文章,而复选框被选中。我发现我的ajax调用需要2+秒激活/停用所有80条记录,这似乎很慢,你能看到一种方法来改善我的代码吗?

感谢所有的帮助!

这是我的Jquery/Ajax:
$(document).on("click",".applybtn",function() {
    // GET SELECTOR
    var selector = $("#selector").attr("name");
    // GET OPTIONS
    var option = $("#control").val();
    var option2 = $("#control2").val();
    if($(".idcheck").is(":checked")) {
        // GET CHECKBOXS
        var val = [];
        $(".idcheck:checked").each(function(i) {
            val[i] = $(this).val();
        });
        if(selector === 'article' || selector === 'articlecats') {
            $.ajax({
                type: "POST",
                url: "controllers/articlecontrol.php",
                data: { id: val, option: option, option2: option2, selector: selector },
                success: function(data){
                    if(option == 'delete' || option2 == 'delete') {
                        $(".idcheck:checked").each(function() {
                            $(this).closest("tr").remove();
                        })
                            }
                    if(option == 'activate' || option2 == 'activate' || option == 'deactivate' || option2 == 'deactivate') {
                        document.location.reload(true);
                    }
                    $('.successmessage').html(data).fadeIn("fast").fadeOut(3000);
                    if($('.select-all').is(':checked')) {
                        $('.select-all').prop('checked', false);
                    }
                }
            });
            return false;
        }
    }
    else {
        $('.errormessage').html("<div class='error'>Please Make Your Selection<'/div>").fadeIn("fast").fadeOut(3000);
    }
});

这是我的PHP:

if (isset($_POST['option']) || isset($_POST['option2'])) {
    // MULTI ACTIVATE ARTICLE
    if ($selector === 'article' && $option === 'activate' || $option2 === 'activate') {
        $id = $id;
        foreach($id as $val) {
            $update = array('article_active' => '1');
            $where = array('article_id' => $val);
            $sql = $database->update('wcx_articles', $update, $where);
        }
        if ($sql) { 
            echo '<div class="success">Article(s) Activated Successfully</div>';
        }
        else {
            echo '<div class="error">There was a problem Activating the Article(s) with ID'.$id.'</div>';   
        }
    }
    // MULTI DEACTIVATE ARTICLE
    if ($selector === 'article' && $option === 'deactivate' || $option2 === 'deactivate') {
        $id = $id;
        foreach($id as $val) {
            $update = array('article_active' => '0');
            $where = array('article_id' => $val);
            $sql = $database->update('wcx_articles', $update, $where);
        }
        if ($sql) { 
            echo '<div class="success">Article(s) Deactivated Successfully</div>';
        }
        else {
            echo '<div class="error">There was a problem Deactivating the Article(s) with ID'.$id.'</div>'; 
        }
    }
}

我使用自定义mysqli包装器来调用DB:

// UPDATE TABLE
public function update( $table, $variables = array(), $where = array(), $limit = '' ) {
    $sql = "UPDATE ". $table ." SET ";
    foreach( $variables as $field => $value ) {
        $updates[] = "`$field` = '$value'";
    }
    $sql .= implode(', ', $updates);
    foreach( $where as $field => $value ) {
        $value = $value;
        $clause[] = "$field = '$value'";
    }
    $sql .= ' WHERE '. implode(' AND ', $clause);
    if( !empty( $limit ) ) {
        $sql .= ' LIMIT '. $limit;
    }
    $query = mysqli_query( $this->link, $sql );
    if( mysqli_error( $this->link ) ) {
        $this->log_db_errors( mysqli_error( $this->link ), $sql, 'Fatal' );
        return false;
    }
    else {
        return true;
    }
}

看起来您正在为正在更新的每篇文章执行单个SQL更新查询。考虑修改PHP脚本,以便对多篇文章执行一次更新。

不生成这样的SQL:

UPDATE wcx_articles SET article_active = 1 WHERE article_id = 123;

UPDATE wcx_articles SET article_active = 1 WHERE article_id = 456;

UPDATE wcx_articles SET article_active = 1 WHERE article_id = 789;

执行一次更新可能会更有效率:

UPDATE wcx_articles SET article_active = 1 WHERE article_id IN (123, 456, 789);

这将是提高代码效率的一个不错的起点。

已更新

PHP

// MULTI ACTIVATE ARTICLE
if ($selector === 'article' && $option === 'activate' || $option2 === 'activate') {
    $id = $id;
    $update = "1";
    $where = implode(',',$id);
    $sql = $database->articleADUpdate('wcx_articles', $update, $where);
if ($sql) { 
    echo '<div class="success">Article(s) Activated Successfully</div>';
}
else {
    echo '<div class="error">There was a problem Activating the Article(s) with ID'.$id.'</div>';   
}
}
public function articleADUpdate($table, $variables, $where) {
    $sql = "UPDATE wcx_articles SET article_active =".$variables." WHERE article_id IN ($where)";
    if( !empty( $limit ) ) {
        $sql .= ' LIMIT '. $limit;
    }
    $query = mysqli_query( $this->link, $sql );
    if( mysqli_error( $this->link ) ) {
        $this->log_db_errors( mysqli_error( $this->link ), $sql, 'Fatal' );
        return false;
    }
    else {
        return true;
    }
}