- This topic is empty.
-
AuthorPosts
-
September 14, 2014 at 3:40 pm #183016chrisburtonParticipant
For a forum, I believe that would be the best approach. And I certainly have to agree with thinking long term about this as it will most likely save him from rewriting. Using a method in which makes sense development wise (
$article->title();
) would be beneficial to a beginner. This is why I love Kirby as that is exactly how it’s structured.September 14, 2014 at 4:33 pm #183021chrisburtonParticipant@traq‘s method allows the same template file to be used across all scripts. Yours does not. Bear in mind that for a beginner, this may not be obvious
While true, I was illustrating a point in which I disagree with using HTML stored in the variable in the case that drose needs to restructure his layout, for example.
I haven’t seen a class appear in any of the code so far. And yet the examples you are using assume class-based data models (as opposed to, for example, arrays).
I was not assuming, I know that he’s not using them. I was illustrating the way in which, a beginner to OOP, like myself, would want the output to be as straightforward and easy to understand as possible. None of my examples would work out of the box.
September 14, 2014 at 6:14 pm #183026__ParticipantJust because I already wrote it, here.
I still feel, as I did weeks ago, that rather than disparate advice you would get the most benefit from a code review (that is, of your whole codebase rather than portions of a handful of scripts).
I agree. I had kinda lost track of this.
traq‘s method allows the same template file to be used across all scripts. Yours does not. Bear in mind that for a beginner, this may not be obvious
While true, I was illustrating a point in which I disagree with using HTML stored in the variable in the case that drose needs to restructure his layout, for example.
I understood that; but it might have escaped drose (who is, after all, the person we are trying to help). I don’t think this was a criticism, per se; but he’s right that we are probably getting ahead of ourselves.
@drose379: I’ll second BenWalker’s reminder. It would be best to have a very firm understanding of the architecture you intend to use, before you decide on how to implement it.September 14, 2014 at 7:44 pm #183027chrisburtonParticipantI understood that; but it might have escaped drose (who is, after all, the person we are trying to help). I don’t think this was a criticism, per se; but he’s right that we are probably getting ahead of ourselves.
Absolutely and my apologies to Ben. My example was to you specifically and not meant for drose to take as a solution. I’m sure it’s a bit confusing now. I was just trying to point out that, from my experience in learning PHP and using Kirby, it’s much much easier keeping the HTML separate from the actual data (text) that you want. For example, when you might want to restructure the layout later on as I’ve pointed out before (I know you understand this now). And while I think your OO method is over my head, personally, I think the way in how the output works makes complete sense and is easier to read.
September 14, 2014 at 8:27 pm #183034__ParticipantPart of the problem there is that this is a very object-oriented idea that I tried to explain in a procedural way. I probably shouldn’t have.
As you say, even though the OO example is more complex, its usage is easier to understand.
September 14, 2014 at 8:52 pm #183036chrisburtonParticipantI think we should also put into perspective his educational level of the front-end. I think that it is quite important that he has fundamental knowledge of basic development and how that works before jumping straight into PHP or even more complex, OO programming. From his previous statements, it doesn’t seem he has that grasped.
As you say, even though the OO example is more complex, its usage is easier to understand.
That’s directly based on my level of PHP. It may make sense to someone like Ben, for instance. There are some things in there that just automatically throw me off and makes me want to fix it (short tags) even though it’s valid. If you look back, I believe I even did this to Ben using an if statement without brackets. When you’re learning PHP it’s just easier to understand when you do things in the way they are shown to you from the beginning.
September 14, 2014 at 9:30 pm #183038__ParticipantThere are some things in there that just automatically throw me off and makes me want to fix it (short tags)
I know exactly what you mean. Brackets, absolutely. I still categorically reject short open (
<?
) tags, but the short echo (<?=
) tag was changed in version 5.4 to be “always on” and safe/standard to use, so I take advantage of that now. (Same with the array literal syntax ([]
); it’s just too good to avoid.) I generally refuse to to use <5.4 for my own work, but I suppose I still need to be careful with my examples, since other people do.September 15, 2014 at 11:51 am #183130__ParticipantThat’s a good approach. You need to pass
$required
into the function (vars outside are not available inside). Also, once you’re done, you need to returntrue
(otherwise, it would be difficult to tell whether everything passed the test or not).function validate_required( $required,$input ){ foreach( $required as $name ){ if( empty( $input[$name] ) ){ return false; } return true; } }
usage:
$required = ['name','email']; $allGood = validate_required( $required,$_POST ); if( $allGood ){ /* good job! */ } else{ /* you forgot some. */ }
You might also be interested in examining how we implemented form validation here.
September 15, 2014 at 12:28 pm #183141__ParticipantOk so if I do a return true at the end of the function, and then put it into use could I do something like…
Yes.
A little confused in your Usage: section on why you passed $_POST into the function?
As a general rule, it is best to pass arguments you need into a function, rather than getting them from the global (or superglobal) scope. It makes it plain, just by looking at the function signature, what is needed for the function to work. It also means that you can be confident that you a working with the data you intended to work with. Some other, more practical benefits in this case:
- you can test your function by passing “dummy” input instead.
- you can accept form submission via other methods (e.g.,
GET
instead ofPOST
). - it is easier to do pre-processing (e.g., parse a JSON submission) on the input.
September 15, 2014 at 1:47 pm #183157__Participantjust wrote something up and I think it would work, but I have not tested it yet, check this out please: http://pastie.org/9556604
Hmm, that doesn’t seem to be working. […] I got this.
Figured out why it didn’t work?
September 15, 2014 at 2:04 pm #183163__ParticipantAny number of possibilities… different PHP versions, different configs, host settings, interactions between existing scripts/code. Since you’re using
$_POST
directly, the only way you can test is to submit the form, so the problem could even be there.What do you mean, specifically, by “it does not work”? what happens? and what does the code look like now?
Yes, I got it all to work
Also, there’s a difference between “getting it to work” and understanding “why it didn’t work.” One matters, and the other doesn’t really. Most people confuse which is which.
September 15, 2014 at 3:07 pm #183172__ParticipantWell for one, for some reason header() functions do not work on my web server, only on my localhost.
This:
header("Location:dashboard.php");
Is incorrect. You need to use a fully qualified URL:
header("Location: http://example.com/dashboard.php");
Ive read online that if the browser receives any HTML tag before the header tag, it will not work, and I do indeed have HTML tags before my header tags on this page…
It’s not “HTML tags,” it’s “any output” (even blank lines, or a BOM if your text editor adds one). Do you understand how HTTP headers work?
September 15, 2014 at 4:17 pm #183176__ParticipantLet us know if you reconsider that code review. : )
September 15, 2014 at 5:19 pm #183184chrisburtonParticipantYep. I wouldn’t list all users to message, however.
September 15, 2014 at 8:17 pm #183203chrisburtonParticipantHopefully with an approve/deny feature.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.