4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 executing php file from inside another php file
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
phpDude
Junior WebHelper
Junior WebHelper


Joined: 28 Apr 2002
Posts: 2

PostPosted: Sun Apr 28, 2002 9:31 pm (15 years, 6 months ago) Reply with QuoteBack to Top

Hi all,

I have a php file that generates a table that I would like to save into a variable inside another php file. How would I do this? the include function seems to just execute the file and output it so I can't seem to save the output to a variable. I thought that maybe exec or passthru might work, but they don't.

Any ideas? The reason I need to be able to save the output to a variable is that my system uses a templating system and I need to save to a variable that the template can access.

I'm pretty sure there is a command in php for doing this, but could not find anything in the manual or through searching.

thanks.
OfflineView User's ProfileFind all posts by phpDudeSend Personal Message
robert
Junior WebHelper
Junior WebHelper


Joined: 28 Mar 2002
Posts: 20
Location: Heythuysen, The Netherlands

PostPosted: Mon Apr 29, 2002 5:02 pm (15 years, 6 months ago) Reply with QuoteBack to Top

I'm not sure about this, but i think you just should save the output in a variable in the "other" script (the script that's generating the table). Remember not to print the output in the table-generating-script, cuz then it would be saved to a variable AND it'll be printed.

hope this works out. Confused

________________________________
Friends help you move. Real friends help you move bodies.
OfflineView User's ProfileFind all posts by robertSend Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Mon Apr 29, 2002 5:59 pm (15 years, 6 months ago) Reply with QuoteBack to Top

You could make your table creator into a function, then it would only out put when you called it. Smile

edit: I just reread the question this probably isn't relevant Embarassed

Code:
<?
//file1.php

function create_table() {

?>

<table cellspacing="0" cellpadding="0" align="center">
  <tr>
   <td>Cell 1</td>
   <td>Cell 2</td>
  </tr>
</table>

<?

}

?>


Code:
<?
//file2.php

include("file1.php");

?>

<html>
<head>
  <title></title>
</head>
<body> 

<?

create_table();

?>

</body>
</html>
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Mon Apr 29, 2002 6:40 pm (15 years, 6 months ago) Reply with QuoteBack to Top

Is this what you want? Question Question

Code:
include.php:

$variable="blabla";

index.php:

include ("include.php");

echo $variable;

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
phpDude
Junior WebHelper
Junior WebHelper


Joined: 28 Apr 2002
Posts: 2

PostPosted: Mon Apr 29, 2002 9:50 pm (15 years, 5 months ago) Reply with QuoteBack to Top

I had a php file (two.php) that generated a table and echoed html output. I needed to store this html output in a variable for a template system. Problem with calling include(two.php) inside one.php was that it started generating html start page stuff and echoed the output into the html instead of the variable. I was looking for an easy way to save the output, but was unsuccessful. I really needed to do this right away, so after I posted the message I hacked a quick, but dirty solution which is essentially what Robert mentioned. Inside two.php now I store the html into a variable $table_html instead of echoing it. Inside one.php I include two.php and then use the variable $table_html. Hopefully they will include a function for this in the future since I had to go in the file and change all the echos. Another way that might work is to use output buffering and save the buffer into a variable, but I did not try this as I was unsure of how it would affect the output buffering already in use in one.php and the file was short enough that I only had to spend a few minutes fixing it.

It looks like others have need for this also. I was browsing around and interestingly found a page that faces the problem of including a file that echos html output inside another. You can see the problems at this site. http://www.marksportal.net/dev/cal_demo/

Thanks for all who replied.
OfflineView User's ProfileFind all posts by phpDudeSend Personal Message
jayant
Team Member



Joined: 07 Jan 2002
Posts: 262
Location: New Delhi, India

PostPosted: Wed May 01, 2002 7:06 pm (15 years, 5 months ago) Reply with QuoteBack to Top

I am sure this will work:

Code:

<?php
// get a web page into an array and print it out
$fcontents = file ('http://idya.resource-locator.com/');
while (list ($line_num, $line) = each ($fcontents)) {
    echo "<b>Line $line_num:</b> " . htmlspecialchars ($line) . "<br>\n";
}
// get a web page into a string
$fcontents = join ('', file ('http://idya.resource-locator.com/'));
?>


Good Luck!

________________________________
Jayant Kumar
Member of the 4WebHelp Team
Nibble Guru - Computing Queries Demystified
GZip/ Page Compression Test
OfflineView User's ProfileFind all posts by jayantSend Personal MessageVisit Poster's WebsiteYahoo MessengerMSN Messenger
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.092035 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme