Jump to content
php.lv forumi
Sign in to follow this  
Emis

OOP, jQuery checkbox delete

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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 by coofen

Share this post


Link to post
Share on other sites
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 by Emis

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×