Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2002
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question user defined functions (how to?)

    Hi!

    I tried making a user defined function that would send e-mail when called but couldn't get it to work. I tried adding it into a existing shopping cart script.

    Feel free to laught (i tried to rip-off the syntax from the other functions in the script... but it did't work)

    Example 1

    function check_out(){
    mail("someone@someone.com","This is a Test","Text Goes Here" );
    }

    i tried calling it with html link http://www.domain.com/index.php?checkout=1

    Example 2

    This one i tried calling with http://www.domain.com/index.php?check=postit

    function check_out ($check){

    if ( $check =="postit" )
    {
    $to = "someone@someone.com";
    $subject = "test";
    $message="only a test $amount $name $price";
    mail ($to, $subject, $message ) or print "couldn't send e-mail";
    }
    }

    I'm not sure which part is wrong. The link or the "script" or both.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i think your a bit confused. userdefined functions work just like system ones, as in
    1) they have to be declared before they are used
    2) they then have to be called, otherwise they wront do anything for example, your mail function
    PHP Code:
    function check_out()
    {
    mail("someone@someone.com","This is a Test","Text Goes Here" );
    }

    // other 
    // code
    // goes
    // here

    //now, say you want to send your email
    check_out(); 
    what you were doing before was setting a $_GET[] variable. the $_GET[] array lists all the variables set in the url.

    look at www.hotscripts.com i think they have some tuts on user defind functions. not sure though
    photoshop too expensive? use the GIMP! www.gimp.org

  • #3
    New Coder
    Join Date
    Dec 2002
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your comments!

    I'm not sure if i understand correctly...

    The function i'm trying to do is inside a class. Does that make any difference?

    None of the functions inside the class have the same syntax that your example has. So, i'm not sure if were talking about the same thing.

    It's a shopping basket class that i'd like to expand further. All the "calls" to it are made with get.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Elmore
    The function i'm trying to do is inside a class. Does that make any difference?
    Oh indeed it does! In your case, it's no longer an ordinary function, but rather called a method. But putting naming issues aside, the syntax to call a method is distinctively different to normal function calls.

    None of the functions inside the class have the same syntax that your example has. So, i'm not sure if were talking about the same thing.
    That is a hard to understand. It would only make sense that the content and actual code of your class methods differ from whackaxe's function. But a class method might look exactly like the one whackaxe posted, so the syntax is ok in general. You might post some examples so we get a better idea where your problem actually lies.

    It's a shopping basket class that i'd like to expand further. All the "calls" to it are made with get.
    You mean with method calls like $this->getArticle($id)? Or rather that the class' behaviour is defined through the GET variables?

    From your post I'm not quite sure if you understand the difference between a function and a method call. Some examples:

    PHP Code:
    check_out(); // <- normal function call
    $cart->addArticle('foo'); // <- calling an object's method
    $this->addArticle('foo'); // <- calling a method "addArticle" from inside the class
    print ShoppingCart::getVersion(); // <- static method call 
    BTW, expanding on an existing class is usually done by extending it (like class MyCart extends ShoppingCart). Not sure if that helps you, but it's a useful way to do things.
    De gustibus non est disputandum.

  • #5
    New Coder
    Join Date
    Dec 2002
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your post does clear things up a bit. I used the last night reading about the functions but i guess i'm stoopid since i still couln't get it to work

    The shopping cart class that i'm trying to add things is here:

    http://phpug.ch/codereview/original....ge_currlang=EN

    The problem is that i'd like to be able to make a function that can be called via html link. I'd like it to be "on/off" kind of thing.

    Like when something is=1 then the e-mail is sent and when it's 0 nothing happens.

    I know that this isn't safe method etc. but i'd still like to experiment with it. Once i could make a function that i could execute via html link then i could start writing something useful inside it.

    I tried making it using the same syntax as the delete_cart() which
    deletes the cart contents when =1

    Thanks for your patience and comments.
    I know that i don't know much and i should built up my knowledge a bit by bit. Now i maybe reaching too high too soon but still i'd like to know how to add a function that can be called and how to call it.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, the source code you linked does not show a call to a method if a certain GET variable is set. Can you post the code you wrote?

    As a quick example, here's how such a code might look like. You first have to check that a GET variable with the appropriate value exists and then you instantiate the object and call the desired method. Like:

    PHP Code:
    if (isset($_GET['send']) && $_GET['send'] == 'on') {
      
    $cart =& new basket();
      
    $cart->check_out();  // <- place your method name here

    I wouldn't recommend doing two things at onec, like figuring out how GET variables are handled and how objects work. Try first to execute some arbitrary code when a certain condition, based on the passed variables, exists and tailor it to your specific needs. After that you should extend your object with the mail sending method and test that without the GET part, and if it works, put both parts together.
    Post the steps you are at so we can have a better idea what's going on, so we can give more detailed help.
    De gustibus non est disputandum.


  •  

    Posting Permissions

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