/** * Gets a connection to the database using the PDO drivers * @return PDO instance of the connection to the database */ public static function DBH() { if (!self::$DBH) { self::$DBH = new PDO("mysql:host=" . config::DBHOST . ";dbname=" . config::DBDATABASE, config::DBUSER, config::DBPASSWORD); } return self::$DBH; }
/** * Allows the user to make changes to an existing vendor * * If data has been posted, an attempt will be made to save the changes to the database. The user * will then be redirected to the vendor view with a notification whether the changes were saved * or not. If no data has been posted, the method will attempt to render a form with the editable * fields for the vendor indicated via ID in the query string. If no ID is given in the query string * or if a vendor matching that ID cannot be found in the database, the user will be redirected to the * vendor index. */ public function actionEdit() { if (isset($_POST['model'])) { $model = vendors::model()->getByPK($_POST['model']['id']); $model->setAttributes($_POST['model']); if ($model->save()) { testProject::setAlert('The changes were saved successfully.', 'success'); } else { testProject::setAlert('We\'re sorry but your changes were not saved. Please try again.', 'danger'); } $this->redirect('vendors/view/' . $model->id); } else { if (isset($_GET['value'])) { $model = vendors::model()->getByPK($_GET['value']); $this->render('edit', array('model' => $model)); } else { $this->redirect('vendors/index'); } } }
* further processing. */ $(document).on("click", ".delete", function(e) { e.preventDefault(); $('#deleteTarget').text($(this).parent().siblings('.itemName').text()); $('.btnConfirmDelete').attr('href', $(this).attr('href')); $('#deleteBox').modal('show'); }); }); </script> </head> <body> <div class="container"> <?php if ($alert = testProject::getAlert()) { ?> <div class="alert alert-<?php echo $alert['class']; ?> "> <button type="button" class="close" data-dismiss="alert">×</button> <?php echo $alert['message']; ?> </div> <?php } ?> <?php echo $content;
/** * Deletes a category * * If no category ID is passed or if it does not match a valid product ID in the database, an error * message is generated. Otherwise the category is deleted and a success message is generated. In either * case the user is redirected to the categories index. */ public function actionDelete() { testProject::setAlert('There was a problem with your request. Please try again.', 'error'); if (isset($_GET['value'])) { $model = categories::model()->getByPK($_GET['value']); $name = $model->name; if ($model->delete()) { testProject::setAlert('The item ' . $name . ' was deleted.', 'info'); } } $this->redirect('categories/index'); }
/** * Returns all the number of matching records found in the database * @param string $query optional formatted SQL query. If omitted, all records from the table will be returned * @param array $params optional array of parameter values to use if the query contains bound parameters * @return integer number of matching records found in the database */ public function getCount($conditions = null, $params = null) { $query = "SELECT COUNT(*) FROM {$this->name}"; if (!empty($conditions)) { $query .= " WHERE {$conditions}"; } $STH = testProject::DBH()->prepare($query); if (!empty($params)) { $STH->execute($params); } else { $STH->execute(); } $result = $STH->fetchColumn(); return $result; }
<?php session_start(); $conf = dirname(__FILE__) . '/conf/conf.php'; require 'core/Application.php'; spl_autoload_register(array('testProject', 'autoload')); testProject::create($conf)->run();
/** * Deletes a product * * If no product ID is passed or if it does not match a valid product ID in the database, an error * message is generated. Otherwise the product is deleted and a success message is generated. In either * case the user is redirected to the products index. */ public function actionDelete() { testProject::setAlert('There was a problem with your request. Please try again.', 'error'); if (isset($_GET['value'])) { //check if ID is provided $model = products::model()->getByPK($_GET['value']); $name = $model->name; if ($model->delete()) { //attempt to delete the item testProject::setAlert('The item ' . $name . ' was deleted.', 'info'); } } $this->redirect('products/index'); }
/** * Returns all the number of matching records found in the database * @param string $query optional formatted SQL query. If omitted, all records from the table will be returned * @param array $params optional array of parameter values to use if the query contains bound parameters * @return integer number of matching records found in the database */ public function getCount($conditions = null, $params = null) { $query = Query::build()->from($this->name)->bind($this->pk, $pk); if ($this->filterByUser) { $query = $query->where('user', '=', Auth::User()->id); } $count = $query->count(); return $count; $query = "SELECT COUNT(*) FROM {$this->name}"; if (!empty($conditions)) { $query .= " WHERE {$conditions}"; } $STH = testProject::DBH()->prepare($query); if (!empty($params)) { $STH->execute($params); } else { $STH->execute(); } $result = $STH->fetchColumn(); return $result; }
public function run($debug = false) { $query = ''; if (count($this->selects) > 0) { //Build a select query $selects = implode(', ', $this->selects); $tables = implode(', ', $this->tables); $query = "SELECT {$selects} FROM {$tables}"; } if (count($this->wheres) > 0) { $wheres = implode(' AND ', $this->wheres); $query .= " WHERE {$wheres}"; } if (count($this->orders) > 0) { $orders = implode(', ', $this->orders); $query .= " ORDER BY {$orders}"; } if ($debug) { var_dump($query); } if ($debug) { var_dump($this->bindings); } $STH = testProject::DBH()->prepare($query); if (count($this->bindings) > 0) { $STH->execute($this->bindings); } else { $STH->execute(); } return $STH; }