SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Running several codes in a sequence

    I have several PHP codes that work separately, each of them needs the user to input something to work and at the end they display the result.

    I want to make the scripts work as one, so when I start the first code by sending some data the first code completes his job and sends the obtained data to the next one, that uses it, works it and send it to the next one.

    How can this be done?
    I want an invisible team V

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For each 'stage', write a function which runs the code required and returns the result.

    Then use that result as a parameter for the next stage, and so on.

    For example:
    PHP Code:
    $Value StageOne($_POST['input']);
    $Value StageTwo($Value);
    $Value StageThree($Value);
    $Value StageFour($Value);
    echo 
    $Value
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where this code should be? Each code is ended with echo $result; or print how is the data sent
    I want an invisible team V

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If these 'codes' start getting very complex, you might want to look at how a Finite State Machine works.

    Pear::FSM

  5. #5
    SitePoint Zealot Servyces's Avatar
    Join Date
    Jun 2007
    Location
    The Netherlands
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fluxduster View Post
    Where this code should be? Each code is ended with echo $result; or print how is the data sent
    Well, functions work a bit different then that. Instead of printing the result you should return it and pass it on to the next function. You can echo the output to the user in the meantime (if this is important). For example:

    PHP Code:
    function one () {
        
    $output = (1);
        echo 
    "1 and 1 equals $output";
        return 
    $output;
    }

    function 
    two ($input) {
        
    $output = ($input 10);
        echo 
    "10 times $input equals $output";
        return 
    $output;
    }

    // Now call the functions
    $var one();
    two($var); 
    The result of function one will be passed to the $var variable. You then feed this variable as input to function two. The echo's in the functions will show:

    "1 and 1 equals 2"
    "10 times 2 equals 20"

    Hope that clears up the use of functions a bit.
    Servyces.com
    Where itís all about you.
    Your partner in online solutions.
    Visit our website at http://www.servyces.com/

  6. #6
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have

    file 1...............................|.........file 2
    ......................................|.....$val1 from file1
    $val1 = (1 + 1);echo $val1;.|...$val2 = ($val1 + 1);..echo $val2;

    How do I get $val1 from file 1 in file 2 so that can $val2 can be calculated?
    I want an invisible team V

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Rather than having them in separate files, have them in one and pass them through functions, as shown above.

    Or, if you'd like to keep it your way, in a 'controller' file:
    PHP Code:
    include('file1.php');
    include(
    'file2.php');
    //etc 
    All files can then access the variables used in the files include()d before them.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •