Page 1 of 1

Refresh an included php-file?

Posted: Thu Sep 02, 2010 5:24 pm
by The Covenant
Hi. I'm trying to refresh a php-file that is included within another one, but I haven't been successful. Here's what I wanna do (simple version):

File 1:
<html>
<head>

Code: Select all

 <?php include('file2.php'); ?>
</head>
<body>
<div>

Code: Select all

<?php echo $variable_from_file2[0]; ?>
</div>
</body>
</html>

File 2:

Code: Select all

<?php
   $variable_from_file2 = array('Lots', 'of', 'Different', 'Values');
   shuffle($variable_from_file2);
?>]
So, what I want to do, is to shuffle the variable every 10 seconds, thus making a new, and different, entry from the array to be echoed out in file 1. I hope this makes sense - I'm not English, so my language skills are limited.

Thx for help! :)

Re: Refresh an included php-file?

Posted: Thu Sep 02, 2010 9:08 pm
by JakeJ
That's a lot of page refreshes. Perhaps you should consider using AJAX requests for this. Or, if you don't mind the client potentially being able to see the entire array, consider doing it in javascript and reducing your server load.

Re: Refresh an included php-file?

Posted: Thu Sep 02, 2010 10:45 pm
by requinix
An idea to throw out, however you end up implementing the refreshing:

Picking a random item each time means you'll likely show a few duplicates before you've shown every item in the array. Consider shuffling the array just once and then looping over it. That way you show each in a random order. Then when you get to the end, shuffle and loop again.

And +1 to the idea of doing it all in JavaScript.

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 1:35 am
by The Covenant
Thx for replies! I'm gonna need help with the JavaScript - that's a relatively new world for me. Think maybe you could show me some code that would help?

I like the idea where the array is shuffled once for then to be looped through - but, as you point out yourself, we still have the refresh-issue :(

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 7:49 am
by JakeJ
The refresher issue with javascript is a non-issue. Java has a function called setInterval() that will take care of that for you. You'll find plenty of usage examples on line.

Javascript isn't really that hard. The concepts are mostly the same as PHP, it's just a different syntax. And if statement is still an if statement. There are some differences though and some things that seem odd coming from a php world, but you should pick up on it pretty quick after a few tutorials.

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 8:13 am
by The Covenant
I've tried to get the setInterval() to work, but with no success... I think it's because the PHP-code is already processed by the server when the JavaScript is called(?) But... Is there a way for JavaScript to maybe send some information through a HTML-form without the use of a button? That way, I could use the setInterval() to send the form every 10secs with a value to File 2, and then use a While loop there to shuffle the Array? Hope that made sense xD

Thx again for the help :)

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 1:47 pm
by Jonah Bron
Post your code.

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 2:41 pm
by The Covenant
Well. Right now I'm trying to do this with JavaScript alone. I've created an array in a separate .js file and included it like this:

<script type="text/javascript" scr="tekst.js"></script>

and it's containing this:

var tekst=new Array("Different", "Data");

and I'm trying to call the array in the index file like this:

document.write(tekst[0]);

but nothing happens? Why is that? I've tried copying the array over to the index file, and then it works, but it's gonna be so many entries in the array that I want it separated. I don't know how to pick a random entry from a javascript array yet, maybe you could help with that as well?

I know that this is not a php matter anymore, but if someone could come up with a php-based solution I'd be very happy :)

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 4:10 pm
by Jonah Bron
Please post your entire index.html (remember the code syntax tags). Here's a function that gets a random element from an array.

Code: Select all

var randomIndex = function (arr)
{
    return arr[Math.round(Math.random() * arr.length)];
}
Math.random() returns a sudo-random float between 0 and 1. If we multiply that by the length of the array, we get a random number between the first and last elements (exclusive). Math.round() rounds that float to an integer to give you a random number between the first and last elements (inclusive).

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 4:18 pm
by The Covenant

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="main.css"  />
<link rel="shortcut icon" href="bilder/favicon.ico" />
<title>Tittel</title>
<script type="text/javascript" scr="tekst.js"></script>
</head>

<body>
<table align="center" width="800px" cellspacing="0" cellpadding="0">
<tr>
    	<td height="5%">&nbsp;
        	
        </td>
    </tr>
	<tr>
   	  <td valign="top" class="header">
        	<div align="left" class="header1">
       	  		<a href="http://www.marnacafe.no/"><img src="bilder/marna-logo.png" id="marna-logo" /></a>
          	</div>
                <div align="center" id="header2">
            	        <img src="bilder/elitmannen - boble.png" id="elitmannen-boble" />
				<p id="bobletekst">
                                        <script type="text/javascript">
						document.write(tekst[0]);
					</script>
				</p>
	   	 </div>
          	<div class="header3">
            	<a href="http://www.it.no"><img src="bilder/elit-logo.png" id="elit-logo"/></a>
            </div>            
        </td>
    </tr>
	<tr>
		<td class="innhold">
        	<div id="sokeramme-ramme">
        		<form method="get" action="http://www.google.com/search" class="google-form">
					<fieldset id="fieldset">
                    <input type="text" name="q" size="22" class="google" maxlength="255" value="" />
		        	<br />
					<input type="submit" value="Google Søk" />
                    </fieldset>
				</form>
                <br />
        		<iframe src="http://www.gulesider.no/info/sokefelt/gstk_gs180x130.html" width="180" height="130" scrolling="no" frameborder="0" marginheight="0" marginwidth="0"></iframe>
			</div>
        	<div class="meny">
				<ul>
            		<li id="meny-topp">
               	 		<b>Nyheter:</b>
               	 	</li>
					<li>
						<a href="http://www.l-a.no">Lindesnes</a>
					</li>
					<li>
						<a href="http://www.farsunds-avis.no">Farsund</a>
					</li>
					<li>
						<a href="http://www.fvn.no">Fædrelandsvennen</a>
					</li>
					<li>
						<a href="http://www.vg.no">VG</a>
					</li>
					<li id="meny-bunn">
						<a href="http://www.dagbladet.no">Dagbladet</a>
					</li>
				</ul>
			</div>
       		<div class="meny">
				<ul>
            		<li id="meny-topp">
                		<b>E-Mail:</b>
	                </li>
					<li>
						<a href="http://epost.telenor.no">Online.no</a>
					</li>
					<li>
						<a href="http://www.gmail.com">Gmail</a>
					</li>
					<li>
						<a href="http://www.hotmail.com" >Hotmail</a>
					</li>
					<li id="meny-bunn">
						<a href="https://login.yahoo.com/config/login_verify2?&.src=ym">Yahoo</a>
					</li>
				</ul>
	        </div>
		</td>
	</tr>
    <tr>
    	<td align="right" class="bunn">
        	 &copy; Copyright 2010           
        </td>
    </tr>
	<tr>
    	<td height="5%" colspan="3">
        </td>
    </tr>
</table>        

</body>
</html>

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 4:23 pm
by The Covenant
Oh.......... You know... I just saw that the <script type="text/javascript" scr="tekst.js"></script> line is wrong! I'd written "scr" instead of "src".... -.-

But how do I implement the "randomizer"? :)

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 4:59 pm
by Jonah Bron
Wherever you were putting

Code: Select all

document.write(tekst[0])
replace it with

Code: Select all

document.write(randomIndex(tekst))
Be sure to put the function itself before that point.

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 5:43 pm
by The Covenant
Great! I've got a working "randomizer" now! Thank you! Now... One last question before it's complete... How to implement setInterval so that I get a new,random array-entry every 10 seconds? I've tried:

Code: Select all

function timer(){
var t =	setInterval("random()", 10000);
}
Where "random" is the randomizer. Now, that didn't work. Sorry for being noob, but I've got to learn this one way or another. :oops:

Re: Refresh an included php-file?

Posted: Fri Sep 03, 2010 6:11 pm
by Jonah Bron

Code: Select all

var t = setInterval(function () { randomIndex(tekst); }, 10000);
On another note...

Woo Hoo!
Post number 1000!!!!!!!
:mrgreen: :drunk: :D


:D :o :) :? :| :( :cry: :x :evil:

Re: Refresh an included php-file?

Posted: Sat Sep 04, 2010 3:05 am
by The Covenant
Thx for all help :) And gratz with the posts ;)