Johan Broddfelt
/* Comments on code */
Donate $

Delete post and Generic Class

If you look at the list.php we have now built the edit form. But there is also a delete link in the form, that we have not implemented yet. There are two main ways to treat a deletion. The first one is to just run a DELETE post WHERE id=$id, and then the post is gone. This is the simplest one and in most cases also the most logical one to use. But when it comes to posts and comments, as well as users for that matter. We do not really want to delete those. What I use to do is add a column in the table called is_deleted. And in all my lists I make sure to include the criteria, WHERE is_deleted=0 to make sure that it is not shown anymore. The reason for this is that you want to be able to restore what your users have written and deleted. Especially if it turns out to be a threat of some kind that could be used as evidence in court, perhaps the deletion was a mistake and the user want to restore it. It might also be useful for retaining relationships between tables.
But in the generic list i just want a regular delete and therefor we are going to add the following code to our generic/list.php

<?php
    if (filter_input(INPUT_GET, 'cmd') == 'del') {
        if (filter_input(INPUT_GET, 'ok') == '') {
            ?>
            <div class="warning">
                <h2>Delete <?php echo $obj->realClassName(); ?></h2>
                Are you sure you want to delete this entry?<br><br>
                <a href="?module=<?php echo $module; ?>&view=list&id=<?php echo $id; ?>&cmd=del&ok=yes" class="button">Yes</a>
                <a href="?module=<?php echo $module; ?>&view=list" class="button">No</a>
            </div>
            <?php
            return false;
        } else if (filter_input(INPUT_GET, 'ok') == 'yes') {
            $obj->delete();
            ?>
            <div class="warning">
                The <?php echo $obj->realClassName(); ?> entry has been deleted.
            </div>
            <?php
        } 
    }

Now we also need to create the delete() function in Db.php.

    function delete() {
        $sql = 'DELETE FROM ' . $this->table . ' WHERE id=' . (int)$this->id;
        Db::query($sql);
    }

And of course we are going to add this to the list-generator as well.

<?php
    if (filter_input(INPUT_GET, 'cmd') == 'del') {
        if (filter_input(INPUT_GET, 'ok') == '') {
            ?>
            <div class="warning">
                <h2>Delete ' . $obj->realClassName() . '</h2>
                Are you sure you want to delete this entry?<br><br>
                <a href="?module=<?php echo $module; ?>&view=list&id=<?php echo $id; ?>&cmd=del&ok=yes" class="button">Yes</a>
                <a href="?module=<?php echo $module; ?>&view=list" class="button">No</a>
            </div>
            <?php
            return false;
        } else if (filter_input(INPUT_GET, 'ok') == 'yes') {
            $obj->delete();
            ?>
            <div class="warning">
                The ' . $obj->realClassName() . ' entry has been deleted.
            </div>
            <?php
        } 
    }

That's it. Now all you have to do is to add all the tables you need to the database and the application will take care of it self... almost we still need to add those class files. Why don't we make a Generic class and be done with it. Here is the class at classes/Generic.php.

<?php
class Generic extends Db {
    public $table = 'generic';
    
    function __construct($input=false, $fields=' * ') {
        $module = filter_input(INPUT_GET, 'module', FILTER_SANITIZE_URL);
        $this->table = $module;
        parent::__construct($input, $fields);
    }
}

And we use this class in the index.php as follows:

    if ($module != '') {
        $db = new Db();
    	$className = $db->className($module);
        if (is_file('classes/' . $className . '.php')) {
            $obj = new $className((int)$id);
        } else if ($module != '') {
            $obj = new Generic((int)$id);
        }
    }

Now we actually do not need to write a single line of code, and still be able to build a quite large application that can handle a large set of relations and datasets. Just make sure you stay to the rules of the database. the first column should be named id and contain a auto increment number as primary key, related tables should be named the same as the table and end with _id, checkboxes should start with is_ and date fields should be set as type date in the database. Then your good to go.

But we are not done yet, we have lots of ground to cover. For starters, date-pickers for our date fields, user, unit test, error logging, comments and a lot more. I have a stack of fun stuff I'm planning to give to you on this site and I'll come up with some new stuff as well as we go along. So stay tuned. Hopefully I'll also make an e-mail list and an rss-feed for you to join.

- Framework, PHP

<< Generating the edit form Security is important >>

Comment

Name
Mail (Not public)
Send mail uppdates on new comments

Comments

0 post found