Jump to content


change content every 10 sec


15 replies to this topic

#1 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 13 June 2007 - 01:33 AM

Hi,
i have a form so users can upload photos ... data is stored in database. I am looking for a simple script that will show the data from database and it changes every 10 sec or so ...
I looked on google, hotscripts and i couldnt find anything usefull ...

best regards,
heki

#2 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 13 June 2007 - 10:02 AM

Do you mean randomly display different images everytime the site is accessed, or do you mean actually having it change only every 10 seconds, or do you mean both? I am guessing you want it to change every 10 seconds in the case of someone just sits on your site and in 10 seconds it will change the images. Am I correct?

For the random display every time someone accesses the site you would use something like this, and it would be along the lines of
$random = rand($selected = mysql_fetch_array($select));
!!Please dont quote me on this, I have no idea if this is correct. Just giving an example of what I think!!

For the 10 second thing, im guessing it would need something like m_uwait, but I don't know what would be needed.

Edited by Korndawg, 13 June 2007 - 10:03 AM.


#3 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 13 June 2007 - 11:02 AM

Fair attempt Korndawg, but no :P

You can populate the array normally and then shuffle it or manipulate it another way, or you can simply use RAND (this is relatively slow in comparison to the first method i mentioned but lets keep things simple).

Example:
$query = mysql_query('SELECT * FROM `table` ORDER BY RAND()') or die(mysql_error());
// LIMIT if you wish to...

For the 10 second "change" of content, well, you could use a javascript alternative but to be honest you could just use a refresh option (done, ironically using javascript, meta tags or since we're on the "php theme"....)

header('refresh: 5; url=' . $_SERVER['PHP_SELF']);

Hope that helps you put something together.

Matt

Edited by Matthew., 13 June 2007 - 11:03 AM.


#4 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 13 June 2007 - 10:11 PM

Eh I knew I should have kept my mouth shut. I was just bored earlier. Thanks for clearing it up though bud!

#5 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 14 June 2007 - 01:10 AM

hm,
i know about java, but it will be like a banner on friends page ... so only that banner needs to "refresh" every 10 sec or so ...
the problem with rand is that user has to refresh page to see the change ... do you think i can pull this of iwth a simple flash, php action on frame 1 and the flash keeps looping back to frame 1 after 10 sec, run the code and that way chagne the content?? sounds simple :P

thx for your replys

well swf solution doesnt work :S
i tried
header('refresh: 5; url=' . $_SERVER['PHP_SELF']);
works like i want yes, but in IE i get that stupid sound that page has been reloaded ...

Edited by Mr. Matt, 15 June 2007 - 05:21 AM.


#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 14 June 2007 - 12:05 PM

Well if you're keen on not having the full page reload, you'd obviously have to invest some time in understanding how AJAX works.

To simplify it for you, this would be the exact method you can use with the Prototype Framework.
Ajax.PeriodicalUpdater()

Simply connect it with a small PHP script that is randomly grabbing the data, and you're good to go.

#7 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 15 June 2007 - 01:52 AM

ill take a look at it ... thx man!

hm,
so i have a php function RANDOM LIMIT1 ... and if i run that script it shows me the stuff i need.
so do i just insert that script here? or show to that .php file?
i am a little lost

and where do i put this ajax in the end??

Edited by Mr. Matt, 15 June 2007 - 05:20 AM.


#8 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 15 June 2007 - 11:51 AM

Simple example.
Filenames are quite blunt, so you know their function.

homepage.php
<script type="text/javascript" language="javascript">
<!--
new Ajax.PeriodicalUpdater('image', 'ajax.php', {method: 'get', frequency: 10});
-->
</script>

<div id="image"><?php include 'ajax.php'; ?></div>
<noscript>You don't have javascript enabled/supported, so the image above won't rotate every ten seconds, sorry!</noscript>

ajax.php
// Database Connection Here

$query = mysql_query('SELECT * FROM `table` ORDER BY RAND() LIMIT 1') or die(mysql_error());
$result = mysql_fetch_array($query);

echo '<img src="'.$results['image_url'].'" alt="Random Image" />';


Notice how I also have the homepage.php include the ajax.php page into the div container. This is not only to show the very first image, but also as a back-up for those who might not have javascript enabled/supported, so they atleast get the first image. Also, the noscript tags will give them a message about it.

#9 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 15 June 2007 - 12:56 PM

man you are great!

#10 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 16 June 2007 - 10:12 AM

hm,
i guess we did something wrong, couse only include script works. ajax stuff doesnt show up!
<_<
any ideas?

#11 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 16 June 2007 - 10:59 AM

You'll have to give me more details than that.

Do you have the Prototype library file attached with the document? You can't use methods from an undefined object...

Is it giving errors, what is it doing.

If it simply 'isnt doing anything' and you have the Prototype framework attached, you'll have to either give m your source or simply point me to your page.

#12 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 17 June 2007 - 01:23 PM

Quote

Do you have the Prototype library file attached with the document?

.no. do i just attache it like external .js file? i guess this will fix the problem :)

#13 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 17 June 2007 - 04:09 PM

View PostHEki_, on Jun 17 2007, 06:23 PM, said:

Quote

Do you have the Prototype library file attached with the document?

.no. do i just attache it like external .js file? i guess this will fix the problem :)

Yes :D

#14 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 18 June 2007 - 01:38 AM

awww
it works pefectly in FF but in IE it only shows 1 image and keeps rotating that one ...
:wacko:
if i run a .php script which i use in IE and hit refresh button it does what it suppose to do.
But putting ajax to rotate the script ... Only loads 1 content in and rotats that 1 content ;)

#15 HEki_

    Young Padawan

  • Members
  • Pip
  • 165 posts

Posted 11 September 2007 - 04:40 AM

Hi,
i am back! Still trying to solve this problem and its getting on my nervers. Why? Ok, here is the thing ...
I have a php function:

$result = mysql_query("SELECT * FROM site WHERE st_status='1' ORDER BY RAND() LIMIT 1");

and than i use echo to show the info.

Than i use this
<script type="text/javascript" language="javascript">
<!--
new Ajax.PeriodicalUpdater('image', 'function.php', {method: 'get', frequency: 5});
-->
</script>

to update the info from sql ... Funny thing is that everything works in FireFox ... But in IE the first info that gets shown on the page, keeps rotating! It never changes the info send from sql!!
Also if i dont use LIMIT 1, than ajax shows all the images in database, but it never mix them up after ... In FireFox images do show on randomly ...

why is this??

#16 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 11 September 2007 - 06:11 PM

Could you give me a link? I'd like to examine the process with FireBug.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users