简体   繁体   中英

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

I have a table with 80 articles in it, I have a checkbox for each article. I have code (AJAX, PHP) to activate / deactivate the articles in bulk while checkboxes are checked. I am finding my ajax call takes 2+ seconds to activate / deactivate all 80 records, this seems slow, can you see a way to improve my code?

All help appreciated!

Here is my 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);
    }
});

Here is my 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>'; 
        }
    }
}

I'm using a custom mysqli wrapper to make the calls to 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;
    }
}

It looks like you're performing a single SQL update query for each article you're updating. Consider changing your PHP script so that you perform a single update for multiple articles.

Instead of generating SQL like this:

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;

Performing one single update would likely be more efficient:

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

That would be a decent starting point for improving efficiency of your code.

UPDATED

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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM