bbb Posted May 14, 2005 Posted May 14, 2005 I'm trying to run a script that uses: fopen('filename.php', 'r') where filename.php contains a loop code to create a page of links If i load the filename in the browser: www.mysite.com/filename.php then it works as expected However, when calling the file using the fopen method above, nothing appears on the page. If i do a 'view source' i can see the code from filename.php, but there is nothing on the page in the browser (where i want the links to be). The <?php and ?> are in the filename.php file. As a test i tried hard-coding it as such: fopen('http://www.mysite.com/filename.php', 'r') and it performed as desired. Any insight on this one? Thank you. Quote
bbb Posted May 14, 2005 Author Posted May 14, 2005 An additional comment: Once the file is selected fread() is used to read it. Also, even a line i've added as a test to filename.php: echo "hello world"; does not appear when the file is called from fopen() and fread() (but does appear when called by typing the path url into a browser) Thanks. Quote
TweezerMan Posted May 15, 2005 Posted May 15, 2005 Since you want 'filename.php' to execute php code ("The <?php and ?> are in the filename.php file."), you probably should be using include() instead of fopen() and fread() in your script. Quote
bbb Posted May 15, 2005 Author Posted May 15, 2005 Thanks for the reply... I was actually editing a pre-made script and although i tried a few variations of include() i was not able to get it to work (as i only understand most of the code in the pre-configured script) For anyone else that may be faced with a similar situation, here is what i did: Since it worked as i wanted wtih fopen('http://www.mysite.com/filename.php', 'r') i set the url in the fopen() as such: 'http://www.mysite.com/' . '$variablefile' So now $variablefile can be: myfile1.php myfile2.php etc, etc and will be called up accordingly. I understanding this uses a little extra bandwidth since the files are called thru the internet rather than locally thru the server, but it serves my purposes well and seems to be working fine. If there are any pitfalls with the above method that i'm not aware of please let us me know. Thanks. Quote
borfast Posted May 15, 2005 Posted May 15, 2005 The difference between calling fopen() with an URL and with a filesystem path is that when you call it with an URL it will generate an HTTP GET request, just as if you were requesting the file from your browser, meaning that the PHP file will be interpreted first and you will only be able to read the script output from the resulting file pointer. If you use a filesystem path, fopen will open the file directly, meaning that the resulting file pointer will give you access to the real content of that file, that is, the PHP script instead of the output it produces. This is what include()/require() does. It fetches the PHP code from the file and executes it along the rest of the code where it is being called. Unless your script is already made to process the output by accessing it via a file pointer returned by fopen() and you can't/don't want to change that, you should use include() or require() instead, since they'll be much faster than using fopen(). Quote
bbb Posted May 16, 2005 Author Posted May 16, 2005 (edited) After too many hours on this i still can't seem to get it right (yes, i can do it by calling a webpage but i want to do it the right way). Essentially this is the part of the code that i believe is messing up: $data = "<table cellpadding='5' cellspacing='0' border='0' width='<{tbl_width}>' style='border:2px solid <{tbl_border}>' class='forum1' align='center'>"; $data .= "<tr><td>"; include("myfile.php"); $data .= "hello"; $data .= "</td></tr>"; $data .= "</table>"; $this->output = $data; $print->add_output("$this->output"); $print->do_output( array( 'TITLE' => $this->page_title, 'JS' => 0, 'NAV' => $this->nav ) ); As you may see i am creating a table and inserting data (from myfile.php) into the table. The problem is that the data from myfile.php appears at the top of the page instead of in the table. The "hello" is just put in there as a test as that text does indeed show up inside the table. Can somebody please explain wh the data from the include statement appears at the top of the page? Thank you. PS- above i mentioned i understand most of the code... the parts wtih "->" in it is what i don't understand... is that html, php, etc? Thanks again. Edited May 16, 2005 by bbb Quote
bbb Posted May 16, 2005 Author Posted May 16, 2005 Code is below (changed some stuff to protect data and filenames): <h2 align="center">TITLE</h2><?php <have some connect to Database stuff here> $sql = "SELECT * FROM table_name; $result = mysql_query($sql) or die(mysql_error()); //creates loop to list each html link //echo "<tr><td>"; while ($newArray = mysql_fetch_array($result)) { $tid = $newArray['tid']; $title = $newArray['title']; echo "<a href='http://www.mywebsite.com/$tid' target='_BLANK'>$title</a><br>"; } //echo "</td></tr>"; ?> I tried it with echo "<tr><td>"; (both opening and closing) included and removed... they are commented out above as that is the last thing i did in my testing. Thank you! Quote
TweezerMan Posted May 17, 2005 Posted May 17, 2005 In your main script, its output is being accumulated in a variable ($data) - it is not immediately sent to the browser. After all of the page data has been accumulated, then the main script sends (prints) the content of the $data variable to the browser. In the script you want include, its output is not accumulated in any variable - it is sent to immediately to the browser. Since this occurs before $data is printed, its output will always appear at the top of the browser. If you want the included script's code to be inserted in the middle of what your main page outputs, its output needs to be inserted into the contents of your main script's $data variable. I'd suggest modifying your 'myfile.php' script to the following: ><?php $data .= "<h2 align=\"center\">TITLE</h2>"; <have some connect to Database stuff here> $sql = "SELECT * FROM table_name; $result = mysql_query($sql) or die(mysql_error()); //creates loop to list each html link // $data .= "<tr><td>"; while ($newArray = mysql_fetch_array($result)) { $tid = $newArray['tid']; $title = $newArray['title']; $data .= "<a href='http://www.mywebsite.com/$tid' target='_BLANK'>$title</a><br>"; } // $data .= "</td></tr>"; ?> With these changes to your 'myfile.php' script, you should now be able to include() it in the main script, and its output should be properly inserted into your main script's output. Hope this helps... Quote
bbb Posted May 17, 2005 Author Posted May 17, 2005 It certainly does help... tried a quick test and got it to work <thanks!>... will try it on all the pages tomorrow. A few additional questions along the same lines if i may: 1) I did previously try this in the original file where i used the include statement: $data .= include('myfile.php'); why would this too have printed the include statement and not simply accumulated the data into the variable $data 2) what language uses the "->" and "=>" symbols: $print->do_output( array( 'TITLE' => $this->page_title, 'JS' => 0, 'NAV' => $this->nav ) );I'd like to read up on it so i have a better understanding of how it works (and can write similar code when necessary). Is there a name for "->" and "=>" so that i can search for it. Thanks again! Quote
TweezerMan Posted May 17, 2005 Posted May 17, 2005 1) I did previously try this in the original file where i used the include statement: $data .= include('myfile.php');why would this too have printed the include statement and not simply accumulated the data into the variable $data <{POST_SNAPBACK}> PHP evaluates the right-hand side of the assignment first [the "include('myfile.php')" part]. The include statement is executed just as if it had been on a line by itself, so any 'echo' statements in the file are still output to the browser. What gets added to $data is the include statement's return value, not the included script's output. Unless the included script explicitly returns a value, the include statement evaluates to true (1) if the include was successful, or false (0) if it was not. To return the included script's output back to the include statement in the main script, the included script would need to accumulate its output in a variable, then the last line of the included script (just before the '?>') would return that value with a 'return $var;' statement. 2) what language uses the "->" and "=>" symbols:>$print->do_output( array( 'TITLE' => $this->page_title, 'JS' => 0, 'NAV' => $this->nav ) ); I'd like to read up on it so i have a better understanding of how it works (and can write similar code when necessary). Is there a name for "->" and "=>" so that i can search for it. <{POST_SNAPBACK}> Those are both PHP. When assigning new elements to an array, the '=>' is what separates the 'key' and the 'value' for that element in the array. Example: ><?php $arr = array('fruit' => 'apple', 'veggie' => 'carrot'); echo $arr['fruit']; // prints 'apple' echo $arr['veggie']; // prints 'carrot' ?> In your example above, there's a function called 'do_output' that expects to have an array passed to it, and it will retrieve the values passed to it by reading $arr['TITLE'], $arr['JS'], and $arr['NAV'] (assuming $arr is the array variable in the 'do_output' function). You can read more about PHP arrays at PHP's web site. The '->' is used with PHP classes and objects. I've never worked with them, but they look similar to Perl's classes and objects (which also uses '->' to work with them). You might try reading "Classes and Objects (PHP 4)" on the PHP web site. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.