Jump to content
php.lv forumi

OOP, jQuery checkbox delete


Emis

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!

Link to comment
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
Link to comment
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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...