I am a newbie and I can take time to learn PDO php from 0, Well this is the concept to activate and deactivate the category. Can someone help me with this? I would like to update the status of the category, the status should be 0 (deactivate) or 1 (activate) to hide (hide) and show (show), how to activate and deactivate the status of the category?
Here is the structure of the table
-------------------------------------------
| category table |
-------------------------------------------
| cid | cname | clink | cparent | cstatus |
-------------------------------------------
index.php
public function Category()
{
$db = getBD();
$sql = $db->prepare("SELECT * FROM category WHERE cparent = 0");
$sql->execute();
$data = $sql->fetchAll(PDO::FETCH_ASSOC);
$menu = array();
foreach ($data as $categ) {
$sql = $db->prepare("SELECT * FROM category WHERE cparent = '".$categ['cid']."'");
$sql->execute();
$sdata = $sql->fetchAll(PDO::FETCH_ASSOC);
$categ['subcategorias'] = array();
foreach ($sdata as $subcateg) {
$categ['subcategorias'][] = $subcateg;
}
$menu[] = $categ;
}
return $menu;
//AQUI UPDATE STATUS
$status = $_GET['cstatus'];
$sql = $db->prepare("SELECT * FROM category WHERE cid = '".$status."'");
$sql->execute();
$data = $sql->fetchAll(PDO::FETCH_ASSOC);
//AQUI UPDATE STATUS
$status_var=$row->status;
if($status_var=='0')
{
$status_state=1;
}
else
{
$status_state=0;
}
$sql = $db->prepare("UPDATE category set cstatus='".$status_state."' WHERE cid='".$status."'");
if($sql)
{
header("Location:index.php");
}
}
category.php
$object = new myObject();
$data = '<table>
<tr>
<th>Id</th>
<th>Categoria</th>
<th>Sub Categoria</th>
<th>Link</th>
<th>Estado</th>
<th>Editar</th>
<th>Delete</th>
</tr>';
$menus = $object->cCategory();
if($menus)
{
foreach ($menus as $menu) {
$status = $menu['cstatus'];
if($status == '0'){
?>
<a href="category.php?status=<?php echo $menu['cid']; ?>" onclick="return confirm('Activate');"> Deactivate </a>
<?php
}
else if($status == '1')
{
?>
<a href="category.php?status=<?php echo $menu['cid']; ?>" onclick="return confirm('Desactivate');"> Activate </a>
<?php
}
$data .= '<tr>
<td>' . $menu['cid'] . '</td>
<td>' . $menu['cname'] . '</td>
<td>' . $menu['cparent'] . '</td>
<td>' . $menu['clink'] . '</td>
<td>' . $menu['cstatus'] . '</td>
<td>
<button onclick="GetUserDetails(' . $menu['cid'] . ')" class="btn btn-warning">Update</button>
</td>
<td>
<button onclick="DeleteUser(' . $menu['cid'] . ')" class="btn btn-danger">Delete</button>
</td>
</tr>';
}
} else {
$data .= '<tr><td colspan="6">No hay resultado</td></tr>';
}
$data .= '</table>';
echo $data;