Emis Posted August 9, 2018 Report Share Posted August 9, 2018 Sveiki! Sāku apgūt OOP un netieku skaidrībā ar checkbox delete funkciju. Tehniski šī(zemāk kods) funkcija strādā, bet tas nav īsti tas, kas man ir nepieciešams. <div class="container"> <?php if(mysqli_num_rows($result) > 0) { ?> <div class="table-responsive"> <table class="table table-bordered"> <tr> <th>Delete</th> </tr> <?php while($row = mysqli_fetch_array($result)) { ?> <tr id="<?php echo $row["id"]; ?>" > <td><input type="checkbox" name="id[]" class="delete_product" value="<?php echo $row["id"]; ?>" /></td> </tr> <?php } ?> </table> </div> <?php } ?> <div align="center"> <button type="button" name="btn_delete" id="btn_delete" class="btn btn-success">Delete</button> </div> <?php //delete.php $connect = mysqli_connect("localhost", "root", "", "test"); if(isset($_POST["id"])) { foreach($_POST["id"] as $id) { $query = "DELETE FROM products WHERE id = '".$id."'"; mysqli_query($connect, $query); } } ?> <script> $(document).ready(function(){ $('#btn_delete').click(function(){ if(confirm("Are you sure you want to delete this?")) { var id = []; $(':checkbox:checked').each(function(i){ id[i] = $(this).val(); }); if(id.length === 0) //tell you if the array is empty { alert("Please Select atleast one checkbox"); } else { $.ajax({ url:'delete.php', method:'POST', data:{id:id}, success:function() { for(var i=0; i<id.length; i++) { $('tr#'+id[i]+'').css('background-color', '#ccc'); $('tr#'+id[i]+'').fadeOut('slow'); } } }); } } else { return false; } }); }); </script> vēlos šo funkciju ieviest iekš klases, kura printē arā visu informāciju no db un pēcāk ar checkbox palīdzību varētu izdzēst vairākus produktus reizē. class ViewProduct extends Product { public function showProduct() { $datas = $this->getAllProducts(); foreach ($datas as $data){ echo "<div class='col-sm-3'> <div class='thumbnail'> <ul class='list-group'> <li class='list-group-item'><b>SKU:</b> ".$data['sku']."</li> <li class='list-group-item'><b>Name:</b> ".$data['name']."</li> <li class='list-group-item'><b>Price:</b> ".number_format($data['price'],2)." €</li> <li class='list-group-item'><b> ".$data['attribute_name'].": </b>" .$data['attribute_value']." ".$data['attribute_unit']."</li> //šeit paradzēts checkbox </ul> <span class='label label-info'>".$data['attribute_product']."</span> </div> </div>"; } } } Vai varat lūdzu ieteikt kādu paņēmienu kā labāk šo funkciju izpildīt? Mēģināju dažādus variantus, bet neviens līdz galam arī nenostrādāja. Savu variantu neliku, lai redzētu vai maz biju uz pareizā ceļa. Paldies! Quote Link to comment Share on other sites More sharing options...
coofen Posted August 9, 2018 Report Share Posted August 9, 2018 (edited) Un, kur ir problēma? Komentārā redzu, "//šeit paredzēts checkbox" .. Ievieto tur čekboksi. public function deleteProducts() { $products = $_POST['id']; //Iespējams var ieviest tranzakcijas, lai vai nu izdzēš visus, via nevienu u.tml. foreach($products as $product) { if ($this->productExists($product) && (... kadi validacijas čeki) ) { $productModel = $this->getProduct($product); $productModel->delete(); } } } Edited August 9, 2018 by coofen Quote Link to comment Share on other sites More sharing options...
Emis Posted August 9, 2018 Author Report Share Posted August 9, 2018 (edited) 2 hours ago, coofen said: Un, kur ir problēma? Komentārā redzu, "//šeit paredzēts checkbox" .. Ievieto tur čekboksi. public function deleteProducts() { $products = $_POST['id']; //Iespējams var ieviest tranzakcijas, lai vai nu izdzēš visus, via nevienu u.tml. foreach($products as $product) { if ($this->productExists($product) && (... kadi validacijas čeki) ) { $productModel = $this->getProduct($product); $productModel->delete(); } } } Paldies par atbildi un ieteikumiem. Bet es iztiku vispār bez šis funkcijas. Pietika ar delete.php, jQueriju un checkboxu. Viss strādā un dati dzēšās. Ir gan maziņš gļuks ar ajax succes, bet patreiz varu iztikt bez tā. Edited August 9, 2018 by Emis Quote Link to comment Share on other sites More sharing options...
coofen Posted August 13, 2018 Report Share Posted August 13, 2018 Būtu forši, ja arī tu padalītos ar risinājumu, tikai tā, lai "topikā" redzama pabeigtības sajūta. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.