Jump to content

AndrewPHP

New Members
  • Posts

    4
  • Joined

  • Last visited

Everything posted by AndrewPHP

  1. Great stuff, I think I'm slowly taking it in (I hope), so how's this... <?php class ScriptControl { private $pdo; private $ScriptName; public function __construct($pdo, $ScriptName) { $this->pdo = $pdo; $this->ScriptName = $ScriptName; } public function IsEnabled(); { $sql = "SELECT 1 FROM config WHERE config_name = ? AND config_value = 1 LIMIT 1"; $stmt = $this->pdo->prepare($sql); $stmt->execute([$this->ScriptName]); return $stmt->fetchColumn(); } public function IsRunnable(); { date_default_timezone_set("Europe/London"); $time = date("Hi"); $sql = "SELECT 1 FROM config WHERE config_name = ? AND config_value = ? LIMIT 1"; $stmt = $this->pdo->prepare($sql); $stmt->execute([$this->ScriptName, $this->$time]); return $stmt->fetchColumn(); } } ?> <?php include_once('PDO.class.php'); include_once('ScriptControl.class.php'); $ThisScript = new ScriptControl('SendMessage'); if ( (!$ThisScript->isEnabled()) && (!isset($_POST['bypass'])) ) { echo "<p>This script is not enabled</p>\n"; die(); } if ( (!$ThisScript->isRunable()) && (!isset($_POST['bypass'])) ) { echo "<p>This script is not scheduled to run</p>\n"; die(); } ?>
  2. Thanks for that, it looks like I should have stuck to the procedural side of things as this looks incredibly more complex. I also need to create some other functions too but unsure how they would break down by class. CheckIfEnabled - This function checks in the DB if this script is allowed to be run. CheckIfTime - This function checks in the DB if this script is allowed to be run at this current minute. CheckMessagesSent - This function checks in the DB to see how many messages have been sent already today CheckMessageLimit - This function checks in the DB to see how many messages are allowed to be sent. Would I create a new class for each function?
  3. I found procedural easier
  4. After writing procedural style PHP for many years I've decided to give OOP & PDO a go (and I'm hating it already). I have written a simple function which validate a flag value, and another function which is later used to get the description of that value. Am I on the right lines here and can any improvements be made? <?php // Connect to MySQL $dsn = 'mysql:dbname=dev_pof;host=127.0.0.1'; $username = 'dev'; $password = 'dev.dev'; $pdo = new PDO($dsn, $username, $password,[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); // Function to validate the flag value function CheckFlagValue($pdo, $FlagName, $FlagValue) { $sql = "SELECT 1 FROM flags WHERE flagname = ? AND flagvalue = ? LIMIT 1"; $stmt = $pdo->prepare($sql); $stmt->execute([$FlagName, $FlagValue]); return $stmt->fetchColumn(); } // Function to get the flag description function GetFlagDescription($pdo, $FlagName, $FlagValue) { $sql = "SELECT flagdescription FROM flags WHERE flagname = ? AND flagvalue = ? LIMIT 1"; $stmt = $pdo->prepare($sql); $stmt->execute([$FlagName, $FlagValue]); return $stmt->fetchColumn(); } // Example of using the CheckFlagValue function if (CheckFlagValue($pdo, 'intent', 2)) { echo "valid"; } else { echo "invalid"; } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.