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
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling functions defined in included files.

    Hi,

    I'm having problems using functions which I have included into the file:

    welcome.php
    PHP Code:
    <?php

    include "content/functions.php";

    echo 
    makeParas('content/welcome.p');

    ?>
    functions.php
    PHP Code:
    <?php

    function makeParas($txtfile) {

           
    // some code here makes $code
           
    return $code;

    }

    ?>
    When I run the code, I get two errors which indicate myParas is being run without the $txtfile argument and so is buggering up some filesystem commands.

    This indicates that the function is not called by welcome.php ( myParas('content/welcome.p'); ) but is being run as functions.php is included.

    How can I stop it from running? Should I not use <?php ?> around it? Also, will I be able to access the myParas function from welcome.php? Could I do this by saving funcitons.php as a plain text file and then opening it and saving its contents to a string and eval()-ing it?

    n a sidenote, can you pass strings to files you include (perhaps by include "file.php?string=hi")? Jus' wondering.

    I did a search for include function and got no threads which helped on this particular subject. Also, I've looked in the PHP manual to no avail.

    Thanks
    skinme.net / v2.php (design nearly completed)

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It's kind of hard to help you when you left of the pertinent parts of the function. So far it looks just fine so long as you are actually putting something into the $code variable before you return it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling functions defined in included files.

    Originally posted by skinme!
    When I run the code, I get two errors which indicate myParas is being run without the $txtfile argument and so is buggering up some filesystem commands.
    What are the error messages?
    What do you get if you echo $txtfile within your makeParas() function?

    This indicates that the function is not called by welcome.php ( myParas('content/welcome.p'); ) but is being run as functions.php is included.
    I'm skeptic here, that's unlikely. If you include function definitions, the functions get only executed the moment you call them in your code.

    How can I stop it from running? Should I not use <?php ?> around it?
    Wait a moment. First make sure the function behaves properly when called from the same file. If there is a problem with the parameters passed, fix them and after that exclude it to an external file. And yes, those <? markup tags are necessary unless you want to have your php code printed to the browser.

    Also, will I be able to access the myParas function from welcome.php? Could I do this by saving funcitons.php as a plain text file and then opening it and saving its contents to a string and eval()-ing it?
    That's a crude workaround; most likely you don't have to resort to such extreme actions.

    n a sidenote, can you pass strings to files you include (perhaps by include "file.php?string=hi")? Jus' wondering.
    No, that's not possible. Define the variable in your including file before the actual inclusion, then you can access it simply by it's variable name, but not through the $_GET array.

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for jumping the gun and making assumptions earlier.

    I have tried losing the ignore and copy-pasteing to debug it.

    Here's the non-includified source code:

    welcome.php
    PHP Code:
    <?php

    function makeParas($txtfile) {
      
    $fullpath 'http://'.$HTTP_SERVER_VARS[HTTP_HOST].$HTTP_SERVER_VARS[REQUEST_URI];
      
    $thisfile basename($fullpath);
      
    $cutoffpos strpos($fullpath,$thisfile);
      
    $thisdir substr($fullpath0$cutoffpos);
      
    // $thisdir is the full path minus filename incluing trailing slash:
      // "www.mysite.com/thisdir/"

      
    if (!$txtfile) {
        
    // no text file [error]
        
    return "<p>error msg here</p>";
      }
      else {
        
    $path2paras $thisdir.$txtfile;
        
    $content_array file($path2paras);
        
    $content implode("",$content_array);

        
    $paras explode("{",$content);

        foreach (
    $paras as $value) {
          
    $lencssclass strstr($value,"}")-1;
          if (
    $lencssclass 0) {
            
    $cssclass substr($value,"1",$lencssclass);
            
    $paratxt substr($value,$lencssclass+2);
            
    $parasrc .= "<p class=\"".$cssclass."\">".$paratxt."</p>\n";
          }
          else {
            
    $paratxt substr($value,2);
            
    $parasrc .= "<p>".$paratxt."</p>\n";
          }
        }
        return 
    $parasrc
      }
    }

    echo 
    makeParas("welcome.p");

    ?>
    welcome.p
    Code:
    {cssclass1}first para with cssclass1 as it's css class.
    {}second para with no css class associated
    {}again no css class
    {bigtext}fat text
    (NB: In source is all one line but I added linebreaks so as not to stretch the screen)

    errors
    Warning: file("welcome.p") - No such file or directory in C:\phpdev\www\public\includes\content\welcome.php on line 17

    Warning: Bad arguments to implode() in C:\phpdev\www\public\includes\content\welcome.php on line 18

    I'll try to explain the script.

    1. welcome.p is a text file defining the text to put into paragraphs and for some paras, a css class to apply to them.

    2. the script opens the above text file and implodes it into one long string (for convenience) and then explodes it by the character { (the start of every para's configuration).

    3. for each seperate part of the array (read: each paragraph):

    4. if $cssclasslen (the number of chars between {} ) is non-zero, then get the chars between the curly brackets and make the start tag (including class="cssclass"). Then get the rest of the text (the content) and add it between the start tag and an end tag. append this para to $parasrc

    5. if $cssclasslen is zero, there is no class so just get the text and sandwich it between "<p>" and "</p>". append this para to $parasrc

    6. return $parasrc - the code for all the paras.

    I know you may say this is a pointless excersise - "why don't you just make welcome.p contain HTML code?" - but I chose to do it this way, and I'd like to make it work... purely to see where i went wrong so I don't do it again. Thanks.
    Last edited by skinme!; 08-07-2002 at 05:22 PM.
    skinme.net / v2.php (design nearly completed)

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    ok, well the question is, does

    www.mysite.com/thisdir/welcome.p exist ?

    as thats where you script is looking for it (assuming the path is correct) - get your function to echo the $thisdir.$txtfile to see if that matches up with where the file really is.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    New Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by firepages
    ok, well the question is, does

    www.mysite.com/thisdir/welcome.p exist ?

    as thats where you script is looking for it (assuming the path is correct) - get your function to echo the $thisdir.$txtfile to see if that matches up with where the file really is.
    How comes the small things are the ones that bring your script to a halt? I forgot that $thisdir would return the dir in which curvy.php is in, not welcome.php (which was included in curvy.php) Sorry for not mentioning it earlier... most of this was coded late last night, as you can probs tell.
    skinme.net / v2.php (design nearly completed)


  •  

    Posting Permissions

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