Forum Moderators: coopster
<?php
$foo="bar";
?>
<script>
window.open("myfile.htm?var=<?php print($foo)?>")
</script>
PHP runs on the server, javascript runs at the client. Thus the output from above will look like this:
<script>
window.open("myfile.htm?var=bar")
</script>
So... You can use PHP to output custom javascripts - it's a very useful method. Try this as an example:
<script>
<?php
for ($i=0;$i<5;$i++){
print("alert('hello world!');");
}
?>
</script>
The result is:
<script>
alert('hello world');
alert('hello world');
alert('hello world');
alert('hello world');
alert('hello world');
</script>
... and you'll see 5 alerts when the script is run.
Good luck!
Actually, I want to display nine items in a page. And when visitors click on the 'More>>', a new windows will be opened, so, can I use one function for all items?
That mean the function will be used several times and display different information.
If so, what can I do in passing variable to it?
How you approach this problem depends where you're getting these "items" from. If you're querying a database, you can use SQL to grab as many or as few items as you want. If the items are in a text file, an array, or generated by some kind of algorithm, then you will have other challenges.
A good strategy to start you off is to make a function that displays $x items, stating at $offset.
function showstuff($myarray,$x=10,$offset=0){
for ($i=$offset;$i<$offset+$x;$i++){
echo($myarray[$i]);
}
}
then you can call the function a few ways:
showstuff($inarray);
// shows the first 10 items in $inarray
// note: these are items 0 through 9, you might
// like to display them as 1 - 10, just by adding 1
// to the value of $i as it loops
showstuff($inarray,count($inarray));
// shows all the contents of $inarray
showstuff($inarray,10,30);
// shows items 30-39 of $inarray
Good luck to you!