Current location: Hot Scripts Forums » Programming Languages » PHP » problem in slidshow with php+Ajax


problem in slidshow with php+Ajax

Reply
  #1 (permalink)  
Old 05-09-10, 01:34 PM
niceguy82 niceguy82 is offline
Newbie Coder
 
Join Date: May 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
problem in slidshow with php+Ajax

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'].'" />';
}  
?>


and this the output of code on the server
http://www.2worldshopping.com/test/slidshow.php
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-10-10, 01:32 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
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);
$phosmysql_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"]) ? $_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'].'" />';
?>
__________________
Jerry Broughton

Last edited by job0107; 05-10-10 at 01:38 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-10-10, 11:16 AM
niceguy82 niceguy82 is offline
Newbie Coder
 
Join Date: May 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
i love you job0107 you are the best
thank you man very much you know that i have 3 days with this problem
now it's work excellent
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-10-10, 12:26 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your very welcome.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-11-10, 05:16 AM
niceguy82 niceguy82 is offline
Newbie Coder
 
Join Date: May 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
god bless you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP or PHP which is better? nepala The Lounge 9 07-14-10 06:48 AM
[SOLVED] PHP AJAX and IE disabled option issue Dan Man PHP 1 08-26-08 03:34 PM
PHP Image Gallery showing headers and code. Apache problem? slaterino Web Servers 0 08-08-08 06:33 AM
PHP Communication Problem Califer PHP 0 07-05-06 01:07 AM
PHP script problem (please help) osmanmumtaz PHP 0 05-24-05 08:29 AM


All times are GMT -5. The time now is 03:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.