-
Posts
1,713 -
Joined
-
Last visited
-
Days Won
54
Everything posted by maxxd
-
mac_gyver is 100% correct with those suggestions. I will add that using '?' placeholders can get confusing if you've got several to many variables in your query - in this case i recommend named placeholders. So, to update mac_gyver's perfectly good code as an example, $sql = "Select * FROM weekends WHERE Weekend_Number = :weekendNumber AND Men_Women = :menWomen"; $stmt = $pdo->prepare($sql); $stmt->execute([ 'weekendNumber' => $_SESSION['Weekend_Number'], 'menWomen' => $_SESSION['Men_Women'] ]); Note that another benefit of using PDO over mysqli is that you don't have to bind the parameters separately. It's been a while since I used mysqli, but i think i remember having to bind the result values as well? If I'm remembering correctly, this is another thing you don't have to do with PDO.
-
There are issues here beyond the error you're seeing. First and foremost, drop mysqli_* and use PDO - it's easier to use and can handle several different SQL dialects. Secondly, never put raw user data into a query (session data can be modified by the user). Use prepared statements in order to not lose or expose your data. As to the actual issue you're seeing, print out the value of $Number and $MW before you run the query to make sure they contain what you think they contain. If the value is actually '55th' you need quotes around the value - another bonus of using prepared statements (preparing the statement will take care of that for you).
-
In any code that I work with that doesn't use a templating engine I try to label my conditionals, like so: <div class="slew-of-form-elements"> <?php if($myVar === true): ?> <input type="text" name="field_1"> <select name="field_2"> <option value="-1">Select an option</option> <?php foreach($options as $key=>$value): ?> <option value="<?= $key; ?>"><?= $value; ?></option> <?php endforeach; // $options as $option ?> </select> <?php endif; // $myVar === true ?> </div> Obviously this is a contrived example and is missing things like output sanitization and the many, many, many form elements some of the code deals with but hopefully the point comes across.
-
Class methods do not have to match the constructor's signature - each method can require as many or as few properties as it needs. For instance, given your example, this is perfectly acceptable: <?php class FeatureRequests { private $details = []; public function __construct(array $details) { $this->details = $details; } public function getAllDetails() { return $this->details; } public function getDetail($index) { if(isset($this->details[$detail])){ return $this->details[$detail]; }else{ return 'Index not set'; } } } In this case, the constructor requires an array, getDetail() requires a string, and getAllDetails() doesn't require any parameters. What matter is that when you invoke the object method, you pass it the parameters it needs.
-
What am I doing wrong with this 'php include' coding?
maxxd replied to Ocean_Voyager's topic in PHP Coding Help
/homepages/29/d1007800584/htdocs is your site root - this is set by your host at the server level. If site-header.php and tester-include.php are both in the /page/ directory, the include from tester-include.php should be simply `require_once('./site-header.php');` If that still doesn't work, try `require_once(dirname(__FILE__).'/site-header.php');` (if site-header.php is in a sub-directory called /root/, add that into the require). As an aside, your host should be on php 8.1 at the least - 7.4 reached end of life in November 2022. You may want to ask them why they haven't upgraded yet. If they can't give you a decent answer, there are plenty of low-cost modern hosts around - ask here. Someone can point you in the right direction. -
Saw that earlier but it's possible I wasn't in the mindset to read it completely - I'll take another look. Thanks much!
-
I have a method that is trying to recognize legitimate names from data submitted by the user. I've got an array of obscene words, an array of bullshit words I've seen in past submissions, an array of just random strings that I've culled over time that I check against. I'm looking now at using AWS Comprehend DetectEntities as a last resort and it works - I hate to admit this - very well. The problem is that there is a phpunit group of tests that passed before the new code needed the actual AWS Comprehend call credentials to test. I'm having issues mocking the ComprehendClient object. $c = $this->getMockBuilder(ComprehendClient::class) ->setConstructorArgs([ 'args' => [], ]) ->addMethods(['detectEntities']) ->getMock(); $c->expects(self::once()) ->method('detectEntities') ->with([ 'LanguageCode' => 'en', 'Text' => 'string' ]) ->willReturn(true); $this->assertTrue(ComprehendController::validateName('Bob')); returns The service "ockobject_comprehendclient_a8" is not provided by the AWS SDK for PHP. And $cce = new class extends ComprehendClient { public function __construct(){ parent::__construct([ 'credentials' => [], 'region' => 'us-west-2', 'version' => 'latest' ]); } public function detectEntities($args = []) { return true; } }; $c = $this->getMockBuilder($cce::class) ->setConstructorArgs([ 'args' => [], ]) ->setMockClassName(ComprehendClient::class) ->addMethods(['detectEntities']) ->getMock(); $c->expects(self::once()) ->method('detectEntities') ->with([ 'LanguageCode' => 'en', 'Text' => 'string' ]) ->willReturn(true); $this->assertTrue(ComprehendController::validateName('Bob')); returns The service "comprehendclient@anonymous\/home/my-dir/project/tests/unit/testcomprehend.php" is not provided by the AWS SDK for PHP. Can anyone point me to a decent tutorial on how the hell to mock an aws object in phpunit? It's kinda driving me nuts...
-
First and foremost, the mysql_* functions were removed from PHP quite some time ago. Look into PDO for all your database needs. That having been said, a lot of old code still exists on the internet, so don't blindly copy and paste. The good aspect of the code you're using is that it's using PHPMailer instead of php's native mail function. Not to be rude, but the rest of it is dusty and shouldn't be used.
-
Yeah, honestly forget that MySQLi even exists - PDO is better and much easier.
-
If you have added the connection code, check your browser tools network tab to see if something is throwing an error. Just a couple weeks ago I spent several hours debugging a script that was barfing and come to find out I had typed 'context' instead of 'content' in a column name.
-
CodeIgniter vs. Laravel for Dating Script Development
maxxd replied to LaraDavies's topic in Frameworks
CodeIgniter 4 is leaps and bounds better than 3 - it was re-written from scratch to make use of modern development patterns. That being said, as gizmola mentioned it was still pretty bare-bones last time I used it, whereas Laravel is fully featured. Some may say too fully featured, but it's optimized very well and has a large and active ecosystem. For instance, if you don't want to pay for Pusher they just released Reverb, which is a Laravel-specific websocket package that isn't the easiest thing to set up, but once you do it's quite good. It has a robust queue system that allows the developer to do a lot of work asynchronously right out of the box. The learning curve is steep, but most frameworks have that problem. Just know going in that there is a lot of magic to Laravel, so making sure you're comfortable reading source code is important (although the docs are better than most, IMO). I've not used Symfony specifically but - again as gizmola says - Laravel is built heavily on Symfony components. Best bet is to try building something smallish in all three and see which sings to you. Personally, even recognizing that Laravel has plenty of warts I prefer to work in it (though that may be Stockholm Syndrome as I've been working with it almost exclusively for about 6 years now). -
I agree - only use JavaScript when it makes sense to use it; there's no need to invent a reason. If you're using a SPA framework (Vue, React, whatever is hot this week in JS land) then you've got a reason to use JS to redirect the browser. At the same point, those frameworks have the foundation in place that will handle redirects in the background.
-
Php and Laravel prevent duplicate entries help please
maxxd replied to PNewCode's topic in PHP Coding Help
I get the username -> email thing; it's one of those wonderful side effects of tech debt and the mitigation thereof. As far as the Laravelese, yeah - laravel is a very opinionated framework. What you can easily do for the validation on username/email is this: https://laravel.com/docs/11.x/validation#rule-unique So if I'm not mistaken (and it's late and I've had some wine so I may very well be mistaken) you can use 'username' => ['required', 'string', 'max:255', 'alpha_num', 'unique:App\Models\User,email'], PS - I'm not for hire, but if your timeline is very slow I'd be glad to help out when I've got time. Post here or DM me, but I can't guarantee I'll be able to respond quickly. -
I also use intelephense in VSCode and I don't see it like it's shown in phpstorm. I have to admit i very much prefer popup on hover over inline.
-
That's a default feature of PHPStorm, at least. VSCode doesn't do it by default but I suspect there are plugins that will add the functionality.
-
Then it seems like an issue with the plugin - your best bet is to reach out to the developer about it.
-
Admittedly I'm not a server admin by any stretch of the imagination, but I guess it could be if you set up the right user/group permissions on the files. I will say - to me - it seems a bit more convoluted than actually just moving the files - a lot of modern IDEs will automatically update any include/include_once/require/require_once statements for you when you move a directory. Hopefully any of the multitude of people on the board that are better with server setup than I will weigh in with a more grounded opinion.
-
Outside the webroot - whether that's called www/ or http/. The whole point is that you don't want the casual browser to be able to access the files directly, only php can access them. So from within the webroot you'll use something along the lines of include_once('../includes/myPhpFile.php'); The contents of myPhpFile are now accessible from the calling script.
-
Adding data to an OpenOffice document from php
maxxd replied to ManageMyMoves's topic in PHP Coding Help
Check out https://github.com/PHPOffice/PHPWord. It's not automatic - you'll still need to code, but it's a library to make it easier to do. -
Don't forget you'll be sacrificing image quality if you upscale the images. This might not be an issue, but every b2c catalog site I've built has wanted good, crisp photos of their products.
-
Forgot about how WP metadata works - ignore me.
-
I like to think of a framework as literally that - a frame you use to build your project on. So instead of having to write your database connection, display, and interconnecting code for each project you can concentrate on the actual functional logic for that specific project. Use the framework-provided database connection to get the data, write the templates to display the data (but don't worry about actually rendering the templates), and auto load the extra files you'll need to process the data the user sends to you, adjust and amend it as necessary, and send that data to the template that will render it. Basically, frameworks at their heart do the boring and repetitive stuff for you. Beyond that I think there's been a slight misunderstanding when you're talking about "controls". Most modern frameworks (Laravel, CodeIgniter, and CakePHP most notably) are based on the MVC design pattern wherein there are Models, Views, and Controllers. Controllers basically ingest the data the user submits, retrieves any additional data your system needs from the Models (and/or saves data via the Models), works on that data as necessary, and then sends that resulting data to be rendered in the View. So think of it like Models interact directly with the database, Views interact directly with the browser, and Controllers dictate the actual function of your system by interacting with the Models and Views.