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!
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hi,
I'm relatively new to PHP coding so please excuse me if this is a stupid question!
Well, I'm writing a piece of code that reads a list of URLs from a text file and then outputs them in a browser, one at a time at set intervals. Kind of like a slideshow but with websites instead of pictures.
I'm using a while loop to process the URLs, so everytime the loop runs a variable acts as a counter and gets a 1 added to it, so that the next URL in the array can be fetched the next time.
The program reads the URLs in the text list into an array just fine and the loop works too - I've just been getting it to output what it's doing to the screen at the moment, so that I can see it's working.
Well, this brings me on to the problem - I need to get the program to actually take the URLs in the array and fetch the actual website into the user's browser. I'm not too sure how to go about this.
What I want is for the user to see a webpage with two frames. the top will have the PHP code and the dialogue the code generates now (this is just stuff like "now showing URL 1, URL 2, etc.") so that the user can see what the program is doing as it runs. The bottom frame will actually show the websites in it. So say, for example, URL 1 in the array is http://www.yahoo.co.uk - the top frame will say "Now showing URL 1" and Yahoo will be showing the bottom frame, and so forth. As I said, I have the top frame working - it's just the bottom one that's the problem!
I've tried putting Javascript into the loop so that it would be executed everytime the loop ran, and would output the URL into the bottom frame but this doesn't seem to work - it just seems to be ignored and nothing happens - the top frame works but the bottom stays blank.
I've also wondered if it's possible to simply call the frame in HTML and somehow add the URL in as a variable - but not sure if this is possible?
So, I guess what I'm asking is: How do I get the PHP code to open a URL in the bottom frame and also is it possible to use a variable as the address for the URL to refer to - so say would you be able to have something like "HREF=$showthisurl" (I know that's not valid code but just as an illustration of what I'm jabbering on about)?
Also, is it possible to put Javascript code into a While loop in PHP code so it will execute everytime the loop is executed, and furthermore is it possible to take a variable from the PHP code and use it in that Javascript code? I've seen some examples while Googling but none of them are particularly clear on this.
Here's my While loop code as it is right now with my attempt at adding the Javascript in (which seems to do nothing!):
while ( $arraycount >= $processcount ) {
print("Now processing url number ");
print $processcount;
print(" </BR>");
print("<SCRIPT LANGUAGE = JAVASCRIPT>");
print("parent.main.location.href = (<? $processurl; ?>)");
print("</SCRIPT>");
$processcount=$processcount + 1;
}
As you can see, I tried to use the variable from the PHP code which holds the value of the URL to be processed (the $processurl variable). It was from a tutorial I found on Google but it wasn't very clear so it may well be wrong! Also it's been a while since I used Javascript!
Anyway, I hope that someone can help or provide some pointers for this as I've been trying stuff out for about 4 hours now and my brain's gone numb!
I'm a little confused about the placing of the <html> and <?php> tags in your code though - would I put the HTML, Frameset stuff after my PHP code (the stuff that creates the array with the URLs in it) and then follow it with the
<?
$array=file('process.txt'); //This tells PHP to load the text file line by line as an array
?>
<html>
<frameset rows="*,450">
<frame name="theframe" src="start_page.html">
</frameset>
<?
foreach ($array as $url) {
echo '<script type="text/javascript">theframe.src="'.$url.'";</script>';
flush(); //Flushes php output to browser
sleep(5); //Time is in seconds, ie. 5 seconds.
}
?>
</html>
Does this look correct or have I missed something? When I try and load the page in a browser (Firefox), I get the two frames but nothing seems to happen after that. On Internet Explorer, I get the same result except that the bottom frame is greyed out.
I've been playing about with the above code over the weekend and have gotten to the point where I get the following when I click 'View Source' on Internet Explorer:
So it looks like the code IS working - in so far as the browser receives the code with the correct URL to show. The problem is that the page (in this case http://www.google.co.uk) isn't appearing in the frame. All I see is two blank frames. I wait for the frame to change to http://www.google.co.uk but it doesn't do it.
How come the browser receives the code (as seen in 'View Source') but doesn't execute it? Anyone have any ideas?
Looks like the PHP is working and that the Javascript is getting the variable from the PHP array (hence the http://www.google.co.uk). Unfortunately, the Javascript still isn't being executed.
It's getting through to the browser, hence the output above from 'View Source' but it still won't run and open the http://www.google.co.uk webpage. Is there something I'm missing?
Well, I've spent the past week trying this and a number of different things and it just seems that the browser ignores whatever code the PHP script tries to get to the browser using 'echo' (or 'print'). I've tried the Javascript as posted here and also tried just getting PHP to 'echo' some html too but it just won't execute it. It's weird, as the browser seems to receive it (hence the fact that I can see the code when I 'view source' in the browser).
I've tried Firefox and Internet Explorer, made sure Javascript is enabled - also tried accessing from other PCs but it just won't go.
Anybody have any ideas on why this might be? Is there a setting I have to enable somewhere on the PHP server to allow this to work?