Page 1 of 1

unable to add php image rotater in html file

Posted: Thu Sep 24, 2009 12:59 pm
by gauri_agr
Hi All,

I am new to php . I have written a php code to display an image. This imgage change on every day (sun, mon)

this is working fine but when I put in another html file using the propert backgrund-image: url(....) it does't work . I using localost for displaying php files:

here are file code

read.php


<?php
$string =array();
$filePath='./photos';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
}
}
/* $n=rand(0, sizeof($string)-1);
echo $n; */
$n1=idate("w");
echo $n1;
echo "<img src='$filePath/$string[$n1]' />";
?>

index.html ( the html code in problem)

<!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" />
<style type="text/css">
#image {width:100px;height:100px;background-color:red;background-image: url(http://localhost/website/alpha/read.php);}

</style>

<title>Welcome to Children’s Yoga!</title>
</head>
<body>

<div id="image"> I want to display image here </div>
</body>
</html>

Thanks
Gauri

Re: unable to add php image rotater in html file

Posted: Thu Sep 24, 2009 1:22 pm
by requinix
Your PHP script is generating HTML. The browser is expecting an actual image.

Tip: if you know the path to the file (on your server) then you can use readfile to print it out.

Re: unable to add php image rotater in html file

Posted: Thu Sep 24, 2009 9:20 pm
by gauri_agr
I changed echo with print as below

print "<img src='$filePath/$string[$n1]' />";

its again the same problem

Re: unable to add php image rotater in html file

Posted: Thu Sep 24, 2009 9:48 pm
by Griven
First, please wrap your code in [ code=php ] [ /code] tags. It makes it much easier to read.

Secondly, your main issue is that you're echoing the image before the HTML of your page. You can mix PHP and HTML as needed to acheive your goals.

Remove the code that echos the image above your DOCTYPE definition and place it inside your DIV, like below.

Code: Select all

<div id="image"><?php echo "<img src='$filepath/$string[$n1]' />"; ?></div>

See how that works for you.

Re: unable to add php image rotater in html file

Posted: Fri Sep 25, 2009 10:42 am
by gauri_agr
Hi Griven,

I am new to html and php. So I amunable to understand the basic of your solution. I tried googling but still unable to digest. Here are my further queries , i just put my comments /quesries on all of your sentances

1. First, please wrap your code in [ code=php ] [ /code] tags. It makes it much easier to read.
why do I need to wrap when I have two seprate file read.php and index.html?

Secondly, your main issue is that you're echoing the image before the HTML of your page.

you mean I need to generate it in index.html. Then I guess I can not use the url property in css. I need to code in a different way...i am right?

You can mix PHP and HTML as needed to acheive your goals.

Remove the code that echos the image above your DOCTYPE definition and place it inside your DIV, like below.

Code: Select all

<div id="image"><?php echo "<img src='$filepath/$string[$n1]' />"; ?></div>[/b]
i tried this code in read.pho but it didnt work. Actually I am unabel to understand the concept so that I could play around your solution. How putting the div around will make the difference?

Thanks & regards
Gauri