links::nameExists PHP Method

nameExists() public method

public nameExists ( $newName )
    function nameExists($newName)
    {
        require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "settings.class.php";
        $settings = new settings();
        if ($settings::db_driver == "xml") {
            // XML
            $links = simplexml_load_file($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . "links.xml");
            foreach ($links as $link) {
                if ($link->name == $name) {
                    return TRUE;
                }
            }
            return FALSE;
        } else {
            // PDO
            require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
            $common = new common();
            $dbh = $common->pdoOpen();
            $sql = "SELECT COUNT(*) FROM " . $settings::db_prefix . "links WHERE name = :name";
            $sth = $dbh->prepare($sql);
            $sth->bindParam(':name', $name, PDO::PARAM_STR, 100);
            $sth->execute();
            $count = $sth->fetchColumn();
            $sth = NULL;
            $dbh = NULL;
            if ($count > 0) {
                return TRUE;
            }
            return FALSE;
        }
    }

Usage Example

Example #1
0
// Load the require PHP classes.
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "account.class.php";
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "links.class.php";
$common = new common();
$account = new account();
$links = new links();
// Check if the user is logged in.
if (!$account->isAuthenticated()) {
    // The user is not logged in so forward them to the login page.
    header("Location: login.php");
}
$nameExists = FALSE;
if ($common->postBack()) {
    // Check if the name already exists.
    $nameExists = $links->nameExists($_POST['name']);
    if (!$nameExists) {
        // Add this link..
        $links->addLink($_POST['name'], $_POST['address']);
        // Forward the user to the link management index page.
        header("Location: /admin/links/");
    }
}
////////////////
// BEGIN HTML
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "admin" . DIRECTORY_SEPARATOR . "includes" . DIRECTORY_SEPARATOR . "header.inc.php";
?>
            <h1>Links Management</h1>
            <hr />
            <h2>Add Link</h2>
            <form id="add-link" method="post" action="add.php">