hello guys this is my first post to your forum i hope that you can help me with my problem
i have that script to take last images added to mysql database and slidshow in main page with intrval 10 second
but there is a problem with code it's show first image and after 10 second it show up all the other images in one time
here i'll post you the code i have
the first file slideshow.php
Code:
<html>
<head>
<script language="javascript">
function jah_switch(url,target)
{
if (document.getElementById(target).innerHTML != '')
{
document.getElementById(target).innerHTML = '';
}
else
{
jah(url,target);
}
}
function Switch(url,target) {
// native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {jahDone(target);};
req.open("GET", url, true);
req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = function() {jahDone(target);};
req.open("GET", url, true);
req.send();
}
}
}
function jahDone(target) {
// only if req is "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
results = req.responseText;
document.getElementById(target).innerHTML = results;
} else {
document.getElementById(target).innerHTML="خطأ:\n" +
req.statusText;
}
}
}
</script>
</head>
<body>
<?php
require('config.php');
echo '<div id="photo_show">';
$get_phos = mysql_query ("select Car_pic from car order by Car_id desc LIMIT 10");
$phos= mysql_fetch_array($get_phos);
echo '<img width="200" src="'.$phos['Car_pic'].'" />';
echo '</div>';
// echo "<script>setInterval('switch(\'photo_show.php\', \'photo_show\')', 10000);</script>";
echo "<script>setInterval('Switch('photo_show.php', 'photo_show\)', 10000);</script>";
?>
</body>
</html>
and the second file photo_show.php
Code:
<?php
include('config.php');
$get_phos = @mysql_query ("select Car_pic from car order by Car_id desc LIMIT 10") or trigger_error(mysql_error(),E_USER_ERROR);
while($phos = @mysql_fetch_array($get_phos))
{
echo '<img width="200" src="'.$phos['Car_pic'].'" />';
}
?>
You only need to grab one photo at a time.
So you need a way to tell photo_show.php which photo you want.
And you need to know how many photos there are total, so you don't tell photo_show.php to grab a photo that doesn't exist.
So in your main program you get the first photo and display it.
You also get a count of how many photos there are total.
Then you need to tell Javascript how many photos there are total.
Then when you send an HTTPrequest, you tell photo_show.php which photo you want,
and you have Javascript increment a variable that keeps track of which photo to get next.
Something like this:
PHP Code:
<html> <head> <script language="javascript"> var id = 0; var numRecords = 0; var slideInterval;
function Switch(url,target) { id++; if(id == numRecords){id = 0;} // native XMLHttpRequest object if (window.XMLHttpRequest) { req = new XMLHttpRequest(); req.onreadystatechange = function() {jahDone(target);}; req.open("GET", url+"?start="+id, true); req.send(null); // IE/Windows ActiveX version } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); if (req) { req.onreadystatechange = function() {jahDone(target);}; req.open("GET", url+"?start="+id, true); req.send(); } } }
function jahDone(target) { // only if req is "loaded" if (req.readyState == 4) { // only if "OK" if (req.status == 200) { results = req.responseText; document.getElementById(target).innerHTML = results; } else { document.getElementById(target).innerHTML="ÎØÃ:\n" + req.statusText; } } } </script> </head> <body> <?php require('config.php'); $get_phos = mysql_query ("select Car_pic from car order by Car_id desc"); $numRecords = mysql_num_rows($get_phos); $phos= mysql_fetch_assoc($get_phos); ?> <div id="photo_show"> <img width="200" src="<?php echo $phos['Car_pic']; ?>" /> </div> <br /> <button onclick="clearInterval(slideInterval);">Stop</button> <button onclick="slideInterval = setInterval('Switch(\'photo_show.php\', \'photo_show\')', 10000);">Start</button> <script> numRecords = "<?php echo $numRecords; ?>"; slideInterval = setInterval("Switch('photo_show.php', 'photo_show')", 10000); </script> </body> </html>
photo_show.php
PHP Code:
<?php include('config.php'); $start = empty($_GET["start"]) ? 0 : $_GET["start"]; $get_phos = @mysql_query ("select Car_pic from car order by Car_id desc limit $start,1") or trigger_error(mysql_error(),E_USER_ERROR); $phos = @mysql_fetch_assoc($get_phos); echo '<img width="200" src="'.$phos['Car_pic'].'" />'; ?>