Calling functions contained in a php file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Calling functions contained in a php file

Post by Michael_C »

I have no experience with php, but have in other languages - I have been "drafted" to develop an on line voting app by an organiization to which I belong - yes, I must be very lucky. I am planning on writing a number of PHP classes and functions and they will reside in a file called Survey.php. How would I let the html parser know about the Survey.php file so I can I call call the functions within html code? What is the mechanism in PHP - are there things like header files that I can reference (i.e., via Include)?
Is there a good source to search for PHP functions/commands by category? For example, string manipulation. I wasn't able to find functions to Uppercase or Lowercase a string, but could easily have missed them. Hard to know if they don't exist or if they just have a spelling that is obscure.

TIA,
Michael
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

php.net has the manual, written by the developers of php.

html cannot, by itself call php functionality. If the file is processed by php, you can use include() to bring Survey.php's functionality into existance for that particular page request.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Calling functions contained in a php file

Post by Michael_C »

feyd wrote:php.net has the manual, written by the developers of php.

html cannot, by itself call php functionality. If the file is processed by php, you can use include() to bring Survey.php's functionality into existance for that particular page request.
I have looked at the manual, but haven't seen a place to view all available commands/functions organized by category. Did I just miss it, or is there another way to approach it? Seems like you need to know the command before you can find it. For example, I was looking for case/endcase control, rather than switch - it took a few tries to find this one, but how would one know if it didn't exist versus spelling was far different than expected?

I thought I can mix HTML and PHP by using the <?php tags.

My original thought was to have a "Vote" button on an existing html form with an action set to call a PHP script and pass two parameters - the name of the user, and the ballot name. I was going to hold the ballot info in a MySql database (survey header and details), and display the ballot as long as name of user wasn't contained in the results tables(header and details)

Is this methodology workable, or will I need to restructure it differently.
Michael


feyd | bold is best used for short bits of text, not entire posts.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Calling functions contained in a php file

Post by josh »

Michael_C wrote:I have looked at the manual, but haven't seen a place to view all available commands/functions organized by category. Did I just miss it, or is there another way to approach it?
http://us3.php.net/manual/en/funcref.php

I thought I can mix HTML and PHP by using the <?php tags.
You can but you must be in a PHP document. Ex: your PHP page can contain HTML
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Re: Calling functions contained in a php file

Post by Michael_C »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


[quote="jshpro2"][quote="Michael_C"]I have looked at the manual, but haven't seen a place to view all available commands/functions organized by category.  Did I just miss it, or is there another way to approach it? 
[/quote]

http://us3.php.net/manual/en/funcref.php
[quote] 
I thought I can mix HTML and PHP by using the <?php tags.
[/quote]

You can but you must be in a PHP document. Ex: your PHP page can contain HTML[/quote]
Thanks for the pointer to the funcref section - this is what I wanted - well, maybe a bit longer than I wanted   

I'm confused about placing PHP script in HTML.  Most likely your talking about apples, and I'm thinking oranges - The following is a portion of some PHP code in an existing HTML file:

Code: Select all

<?
echo $error;
if ($set_action=="client_update") {
	$db = new ps_DB;
	$q  = "SELECT * FROM account WHERE account_id='$account_id'";
	$db->query($q);
	$db->next_record();
	
	$username = $db->f("account_username");
                $company = $db->f("account_company");
                $address = $db->f("account_address");

                $check_no = $db->f("account_check_no");
                $acct_status = $db->f("account_status");
                $status = $db->f("account_status");
} else {
                $acct_status="1";
                $country_op="<option value=840 selected>UNITED STATES</option>";
                $pmt_type="1";
}
?>
Are you saying I can't call a PHP function or class from HTML, or I can't have PHP code in HTML file?

Michael


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Please refrain from bolding your whole response Michael.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I think you are confused.

first of all by default PHP will only parse PHP code inside of files with a .php extension. That being said your definitions of HTML page and PHP pages are not really solid. A file with a .php extension can contain both PHP code and HTML code, a file with a .html extension may only contain HTML code (by default). If you want PHP in your .html files to be executed you will have to tell PHP to do so by editing your httpd.conf file or your .htaccess file.

Let's say we have the following "html file" called myFile.php

Code: Select all

<html>
 <head>
  <title>mypage</title>
 </head>
 <body>This page left intentionally blank</body>
</html>
Let's say we have the following "PHP file" called 'funky.php'

Code: Select all

function myFunkyFunction() {
    echo 'Hello world';
}

We can call myFunkyFunction inside of your "HTML" file like so:

Code: Select all

<html>
 <head>
  <title>mypage</title>
 </head>
 <body>
   This page left intentionally blank
   <?php
   include('funky.php');
   myFunkyFunction();
   ?>
 </body>
</html>
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

Sounds like the distinction you are making is PHP can't be included in HTML if you haven't configured thngs appropriately. Is this accurate?
If so, then you could include PHP code in an HTML doc if the httpd.conf file or .htaccess file were set appropriately, and used the <?php tag??

What is the downside of doing this?

Is there a better way to handle this?

Michael
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Michael_C wrote:Sounds like the distinction you are making is PHP can't be included in HTML if you haven't configured thngs appropriately. Is this accurate?
If so, then you could include PHP code in an HTML doc if the httpd.conf file or .htaccess file were set appropriately, and used the <?php tag??
That is only if you wish to parse php code in a file with a non .php extension
Michael_C wrote:What is the downside of doing this?
Lets say you added .html to be parsed by php.. now every single time a .html is requested, it will fire up the php engine (even if you do not have any php code in the file)
Michael_C wrote:Is there a better way to handle this?
Change your files extension to .php :wink:
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Depending on your server set up, you could use SSI to include the output of a php page into a (S)HTML page.

Code: Select all

Put this into your (S)HTML page.
<!--#include virtual="/path/to/file.php" -->
I wrote a script today that selects a random image, outputs the image tag and link tag, opens a text file and increments an impression counter. I included it using SSI.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

Jcart wrote:Lets say you added .html to be parsed by php.. now every single time a .html is requested, it will fire up the php engine (even if you do not have any php code in the file)

Thanks for the feedback. I'll check into it further, but my quess is the hit is already being paid - since the website has a number of PHP code sections contained in HTML pages.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

Jcart wrote:Change your files extension to .php :wink:
I thought a bit about your suggestion overnight and the only reason I would want to have PHP embeded in the HTML file is to make the two pieces of information available to the PHP function by passing them as parameters . Would the vars gcSurveyId and gcUserName be in scope inside Survey.php if I had something like the following?

Code: Select all

<form action="Survey.php" method="post"> 
  $gcSurveyId = "SrvHF06" 
  $gcUserName = $lcFirstName . " " . $lcLastName 
  <input type="Submit"> 
</form>
Is there a better way to "kick off" the php script and make the two pieces of info available to the script?

Michael
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That information would not be available to PHP upon submission. If you want to transfer data between pages without passing said data to the user's browser, sessions are the way to go.

http://php.net/sessions
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

feyd wrote:That information would not be available to PHP upon submission. If you want to transfer data between pages without passing said data to the user's browser, sessions are the way to go.

http://php.net/sessions
The PHP manual says you must call session_start() prior to outputing anything to the browser(when using cookie based sessions).
Does this mean it should be done prior to outputting anything at all, or when outputing anything on a particular website page?

In the current website the session id is created at time of login. I'd like to store some of the user data in _SESSION to be used in the Advanced Poll app. What I thought I would do was add the following, to the existing login section.

Code: Select all

session_start() ;
$_SESSION['AccountName'] = $lcAccountName ;
$_SESSION['PrimaryName'] = $lcPrimaryName ;
$_SESSION['SecondaryName'] = $lcSecondaryName ;
session_write_close();
The existing website has code that generates a session id.
I'm assuming there is some mechanism that associates a particular users _SESSION array to their session id. Is this an internally created session id, or would the association use the session id generated during login? What do I need to do to support this "association"?

Like always, any and all help is greatly appreciated.

Michael
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Michael_C wrote:The PHP manual says you must call session_start() prior to outputing anything to the browser(when using cookie based sessions).
Does this mean it should be done prior to outputting anything at all, or when outputing anything on a particular website page?
Sessions must be restarted on each page request to have session data accessible to your script(s).
Michael_C wrote:The existing website has code that generates a session id.
I'm assuming there is some mechanism that associates a particular users _SESSION array to their session id. Is this an internally created session id, or would the association use the session id generated during login? What do I need to do to support this "association"?
The ID used is created by PHP. It will remain the same id throughout the user's session provided you do not call session_regenerate_id() or they delete the session. So long as you keep the session alive, the association is kept for you.

This is all assuming you don't overwrite the default session handling systems in PHP.
Post Reply