unable to add php image rotater in html file

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!

Moderator: General Moderators

Post Reply
gauri_agr
Forum Newbie
Posts: 3
Joined: Fri Jun 26, 2009 6:33 pm

unable to add php image rotater in html file

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: unable to add php image rotater in html file

Post 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.
gauri_agr
Forum Newbie
Posts: 3
Joined: Fri Jun 26, 2009 6:33 pm

Re: unable to add php image rotater in html file

Post by gauri_agr »

I changed echo with print as below

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

its again the same problem
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: unable to add php image rotater in html file

Post 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.
gauri_agr
Forum Newbie
Posts: 3
Joined: Fri Jun 26, 2009 6:33 pm

Re: unable to add php image rotater in html file

Post 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
Post Reply