Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-07-2002, 03:24 PM   PM User | #1
skinme!
New Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
skinme! is an unknown quantity at this point
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)
skinme! is offline   Reply With Quote
Old 08-07-2002, 03:38 PM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 08-07-2002, 03:57 PM   PM User | #3
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
Re: Calling functions defined in included files.

Quote:
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?

Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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.
mordred is offline   Reply With Quote
Old 08-07-2002, 05:18 PM   PM User | #4
skinme!
New Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
skinme! is an unknown quantity at this point
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.
__________________
skinme.net / v2.php (design nearly completed)

Last edited by skinme!; 08-07-2002 at 05:22 PM..
skinme! is offline   Reply With Quote
Old 08-07-2002, 06:19 PM   PM User | #5
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,901
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 08-07-2002, 06:46 PM   PM User | #6
skinme!
New Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
skinme! is an unknown quantity at this point
Quote:
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)
skinme! is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:57 PM.


Advertisement
Log in to turn off these ads.