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



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

i have table 80 articles in it, have checkbox each article. have code (ajax, php) activate / deactivate articles in mass while checkboxes checked. finding ajax phone call takes 2+ seconds activate / deactivate 80 records, seems slow, can see way improve code?

all help appreciated!

here jquery / ajax:

$(document).on("click",".applybtn",function() { // selector var selector = $("#selector").attr("name"); // options var alternative = $("#control").val(); var option2 = $("#control2").val(); if($(".idcheck").is(":checked")) { // 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' || alternative == 'deactivate' || option2 == 'deactivate') { document.location.reload(true); } $('.successmessage').html(data).fadein("fast").fadeout(3000); if($('.select-all').is(':checked')) { $('.select-all').prop('checked', false); } } }); homecoming false; } } else { $('.errormessage').html("<div class='error'>please create selection<\/div>").fadein("fast").fadeout(3000); } });

here php:

if (isset($_post['option']) || isset($_post['option2'])) { // multi activate article if ($selector === 'article' && $option === 'activate' || $option2 === 'activate') { $id = $id; foreach($id $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 problem activating article(s) id'.$id.'</div>'; } } // multi deactivate article if ($selector === 'article' && $option === 'deactivate' || $option2 === 'deactivate') { $id = $id; foreach($id $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 problem deactivating article(s) id'.$id.'</div>'; } } }

i'm using custom mysqli wrapper create calls db:

// update table public function update( $table, $variables = array(), $where = array(), $limit = '' ) { $sql = "update ". $table ." set "; foreach( $variables $field => $value ) { $updates[] = "`$field` = '$value'"; } $sql .= implode(', ', $updates); foreach( $where $field => $value ) { $value = $value; $clause[] = "$field = '$value'"; } $sql .= ' '. implode(' , ', $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' ); homecoming false; } else { homecoming true; } }

it looks you're performing single sql update query each article you're updating. consider changing php script perform single update multiple articles.

instead of generating sql this:

update wcx_articles set article_active = 1 article_id = 123;

update wcx_articles set article_active = 1 article_id = 456;

update wcx_articles set article_active = 1 article_id = 789;

performing 1 single update more efficient:

update wcx_articles set article_active = 1 article_id in (123, 456, 789);

that decent starting point improving efficiency of code.

javascript php jquery mysql ajax

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -