Page 3 of 3 FirstFirst 123
Results 21 to 28 of 28

Thread: How to do Mail Merge with php and MySQL

  1. #21
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Daniel: This is great info! Thanks so much for being explicit. Yes, this is how I like to do things too, modularly with a disciplined structure. The site owner is constantly wanting major changes, so it is worth the effort to clean up the code. I've wanted to bail so many times.

    I'm struggling a bit with #2. I looked up _FILE_ and it says... "The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved"

    Is this what you mean? Say I create a file called functions.php that is in the /php directory. Then in my /php/reserve.php file in the <head> section it will say <?php include('functions.php'); ?> and the first lines of the functions.php file will be:

    Code:
    include (_FILE_).'/email.php';
    include (_FILE_).'/arrays.php';
    include (_FILE_).'/users.php';
    Sorry if I am a moron here, but the slash doesn't make sense to me. I would have thought it would be a hyphen. When parsed, does _FILE_ turn into functions.php? Wouldn't it be easier to just write functions.php? This is how I imagine it...

    Code:
    include('functions-email.php');
    include('functions-arrays.php');
    include('functions-users.php');
    Sorry if this went right over my head.

    6. I've never written a function before but am game to try.

    Many thanks, e

  2. #22
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    __FILE__ is a constant that contains the path and filename of the current working file. It will be the filename/path of the file that it is executed in, regardless of whether it is included into another file or it is the main file-- this means that when you include functions.php into any file, you will always have the same path.
    If you do not use it, here is what happens:
    functions.php: include 'numbers.php';
    test/yourfile.php: include '../functions.php';
    Now it will include it and use that line-- it will look for "numbers.php" in the SAME FOLDER as 'yourfile.php', in other words it will be an entirely different place and the file will not exist.
    If you make functions.php like this, it will work:
    function.php: include dirname(__FILE__).'/numbers.php';

    dirname() gets the directory name that includes the path-- this will be:
    [current directory]/....
    So that means we add the slash to then add the filename: numbers.php

    That line of code is equivalent to: [functions folder]/numbers.php.


    You are using only __FILE__, which would be very wrong-- that would do this:
    [folder]/functions.php/numbers.php
    or
    [folder]/functions.php-numbers.php
    or something else that doesn't work.


    6. Functions are easy and very useful once you get to use them. There's plenty of information out there. They can get confusing sometimes when you are doing something complex, but a basic operation function is not too hard to make.
    There are two things to watch out for:
    "return" is how you do something with a function-- it "returns" the value and stops the function.
    Variable scope becomes an issue in functions-- variables outside the function are not available inside, unless they are global or in the function call (like func($a,$b,...)), and variables made within the function are not available outside it.
    Good luck.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #23
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Oh, I think I get what you mean about __FILE__. It preserves the relative position of the included file relative to the file that is including it? The reason I was having trouble was because I always set up an include path in the php.ini file so I never have to even think about the paths... the php.ini takes care of it. That is the way I have done it from Day One. Is there any advantage to using __FILE__ over defining an include path, or can I stay with the way I am used to?

    Taking into account the include path, would what I suggested above work?

    6. I was trying to set up an include for loading the variables from the table and one for replacing the variables for the email but ran into trouble because of the different variable names in different places. It sounds as if a function works for different variables, and I can see how it works in the second case, but for loading the variables, wouldn't it require as many elements as there are fields in the table? ie. function load($v1,$v2,$v3...$v23). Seems a bit messy, or maybe I just messed up what you meant in my head. I need some sleep now. I don't want to exhaust you with my questions. Thank you so much! e

  4. #24
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, if you do setup something in php.ini, that will work as well. My method just requires no setup, since I don't have anything like that setup. In general, includes don't need a path relative to another file, but in the case of this sort of project they do. Go ahead with that method.

    Yes, if you have many variables the function will be very long. There's nothing wrong with this, but it may look a bit messy.
    One problem with functions is that, unless you do a lot of work to get around it, you need to always have the right number of variables in the function call. So if func($a) is valid, func($a,$b) should give you an error. You can allow some variables to be omitted by giving them a default value, so you can do exactly that-- enter one or two, if the second has a default value. But doing that will get very messy with 23 variables.
    But if you do need all 23 of those, that's not really a problem-- the fact that it throws an error may actually help you.
    Regardless, once you get to that many variables, one way around it is to use an array:
    $loadvars = array($a,$b,$c,....,$z);
    load($loadvars);
    (Now, inside the function you'll have a single array with all the parts.)
    But that actually just makes more code for you. In the end, the problem is the number of variables, not the method, really. At some point you're going to have a lot of code.
    Using the array will let you add the parts at various times, though, and you can use named parts rather than just numbers:
    $loadvars = array('name'=>$a,'address'=>$b);
    $loadvars['description'] = $c;
    Then all of those parts will be sent to the function when you send the array as the argument.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (12-26-2009)

  6. #25
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Daniel: As we say in Maui, Mele Kalikimaka! Hope you are having a good day with family and friends.

    It's starting to sound as if it might be easier just to do a search and replace on the odd variables to make them consistent and then use includes. I am very comfortable with includes but know only a bit about arrays. I am happy to learn arrays, but would rather not do it in the middle of trying to solve this problem unless it is the best way. I have to learn Magento right now too so am not getting much sleep. Please tell me which way you would recommend (function, array, include) as the best way to proceed. It's hard for me to make an informed decision when I am not conversant with 2 of the options. I really appreciate your help & guidance. Mahalo plenty! e

  7. #26
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Aloha and happy holidays to you too.

    There is always some logic with just doing what you are familiar with-- there's less chance it will break in a way you can't fix.

    I think it would be possible in this case to use includes.

    Arrays are not needed. If you know what you're doing with arrays, you could save a bit of mess in the way the function looks in the code, but that's about it. You could also setup the function over many lines of code, rather than just doing it in one. Basically, it would make using a function more convenient. It wouldn't be required.

    A function would also be a way to make it easier to use once it's all setup. You don't really need one. There's no absolute benefit to functions, except that to someone who knows what they're doing with functions the method might be a bit cleaner.

    Try to go ahead with includes. If you get it to work, that's all that matters. If you end up with problems, then you may want to try using a function instead.


    The real difference between using includes (or any other methods) and using functions is that functions are more predictable sometimes: includes can get messy, but functions will almost always reliably do the same thing each time. Includes may or may not do exactly the same thing, if they are included in various parts of various pages. Functions are self-contained routines. Includes just grab code and place it into the mess of existing code, so things can get messier there. For example, if you have a variable named $x in the main script and you do an include, then it will conflict with a variable $x in the include. If the variable $x was in a function, there would be no conflict-- functions are basically self-contained.
    Last edited by djr33; 12-26-2009 at 06:20 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (12-26-2009)

  9. #27
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Well I must say you have put my mind at ease. Thank you for that. Now I can go running and relax a bit. I think I understand now about the scope of the variables in functions as opposed to variables in the script. Is it true that declaring variables as global within a function increases their scope to the same level as other variables in the script? If that is true then I can feel much less intimidated by his code now that I can see the different parts to it and what they do.

    One thing that his code doesn't do is prevent invalid dates. People can enter dates like 09-31-2009 or 02-30-2010. Is there an easy way to test for a valid date? Some blank records are ending up in the table and I am wondering if this may be a possible source. I know spam bots can cause this but there has never been a CAPTCHA on the form and it started happening only in the past 6 months after I "improved" some other parts of the code. Have to go run now.. thanks so much! e

  10. #28
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yes, basically. Global variables in a function are elevated to a main variable's level.

    There are ways to limit what dates can be entered. The easiest way is to convert it to a timestamp then have an upper and lower limit on what that can be. There's no default way, so you'll have to code it yourself.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •