Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call php from php.

Status
Not open for further replies.

jgraves13

Programmer
Jan 31, 2007
42
0
0
US
Hello,

I want to call a php file "Rotate.php" from my index.php file, any ideas?

~JG
 
If I use a one of these options
Code:
include("Rotate.php");
require("Rotate.php");
include_once("Rotate.php");
require_once("Rotate.php");

can I then do this?
Code:
<Body OnLoad="Rotate.php">

Best Regards!
 
can I then do this?
CODE
<Body OnLoad="Rotate.php">

no. of course not. php is a server side language and the onload method of a page is an instruction to a browser to invoke a client side script (such as vbscript or javascript).

you can, however, use javascript to create an xmlhttpobject that can interact with a php script on a server.

you can also create a javascript that will refresh an image tag from time to time. if you make the source of the image tag point at a php file, your php file can then rotate the information (i.e. an image) that it serves to the browser.
 
Audiopro to answer your question, yes I want to rotate a picture everything the browser is reloaded.

jpadie thank you for straitening me out on the onLoad. I am not sure if I am comfort about yet with using AJAX to have it interact with the php script for security reasons. I have a php script that randomly selects an image in the image directory when it's called. but I want to display the random picture on the index page.

I am currently using a javascript to load 3 images to an array then when the page load it flips through them every 35 seconds. However, I am tired of having to load new pictures in the array everyday, and would like to either automaticly load the array for me, or have a script grab one pic at time everytime the page is loaded. I suppose I should look at the AJAX way, and not worry so much about it.

~JG
 
the application you are considering is very straightforward in both javascript and php.

try something like this

Code:
<?php
session_start();
initvars();	//create some variables if they don't exist
$imgdir = "path/to/image/directory";	//no trailing slash.  

//grab all the images into an array
$dh = opendir($imgdir);
while (false !== ($image=readdir($dh))){
	//test the image to make sure it is displayable and not recent
	if (		$image !== "." && 
				$image !== ".." && 
				isImage($image) && 
				!in_array($image, $_SESSION['oldimages']) && 
				!in_array($image, $_SESSION['newimages'])){
			$images[] = $image;
		}
}
$num_images = count($images);
//grab a random image
$img = rand(0, $num_images-1);
serve($images[$img]);

function isFile($image){
	global $imgdir;
	if (!is_readable($imgdir.'/'.$image)) return false;
	$parts = explode(".", $image);
	$extension = $parts[count($parts)-1];
	$permittedTypes = array("jpeg", "jpg", "png", "gif");
	if (!in_array($extension, $permittedTypes)) return false;
	return true;
}
function serve($image){
	global $imgdir;
	cleanSessions($image);	//manipulate session data
	@readfile($imgdir.'/'.$image);	//deliver the file to the browser
	exit();	//kill the script
}
function cleanSessions($image){
	$_SESSION['newimages'][] = $image;
	if (count($_SESSION['newimages'])){
		$_SESSION['oldimages'] = $_SESSION['newimages'];
		$_SESSION['newimages'] = array();
		session_write_close();// this is important because we want the new data to be saved asap as there will be multiple calls on this file in short succession
	}
}
function initvars(){
	if(!isset($_SESSION['newimages'])) $_SESSION['newimages'] = array();
	if(!isset($_SESSION['oldimages'])) $_SESSION['oldimages'] = array();
}
?>

Code:
var R = 0;
var intv = window.setInterval(setSrc, 35000);
function setSrc(){
	var images = document.getElementsByName("rotator");
	for (i=0; i<images.length; i++){
		images[i].src = "rotate.php?id=" + R;
		R++;
	}
}
Code:
<img name="rotator" src="rotate.php" />
<!--include class, h and w and alt etc if you want -->

Note that this script uses a single php script as the image name for each image. the content differentiation is done entirely serverside.

An alternative method would be for the rotator.php script to return an array of three file names which your receiving script would then assign out to each of the img tags. the latter method could make use of image caches and thus speed up the page interaction. the former does not (in fact you should explicitly send anti-cache headers to the browser). If you want to go the filename only route you need to look into ajax and, probably, JSON. my rotate.php script should be easy enough to adapt. below is a version that might work for you. You still need to build the js to request and deal with the input

Code:
<?php
session_start();
initvars();	//create some variables if they don't exist
$imgdir = "path/to/image/directory";	//no trailing slash.  

//grab all the images into an array
$dh = opendir($imgdir);
while (false !== ($image=readdir($dh))){
	//test the image to make sure it is displayable and not recent
	if (		$image !== "." && 
				$image !== ".." && 
				isImage($image) && 
				!in_array($image, $_SESSION['oldimages']) { 
				$images[] = $image;
		}
}
$num_images = count($images);
//grab 3 random images
while (count($_SESSION['newimages']) < 3) {
	$img = rand(0, $num_images-1);
	if (!in_array($images[$img], $_SESSION['newimages']){
		$_SESSION['newimages'] = $images[$img];
	}
}
serve();

function isFile($image){
	global $imgdir;
	if (!is_readable($imgdir.'/'.$image)) return false;
	$parts = explode(".", $image);
	$extension = $parts[count($parts)-1];
	$permittedTypes = array("jpeg", "jpg", "png", "gif");
	if (!in_array($extension, $permittedTypes)) return false;
	return true;
}
function serve(){
	global $imgdir;
	cleanSessions();	//manipulate session data
	$path = "[URL unfurl="true"]http://www.example.com/images/";[/URL] 
	foreach($_SESSION['newimages'] as $image){
		$images[] = $path . $image;
	}
	echo json_encode($images);	//output the array to the ajax object
	exit();	//kill the script
}
function cleanSessions($image){
	$_SESSION['oldimages'] = $_SESSION['newimages'];
	$_SESSION['newimages'] = array();
	session_write_close(); // this is important because we want the new data to be saved asap as there will be multiple calls on this file in short succession
	}
}
function initvars(){
	if(!isset($_SESSION['newimages'])) $_SESSION['newimages'] = array();
	if(!isset($_SESSION['oldimages'])) $_SESSION['oldimages'] = array();
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top