-
Posts
2,134 -
Joined
-
Last visited
-
Days Won
42
Everything posted by benanamen
-
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.
-
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);
-
The OP said enough right here In that case, @Jaques1 and I are correct.
-
He doesn't have a list. Somebody lied to him
-
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.
-
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.
-
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(); }
-
Categories and subcategories array and dynamic dropdown list
benanamen replied to z4z07's topic in PHP Coding Help
The fact that you have more than one table says you're wrong, but you know better right? -
Categories and subcategories array and dynamic dropdown list
benanamen replied to z4z07's topic in PHP Coding Help
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/ -
The Bike Shed Problem http://it.toolbox.com/blogs/coneblog/about-project-management-the-bikeshed-problem-32923
-
Suddenly? I have never asked for homework help and I don't violate any of the other rules. What are you trying to say?
-
From the Forums Rules
-
Post the relevant code. We are not going to download an unknown zip file.
-
Implementing multiple incrementing keys in a table
benanamen replied to NotionCommotion's topic in MySQL Help
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. -
Implementing multiple incrementing keys in a table
benanamen replied to NotionCommotion's topic in MySQL Help
@NotionCommotion, I think it's time for an intervention. -
Implementing multiple incrementing keys in a table
benanamen replied to NotionCommotion's topic in MySQL Help
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? -
Implementing multiple incrementing keys in a table
benanamen replied to NotionCommotion's topic in MySQL Help
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. -
Isn't this when you would use a destructor in the class?
-
Implementing multiple incrementing keys in a table
benanamen replied to NotionCommotion's topic in MySQL Help
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? -
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
-
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.
-
How can I display the name of the month that the user requests?
benanamen replied to jaytea41's topic in PHP Coding Help
Is that the best you can do? What basics? What Month? What number? What Array? -
You mean something like this? https://css-tricks.com/examples/DynamicOrderForm/