Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Try out my first PHP web app!

  • This topic is empty.
Viewing 15 posts - 181 through 195 (of 211 total)
  • Author
    Posts
  • #183016
    chrisburton
    Participant

    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.

    #183021
    chrisburton
    Participant

    @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.

    #183026
    __
    Participant

    Just 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.

    #183027
    chrisburton
    Participant

    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.

    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.

    #183034
    __
    Participant

    Part 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.

    #183036
    chrisburton
    Participant

    I 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.

    #183038
    __
    Participant

    There 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.

    #183130
    __
    Participant

    That’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 return true (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.

    #183141
    __
    Participant

    Ok 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 of POST).
    • it is easier to do pre-processing (e.g., parse a JSON submission) on the input.
    #183157
    __
    Participant

    just 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?

    #183163
    __
    Participant

    Any 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.

    #183172
    __
    Participant

    Well 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?

    #183176
    __
    Participant

    Let us know if you reconsider that code review. : )

    #183184
    chrisburton
    Participant

    Yep. I wouldn’t list all users to message, however.

    #183203
    chrisburton
    Participant

    Hopefully with an approve/deny feature.

Viewing 15 posts - 181 through 195 (of 211 total)
  • The forum ‘Back End’ is closed to new topics and replies.