TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-27-2008, 04:26 PM   #1 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default Refreshing an image + preloading it

Hi guys,

Basically, I have a site which just basically uses the glob and array_rand functions to sweep my image dir and show a random image.

Now, I would like to implement two things, however the main thing is to refresh the image, without refreshing the whole page.
All it needs to be is a simple refresh: it doesn't need to know what image its going to show next, as the array_rand will sort that out on the refresh.

This leads me onto something which I reckon is more complicated. The very fact that it doesn't know what it is showing next means that it can't preload it- another feature I wish to implement.

I am not asking for any code (although I would really appreciate it if you did give me some), just your ideas on how they can be implemented..

Does anyone have any ideas?

Gareth

Last edited by Gareth : 02-03-2009 at 06:38 PM.
Gareth is offline  
Reply With Quote
Old 08-27-2008, 05:38 PM   #2 (permalink)
The Contributor
 
flyingbuddha's Avatar
 
Join Date: Jan 2008
Location: Birmingham, UK
Posts: 60
Thanks: 10
flyingbuddha is on a distinguished road
Default

Use jQuery.
Make the image src point to your script, and append a querystring to the end with a unique number (using Date() / getSeconds()) every time you want to refresh the image (assuming you'll use something such as setInterval())

Using jQuery, you could use the load event to monitor when it's loaded, you might need to trigger this event each time you have refreshed the src -

$('img').load(function(){
alert('loaded');
});
__________________
Pro. Geek
http://www.mikeholloway.co.uk
flyingbuddha is offline  
Reply With Quote
Old 08-28-2008, 12:52 AM   #3 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

And how exactly would I refresh the image? Your last two paragraphs lost me, too :P
Gareth is offline  
Reply With Quote
Old 08-28-2008, 06:32 AM   #4 (permalink)
The Contributor
 
flyingbuddha's Avatar
 
Join Date: Jan 2008
Location: Birmingham, UK
Posts: 60
Thanks: 10
flyingbuddha is on a distinguished road
Default

So you are looking for some code ;)

I've not tested this as I'm off to work now, you may have some problems in PC IE, if you do, let me know and I'll fix it for you.

Code:
var refresh = setInterval(function(){
	
	// create a new timestamp
	var timestamp = new Date();
	
	// keep track of the image we're going to work on
	var img = document.getElementById('id_of_your_img');
	
	// make sure the pic. exists before operation
	if(img){
	
		// get the source of the pic. (minus any previous appenditures)
		var src = img.getAttribute('src').replace(/\?time=.+$/i,"");
		
		// set the source of the pic. to the current src + timestamp (querystring)
		img.setAttribute('src',src +"?time="+timestamp.getSeconds());
	}
	
},1000);
__________________
Pro. Geek
http://www.mikeholloway.co.uk

Last edited by flyingbuddha : 08-28-2008 at 06:41 AM. Reason: Whoops, forgot to add the timestamp properly ;)
flyingbuddha is offline  
Reply With Quote
Old 08-28-2008, 05:52 PM   #5 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

I'm really not following this, hehe. So am I correct in saying that I put the script as the source of the image with the time details? but how does the script know where to get the images etc? This is my php:

PHP Code:
    <?php
        
        $getImage 
glob 'i/*.jpg' );
        
$randomImage $getImage[array_rand($getImage1)];
                    
        if ( isset( 
$_GET['num'] ) ) {
                        
            
$getNum       $_GET['num'];
            
            if ( !
is_numeric$getNum ) ){
                
                echo 
'<div id="image">Oops. Malformed URL! <br /> (Has to be a number...)</div>';
                
                exit();
                
            }
            
            echo 
'    <div id="image">
            <a href="http://laughomatic.net" ><img src="i/lom (' 
$getNum ').jpg" /></a>
            </div>
            
            <p><a id="refresh" href="http://laughomatic.net">More!</a></p>
            <p><input type="text" value="http://laughomatic.net/?num=' 
$getNum '" /></p>
            
            '
;
                        
        } else {
            
            echo 
'    <div id="image">
            <a href="javascript:this.location.reload();" ><img src="' 
$randomImage '" /></a>
            </div>
    
            <a id="refresh" href="javascript:this.location.reload();" >Next!</a>'
;
    
            
$pFile = (object) pathinfo($randomImage);
            echo 
'<p><input type="text" value="http://laughomatic.net/?num=' preg_replace('~[^\d]~i'''$pFile->filename) . '" /></p> ';
            
        }

    
?>
Sorry for being such a nuisance :(
Gareth is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


All times are GMT. The time now is 04:09 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design