Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Everything posted by benanamen

  1. Except for the DSN string, I didn't write the code. It came from this site that is supposed to be teaching patterns http://designpatternsphp.readthedocs.io/en/latest/Structural/DependencyInjection/README.html From what you see on their page, is what they show wrong? Their example of DI was incomplete lacking the actual DSN part so I had to wing it to see what it did so I could understand it. As far as class diagrams, I have spent many days studying this site teaching patterns using Java that shows a diagram for every Pattern https://www.tutorialspoint.com/design_pattern/index.htm One pattern diagram they use makes sense, the builder pattern. Not sure how much is the pattern itself, or the real life example of a Fast food meal. All these shapes and animal examples everywhere just aren't cutting it for me. Is this diagram correct? (Also attached to this post) https://www.tutorialspoint.com/design_pattern/builder_pattern.htm What I need to see is a complete, proper working example, otherwise I am just guessing at how it should be done. It is helpful to hear what is wrong with my attempts but seeing how something should be done would go a lot farther.
  2. I am stuck on the following DI code with the DSN. Relevant lines 62, 77, 78. I expect line 77 to work since the output of line 62 is exactly what works in line 78. I get Fatal error: Uncaught PDOException: could not find driver in C:\Program Files (x86)\Ampps\www\dependencyinjection\DatabaseConnection.php on line 77. If I use line 78 instead, it works. Anyone know what the problem is? $connection var_dump C:\Program Files (x86)\Ampps\www\dependencyinjection\DatabaseConnection.php:75: object(DatabaseConnection)[2] private 'configuration' => object(DatabaseConfiguration)[1] private 'host' => string 'localhost' (length=9) private 'port' => int 3306 private 'username' => string 'root' (length=4) private 'password' => string 'mysql' (length=5) <?php class DatabaseConfiguration { /** * @var string */ private $host; /** * @var int */ private $port; /** * @var string */ private $username; /** * @var string */ private $password; public function __construct(string $host, int $port, string $username, string $password) { $this->host = $host; $this->port = $port; $this->username = $username; $this->password = $password; } public function getHost(): string { return $this->host; } public function getPort(): int { return $this->port; } public function getUsername(): string { return $this->username; } public function getPassword(): string { return $this->password; } } class DatabaseConnection { /** * @var DatabaseConfiguration */ private $configuration; /** * @param DatabaseConfiguration $config */ public function __construct(DatabaseConfiguration $config) { $this->configuration = $config; } public function getDsn(): string { return sprintf( '"mysql:host=%s;dbname=test", %s, %s', $this->configuration->getHost(), $this->configuration->getUsername(), $this->configuration->getPassword() //$this->configuration->getPort() ); } } $config = new DatabaseConfiguration('localhost', 3306, 'root', 'mysql'); $connection = new DatabaseConnection($config); echo $dsn = $connection->getDsn();// "mysql:host=localhost;dbname=test", root, mysql $pdo = new PDO($dsn);// Doesnt Work //$pdo = new PDO("mysql:host=localhost;dbname=test", root, mysql); //Works $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM users"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); echo '<pre>'; print_r($result);
  3. The OP said enough right here In that case, @Jaques1 and I are correct.
  4. What @Jaques1 showed you is the way to do it. You are wanting to know if there are results returned or not, true or false. How many are returned if any is irrelevant.
  5. It doesn't matter. The code has been completely removed from Php. Don't use it. At the least, you should be using Mysqli
  6. You cannot just through an i on the end of the deprecated functions. Take some time and read the manual. You should be using PDO anyways. https://phpdelusions.net/pdo
  7. You need to create a unique constraint on whatever identifies the users. They should not be allowed to sign up more than once with the same parameters.
  8. I ran across the following example of a Factory Pattern. My question is, why would you have all this Factory code when you could just call one of the extended button classes that you need? <?php abstract class Button { protected $_html; public function getHtml() { return $this->_html; } } class ImageButton extends Button { protected $_html = "..."; //This should be whatever HTML you want for your image-based button } class InputButton extends Button { protected $_html = "..."; //This should be whatever HTML you want for your normal button (<input type="button"... />); } class FlashButton extends Button { protected $_html = "..."; //This should be whatever HTML you want for your flash-based button } class ButtonFactory { public static function createButton($type) { $baseClass = 'Button'; $targetClass = ucfirst($type).$baseClass; if (class_exists($targetClass) && is_subclass_of($targetClass, $baseClass)) { return new $targetClass; } else { throw new Exception("The button type '$type' is not recognized."); } } } $buttons = array('image','input','flash'); foreach($buttons as $b) { echo ButtonFactory::createButton($b)->getHtml(); }
  9. The fact that you have more than one table says you're wrong, but you know better right?
  10. You can start with a proper database design. There is no such thing as sub categories. They are ALL categories. Some are Parents, some are Children. Look this over. http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
  11. The Bike Shed Problem http://it.toolbox.com/blogs/coneblog/about-project-management-the-bikeshed-problem-32923
  12. Suddenly? I have never asked for homework help and I don't violate any of the other rules. What are you trying to say?
  13. From the Forums Rules
  14. Post the relevant code. We are not going to download an unknown zip file.
  15. LOL! While I never did anything about it, in my early days missing numbers really bothered me. Notion, in a good way. We here need to gather to save you from yourself.
  16. @NotionCommotion, I think it's time for an intervention.
  17. I don't have time at the moment to get into the implementation. I am sure Barand will jump in before I can. As far as MyISAM, what is stopping you from using InnoDB?
  18. Without the exact detail on the public_id I cannot make any comments on that part. You are on the right track here. Is this the part you do not understand how to do? On a side note, MongoDB is very well suited to this type of data.
  19. Isn't this when you would use a destructor in the class?
  20. Why are you creating multiple tables? You should have ONE animals table linked to an animal_type_id in a animal type table. What is with the multiple keys?
  21. Your code is obsolete and vulnerable to exploits and has been completely removed from Php. You need to use PDO with prepared statements. https://phpdelusions.net/pdo
  22. Your approach is wrong in the first place. You never ever put variables in the query. You need to use prepared statements and PDO. https://phpdelusions.net/pdo Also, if you have 42 columns in a single table it is highly likely your DB design is wrong. A DB is not a spreadsheet. Look up and learn Database normalization.
  23. Is that the best you can do? What basics? What Month? What number? What Array?
  24. You mean something like this? https://css-tricks.com/examples/DynamicOrderForm/
×
×
  • 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.