Jump to content

jbonnett

Members
  • Content count

    91
  • Joined

  • Last visited

Community Reputation

1 Neutral

About jbonnett

  • Rank
    Regular Member
  • Birthday 09/01/1992

Profile Information

  • Gender
    Male
  • Location
    Aberystwyth, Wales
  1. jbonnett

    Custom ID Generator PDO

    There is a unique constraint on the column, transactions should work due to ACID. Also MySQL doesn't natively support base64.
  2. jbonnett

    Custom ID Generator PDO

    If you are wondering why I wanted to change to a non numerical system take a look at this: https://www.youtube.com/watch?v=gocwRvLhDf8 The video explains how it could be a security risk and how you can't have as many entries as the following. I also figured out how to do it with transactions, if you would like to know how I managed it for your future code here you go: try { $pdo->beginTransaction(); // start a transaction // create and execute query to see if the id exists (like the loop in my first post) // Add something to the database using the new id $pdo->commit(); // make the changes } catch (Exception $e) { $pdo->rollback(); // if it fails you can just take the database to the previous state }
  3. jbonnett

    Custom ID Generator PDO

    Thank you for the replies, I don't want a numerical id, also that loop will only keep looping if the id already exists. E.g. if it already exists generate another id. The generating function will eventually become custom, this is just for testing purposes. I realise the more rows the more likely it is to loop, although this is not the final code. Also you mention that it's a bad idea to create your own... My University seems to think not, as there are more combinations to be had with custom generators (if done the right way). They even use one themselfs. And knowing this is why I mentioned transactions as transactions allow you to work with existing data in a powerful way. Not meaning to come across as ungrateful, although I'm looking for this specifically and not the easiest way of achieving the somewhat same result.
  4. jbonnett

    Custom ID Generator PDO

    Hi All, I was wondering if there was a way that I could check if an ID already exists if not reserve it for the final query? Maybe with some sort of transaction based query. So if I were to do this: $exists = 1; while($exists > 0) { $new_id = uniqid(); $exists = $database->prepare('SELECT COUNT(*) FROM `Employees` WHERE `EmployeeID` = :EmployeeID'); $exists->execute([":EmployeeID" => $new_id]); $exists = $exists->rowCount(); } Along with: $query = $database->prepare(" INSERT INTO `Employees` ( `EmployeeID`, `Username`, `Password`, `FirstName`, `LastName`, `Email`, `Timestamp`, `Roles_Role` ) VALUES ( :EmployeeID, :Username, :Password, :FirstName, :LastName, :Email, :Timestamp, :Roles_Role ) "); $query->execute([ ":EmployeeID" => $new_id, ":Username" => $this->Username, ":Password" => $this->Password, ":FirstName" => $this->FirstName, ":LastName" => $this->LastName, ":Email" => $this->Email, ":Timestamp" => $this->Timestmap, ":Roles_Role" => $this->Roles_Role ]); $query->execute(); I may get an error where if I check if the new custom ID exists, if it doesn't then the next query may fail to duplicates. Thank you for any help in advance.
  5. jbonnett

    Check if argument exists?

    I'm confused could you provide me with a more clear example? I'm thinking something more along the lines of: http://stackoverflow.com/questions/1038435/sql-conditional-select As I'm using PDO to Bind the value, but rather than making separate SELECT statements and all results (that may overlap all results from each into one, which would render it useless).
  6. jbonnett

    Check if argument exists?

    There should be a way of doing this in SQL though, if I do it that way I'll have to check every combination.... e.g. if all exist, if only one exists, if only 2 exists .etc
  7. jbonnett

    Check if argument exists?

    Hi All, I'm trying to check if an argument exists or is not null in postgres, rather than making an if / else statement for every single combination the arguments can be... So here's an example of what I'm trying to accomplish, I have an index page that can search by a text query, dropdown box of platform and a dropdown box of genre. That means I can have a combination of 6 different if / elses, if I added more well you get the idea that can be a lot of code. The simplest alternative I could come up with is getting all results from the database and excluding them all in the loop like so: // searching for a specific title if(isset($_GET["q"]) && $_GET["q"] !== "") { if(strpos(strtolower($game->title()),strtolower($_GET["q"])) === false) { continue; } } // searching by platform if(isset($_GET["platform"]) && $_GET["platform"] !== "all") { if(strpos(strtolower($game->platform()),strtolower($_GET["platform"])) === false) { continue; } } // searching by genre if(isset($_GET["genre"]) && $_GET["genre"] !== "all") { if(strpos(strtolower($game->genre()),strtolower($_GET["genre"])) === false) { continue; } } I have tried the SQL "SELECT * FROM videogames WHERE IF (:string IS NOT NULL) title ILIKE :string AND IF (:platform IS NOT NULL) platform = :platform AND IF (:genre IS NOT NULL) genre = :genre". I don't think I've done the if statement correctly in SQL either. Each of these arguments can exist or not. If you can think of a simple way to accomplish this please let me know.
  8. jbonnett

    Using ajax to retain login?

    Hi All, I'm trying to use ajax to essentially get a page without changing anything already on the current page, so a modal shows after 29 mins and gives me the option to stop the user from logging out by clicking a button. The php session controls the logout by checking the session timestamp vs the one in the database, if the session one is older than 30 mins with no activity e.g. viewing a page... then logout. The problem is to "view" a page I use an ajax call (although it still logs the user out): $.ajax({ cache: false, type: "GET", dataType: 'html', url: "<?=$config["pre_link"].$_SERVER["REQUEST_URI"]?>", success: function(data) { }, error: function(){ }, complete: function(){ } }); Here is the whole thing if you need it: $(document).ready(function() { var timeout = setTimeout(function() { $("#logout_warning").modal('show'); }, 29*60*1000); var end = new Date().getTime(); function resetTimeout() { clearTimeout(timeout); timeout = setTimeout(function() { $("#logout_warning").modal('show'); }, 29*60*1000); end = new Date().getTime(); } $("#stop_logout").click(function() { resetTimeout(); $("#logout_warning").modal('hide'); $("#logout_warning .modal-content").css("display", "none"); $.ajax({ cache: false, type: "GET", dataType: 'html', url: "<?=$config["pre_link"].$_SERVER["REQUEST_URI"]?>", success: function(data) { }, error: function(){ }, complete: function(){ } }); }); $("#stop_logout .close").click(function() { $("#logout_warning").modal('hide'); $("#logout_warning .modal-content").css("display", "none"); }); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showRemaining() { var now = new Date().getTime(); var distance = ((end+30*60*1000) - now) - 1000; if (distance < 0) { clearInterval(timer); window.location.reload(true); return; } var days = Math.floor(distance / _day); var hours = Math.floor((distance % _day) / _hour); var minutes = Math.floor((distance % _hour) / _minute); var seconds = Math.floor((distance % _minute) / _second); //$("#logout_mins").html(minutes); $("#logout_secs").html(seconds); } timer = setInterval(showRemaining, 1000); });
  9. jbonnett

    rawurldecode() not decoding %20 as white space

    Nevermind I found the problem: private function parseURL() { if(isset($_GET["url"])) { return explode("/", filter_var(rtrim($_GET["url"], "/"), FILTER_SANITIZE_URL)); } } I fixed it by: private function parseURL() { if(isset($_GET["url"])) { return explode("/", str_replace('\/', ' ', filter_var(rtrim(str_replace(' ', '\/', $_GET["url"]), "/"), FILTER_SANITIZE_URL))); } } Do you have any ideas to make this better? Maybe a different character to make the spaces?
  10. jbonnett

    rawurldecode() not decoding %20 as white space

    If I don't encode the URL or decode it e.g. keep the white space in the URL, it works fine. I'm have my own MVC, I'm retrieving the URI via $_GET request forwarded to a function using call_user_func_array(), so I then do something like this: some_function($get = "") { if($get != "") { echo rawurldecode($get); } }
  11. Hey all, My URL is being sent as http://localhost/videos/On%20the%20go although if I use rawurldecode("On%20the%20go") I get "Onthego" rather than "On the go"... Any ideas? This should work, I've also tried urldecode() as it's a similar method but had no luck.
  12. jbonnett

    Accessing global in constructor or parent class

    Already tried, not showing nothing... I can tell you that it's with the core class specifically the constructor, it's almost like the constructor is running twice or the if statement inside the constructor is true both sides of the condition... Note I'm only calling the class once. public function __construct($subdir = "") { if($subdir != "") { $this->root = $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . $subdir . DIRECTORY_SEPARATOR; } else { $this->root = $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR; } echo $this->root . "<br/>"; $this->rootds = DIRECTORY_SEPARATOR; $this->loadCoreClasses(); }
  13. jbonnett

    Accessing global in constructor or parent class

    I did check, can't find anything... And please, I have a general idea in my head... Which I'm trying to implement with TDD, don't necessarily need to design as I'm using MVC, which is a "design" pattern, I need to implement that first then somewhat design the rest when I have the MVC in place. No need to argue the point.
  14. jbonnett

    Accessing global in constructor or parent class

    I ended up doing this, I don't know if its the right thing to do... But I can tell you that I keep exceeding the memory limit, I have tried editing the ini for more memory, but it's like it just needs more and more... <?php /** * Created by PhpStorm. * User: Jamie * Date: 27-Jun-15 * Time: 10:44 AM */ class Init { private $subdir; public function __construct($subdir = "") { if($subdir != "") { $this->subdir = $subdir; } set_error_handler(array($this, "ErrorHandler")); $this->loadCore(); } public function ErrorHandler($errno, $errstr, $errfile, $errline) { echo "<b>Custom Error:</b> [" . $errno . "] " . $errstr . "<br />"; echo " Error on line " . $errline . " in line " . $errfile . "."; } public function logHandler() { $development = true; if($development == true) { error_reporting(E_ALL); ini_set("display_errors", "On"); } else { error_reporting(E_ALL); ini_set("display_errors", "Off"); ini_set("log_errors", "On"); if (isset($this->subdirectory)) { ini_set("error_log", $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . $this->subdir . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Tmp" . DIRECTORY_SEPARATOR . "Logs" . DIRECTORY_SEPARATOR . "Error.log"); } else { ini_set("error_log", $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Tmp" . DIRECTORY_SEPARATOR . "Logs" . DIRECTORY_SEPARATOR . "Error.log"); } } } private function loadCore() { if($this->subdir != "") { if (file_exists($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . $this->subdir . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Core" . DIRECTORY_SEPARATOR . "Core.php")) { require_once($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . $this->subdir . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Core" . DIRECTORY_SEPARATOR . "Core.php"); new Core($this->subdir); } } else { if (file_exists($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Core" . DIRECTORY_SEPARATOR . "Core.php")) { require_once($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Core" . DIRECTORY_SEPARATOR . "Core.php"); new Core(); } } } }; ?> <?php /** * Created by PhpStorm. * User: Jamie * Date: 29-Jun-15 * Time: 5:42 AM */ class Core { protected $root; protected $rootds; protected $core = array(); public function __construct($subdir = "") { if($subdir != "") { $this->root = $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . $subdir . DIRECTORY_SEPARATOR; } else { $this->root = $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR; } $this->rootds = DIRECTORY_SEPARATOR; $this->loadCoreClasses(); } private function loadCoreClasses() { $classes = array("Request", "Redirect", "Session", "Controller", "Router"); for($i = 0; $i < count($classes); $i++) { if(file_exists($this->root . "Private" . $this->rootds . "Core" . $this->rootds . $classes[$i] . ".php")) { require_once($this->root . "Private" . $this->rootds . "Core" . $this->rootds . $classes[$i] . ".php"); if($classes[$i] != "Router" || $classes[$i] != "Controller") { $this->core[$classes[$i]] = new $classes[$i]; } } else { trigger_error($classes[$i] . " Class missing."); exit(); } } } }; ?> <?php /** * Created by PhpStorm. * User: Jamie * Date: 27-Jun-15 * Time: 11:14 AM */ class Router { protected $controller = "Home"; protected $method = "index"; protected $perams = array(); public function __construct($init = Null) { $url = $this->parseURL(); if(file_exists($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Controllers" . DIRECTORY_SEPARATOR . $url[0] . ".php")) { $this->controller = $url[0]; unset($url[0]); } require_once($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "Private" . DIRECTORY_SEPARATOR . "Controllers" . DIRECTORY_SEPARATOR . $this->controller . ".php"); $this->controller = new $this->controller; if(isset($url[1])) { if(method_exists($this->controller, $url[1])) { $this->method = $url[1]; unset($url[1]); } } $this->perams = $url ? array_values($url) : array(); call_user_func_array(array($this->controller, $this->method), $this->perams); } private function parseURL() { if(isset($_GET["url"])) { return explode("/", filter_var(rtrim($_GET["url"], "/"), FILTER_SANITIZE_URL)); } } }; ?>
  15. jbonnett

    Accessing global in constructor or parent class

    I want to be able to call init though... Init will end up with a lot more stuff yet.
×

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.