<?php
$query = $db->query("SELECT * FROM tbl_message", PDO::FETCH_ASSOC);
if (isset($_POST['type'])) {
if ($_POST['type'] == 'delete') {
$delete = $db->prepare("DELETE FROM tbl_message WHERE id = ?");
$delete->execute([$_POST['id']]);
$response = [
'status' => (bool) $delete->rowCount(),
'text' => $delete->rowCount() ? 'Silme işlemi başarılı' : 'Silme işlemi başarısız'
];
} elseif ($_POST['type'] == 'update') {
$update = $db->prepare("UPDATE tbl_message SET u_sub = ?, u_message = ? WHERE id = ?");
$update->execute([
$_POST['konu'],
$_POST['mesaj'],
$_POST['id']
]);
$response = [
'status' => (bool) $update->rowCount(),
'text' => $update->rowCount() ? 'Güncelleme işlem başarılı' : 'Güncelleme işlemi başarısız!'
];
} else {
$response = [
'status' => false,
'text' => 'Hatalı bir işlem gerçekleşti'
];
}
echo json_encode($response);
exit();
}
?>
<!doctype html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<title>Document</title>
<style>
table {
border-collapse: collapse;
}
table, tr, td, th {
border: 1px solid;
}
</style>
</head>
<body>
<table style="width: 100%">
<thead>
<tr>
<th>ID</th>
<th>AD</th>
<th>SOYAD</th>
<th>TELEFON</th>
<th>KONU</th>
<th>MESAJ</th>
<th>SİL</th>
<th>GÜNCELLE</th>
</tr>
</thead>
<tbody>
<?php
if ($query->rowCount()){
foreach( $query as $row ){?>
<tr id="<?= $row['id'] ?>">
<td style="width:5%"><?php echo $row['id'] ?></td>
<td style="width:20%"><?php echo $row['u_name'] ?></td>
<td style="width:20%"><?php echo $row['u_surname'] ?></td>
<td style="width:20%"><?php echo $row['u_phone'] ?></td>
<td style="width:5%">
<select name="u_konu" id="konu">
<option value="1" <?= $row['u_sub'] == 1 ? 'selected' : null ?> >Öneri</option>
<option value="2" <?= $row['u_sub'] == 2 ? 'selected' : null ?> >Talep</option>
<option value="3" <?= $row['u_sub'] == 3 ? 'selected' : null ?> >Şikayet</option>
</select>
</td>
<td style="width:20%">
<textarea name="u_mesaj" id="u_mesaj" cols="15" rows="2"><?php echo $row['u_message'] ?></textarea>
</td>
<td style="width:5%">
<button data-type="delete" class="delete"><i class="fa fa-times"></i></button>
</td>
<td style="width:5%">
<button data-type="update" class="update"><i class="fa fa-redo"></i></button>
</td>
</tr>
<?php
}
} else { ?>
<tr>
<td colspan="8" style="text-align: center">Tabloda veri bulunamadı</td>
</tr>
<?php } ?>
</tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function () {
$(".update, .delete").on("click", function () {
let id = $(this).parents("tr").attr("id"),
konu = $(this).parents("tr").find("select").val(),
mesaj = $(this).parents("tr").find("textarea").val(),
type = $(this).data("type")
if (type === 'delete') {
if (!confirm('Silmek istediğine emin misin?'))
return false
}
$.ajax({
method: 'post',
data: {
konu: konu,
mesaj: mesaj,
id: id,
type: type
},
dataType: "json"
}).done(e => {
alert(e.text)
if (type === 'delete' && e.status)
$(this).parents("tr").fadeOut()
})
})
})
</script>
</body>
</html>
Ben de daha evvel kullandığım iki şekli ile örnek vereyim, mantık aynı tabii de örnek çeşitliliği açısından.
Bir diziyi döngüye soktuğumuzda başka bir array daha oluşturmadan içindeki değerleri direkt olarak değiştirebilmek için foreach gibi döngülerde kullanabilirsin;
$array = ['item 1', 'item 2', 'item 3', 'item 4', 'item 5'];
foreach ($array as $index => &$item) {
$item = "item $index";
}
print_r($array);
// Array
// (
// [0] => item 0
// [1] => item 1
// [2] => item 2
// [3] => item 3
// [4] => item 4
// )
Ya da belki bazen recursive bir anonim fonkiyon yazmak istersin, fonksiyonun döndürdüğü değeri yine aynı fonksiyonda kullanmak istersin;
$func = function ($param) use (&$func) {
$return = $param * 3;
if ($return < 50)
return $func($return);
return $return;
};
echo $func(2);
// 54
Örnekler biraz garip olabilir ama aklıma ilk gelen şeyi yaptım :)
function a(int $accumulator, array $array)
{
foreach ($array as $item) {
$accumulator += $item;
echo $accumulator . '<br>';
}
}
$sayi = 100;
$array = [1, -2, 3, -4, 5, -6];
a($sayi, $array);
Şayet doğru anladıysam şöyle bir şey işinize yarayabilir
<?php
if(isset($_GET['duzenle_id'])){
$duzenle = $db->query("SELECT * FROM urun WHERE id = '{$_GET['duzenle_id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
}
$cek = $db->query("SELECT * FROM kategori", PDO::FETCH_ASSOC);
if($cek->rowCount()){
foreach( $cek as $c ){ ?>
<label class="checkbox" for="kategori<?= $c['id'] ?>">
<input <?= isset($duzenle) ? ($c['id'] == $duzenle['kategori_id'] ? 'checked' : null) : null ?> class="checkbox__input" type="checkbox" name="kategori[]" value="<?= $c['id'] ?>" id="kategori<?= $c['id'] ?>">
<span class="checkbox__inner">
<span class="checkbox__tick"></span>
<span class="checkbox__text"><?= $c['baslik'] ?></span>
</span>
</label>
<?php }
}
?>
$duzenle = $db->query("SELECT * FROM urun WHERE id = '{$_GET['duzenle_id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
Bu sorgu sonucunda kategori_id değerini elde edebilyor musunuz?
Yoo estağfurullah öyle amiyane bir tabirle işim yok fakat yanlışları düzeltmek gerekir :)
let group = 1,
y = [
{
name: "John",
groupId: [1, 2]
},
{
name: "Doe",
groupId: [1]
},
{
name: "Sam",
groupId: [1, 3]
},
{
name: "Joe",
groupId: [3]
}
]
let a = y.filter(f => f.groupId.includes(group))
console.log(a)
// (3) [{…}, {…}, {…}]
// 0: {name: 'John', groupId: Array(2)}
// 1: {name: 'Doe', groupId: Array(1)}
// 2: {name: 'Sam', groupId: Array(2)}
// length: 3
// [[Prototype]]: Array(0)
Bu sorunun cevabını burada verebilecek kimsenin olduğunu düşünmüyorum :)
Hocam ben kodda değişiklikler yaptım, birebir kopyalayıp alırsanız bazı yerler uyuşmayabilir, daha çok mantığını göstermek adına. Sizin kendinize göre uygun yerleri düzeltmeniz lazım.