Looking for a solution to have php refresh when new data hits database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjm
    Contributor
    • May 2007
    • 348

    Looking for a solution to have php refresh when new data hits database

    Hi everyone,

    I am looking for a php and maybe javascript solution that will show a particular php page when there is new data entered into a database table.

    So when a field in a table has new data inserted, maybe a javascript alert or something else could notify the user that there is new data inserted. Realtime I guess is what I am talking about. I wouldn't even know how to go about implementing this without a UNIX cron job. Cron is not really a solution because it should be in realtime.

    Can someone maybe tell me if this is at all possible?

    Thanks,

    Frank
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Frank.

    The simplest way I can think of to do this would be to log the date when the User checks for new entries. Then, all you have to do is display all entries whose date is after the last check date.

    Comment

    • fjm
      Contributor
      • May 2007
      • 348

      #3
      Originally posted by pbmods
      Heya, Frank.

      The simplest way I can think of to do this would be to log the date when the User checks for new entries. Then, all you have to do is display all entries whose date is after the last check date.
      Hey Pbmods!

      Thanks for the reply. Maybe I wasn't clear before but what would need to happen would be when user1 is working on any page of the website and another (completely seperate user-not known to user1) drops info to the db, is it possible for a javascript alert to alert user1 of the new data? The whole point would be so that user 1 *shouldn't have to* check for updates.

      In other words, so that user1 doesn't have to go back to a certain screen and check for new data. I'm sorry if I am not clear.

      Thanks again,

      Frank

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Frank.

        So what you're saying is that you would like to do a periodic check via AJAX to see if the database has been modified while the User is making changes. Is this correct?

        Comment

        • fjm
          Contributor
          • May 2007
          • 348

          #5
          Originally posted by pbmods
          Heya, Frank.

          So what you're saying is that you would like to do a periodic check via AJAX to see if the database has been modified while the User is making changes. Is this correct?
          Hey Pbmods..

          Well.. kind of... I guess we could put an example likr this.

          Say that user1 (the person that needs to know about the newly inserted data). Say this person is not even on his computer but talking on the telephone.

          While this telephone conversation is going on, User2 dumps info into the db at this point but user1 has no idea because he is still on the telephone.

          User1 *could* however go to that page that would tell him that new data has been inserted but I need a solution that will notify user1. Be it a bell, whistle, hammer on the head, something :) Something to alert user 1 that new data is there.

          Thanks,

          Frank

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Frank.

            Ok. So you want the User to get some kind of a notification outside of the context of your website. Perhaps you want your site to send out an email?

            The easy way to do this would be to add your mailer to the end of any script that modifies the database.

            Comment

            • ronnil
              Recognized Expert New Member
              • Jun 2007
              • 134

              #7
              Hi Frank

              I'd have to go with pbmods. Use Ajax (that's what the others use). Every 10 seconds or so you send a request to a specific page, if the response is not empty, new info must have been dropped.

              This works through Javascript and therefore makes it very easy notifiyng the user with an alert box or a pop-up (or whatever)

              may i suggest you take a look at http://www.w3schools.com/ajax/default.asp

              Comment

              • jx2
                New Member
                • Feb 2007
                • 228

                #8
                well as the guys above said there is no real solution (i wish there was)
                but you can work around this limitation with ajax - well its ok but thats not really solution if you want it to be done in real tiime - ajax can send requests every second or even more often then that (for me one second is too much)
                and if you have many users server will have to deal with many requests - its very limiting

                my idea (wich i am trying right now for my litle game)is to use infinite loop in php
                [php]<?php
                while(1){
                checkDB();
                sleep(1);
                }
                function checkDB(){

                $file = file('d.txt');
                if($file[0]){
                echo $file[0];
                exit;
                }
                }[/php]

                yeah i know its still have limitation maximum execution time :-)

                well if you use ajax togheter with it your and up with ajax requesting your server every $MAX_EXECUTION_ TIME (usualy 30sec) and you will gain really REAL time control

                i should copyright this technology ;-) its working wonderfully

                regards
                jx2

                Comment

                • fjm
                  Contributor
                  • May 2007
                  • 348

                  #9
                  Originally posted by ronnil
                  Hi Frank

                  I'd have to go with pbmods. Use Ajax (that's what the others use). Every 10 seconds or so you send a request to a specific page, if the response is not empty, new info must have been dropped.

                  This works through Javascript and therefore makes it very easy notifiyng the user with an alert box or a pop-up (or whatever)

                  may i suggest you take a look at http://www.w3schools.com/ajax/default.asp
                  Hi Ronnil,

                  Thanks for the reply. Honestly, I wish I had the time to learn AJAX but it isn't an option for me. I can however look aroind and see if I can find someone that has already created something like this.

                  What do you guys think about the infinite loop provided by jx2? Seems easy.. :) Very very tempting....

                  Comment

                  • fjm
                    Contributor
                    • May 2007
                    • 348

                    #10
                    Originally posted by jx2
                    well as the guys above said there is no real solution (i wish there was)
                    but you can work around this limitation with ajax - well its ok but thats not really solution if you want it to be done in real tiime - ajax can send requests every second or even more often then that (for me one second is too much)
                    and if you have many users server will have to deal with many requests - its very limiting

                    my idea (wich i am trying right now for my litle game)is to use infinite loop in php
                    [php]<?php
                    while(1){
                    checkDB();
                    sleep(1);
                    }
                    function checkDB(){

                    $file = file('d.txt');
                    if($file[0]){
                    echo $file[0];
                    exit;
                    }
                    }[/php]

                    yeah i know its still have limitation maximum execution time :-)

                    well if you use ajax togheter with it your and up with ajax requesting your server every $MAX_EXECUTION_ TIME (usualy 30sec) and you will gain really REAL time control

                    i should copyright this technology ;-) its working wonderfully

                    regards
                    jx2
                    Jx2,

                    Thanks for the snippet.. I never thought about using an infinate loop. :) My solution doesn't have to provide true "real time" but has to provide something that would alert or notify the computer user when there has been new data inserted into the database within a reasonable amount of time. Say between 1 and 2 minutes.

                    Thanks!

                    Frank

                    Comment

                    • fjm
                      Contributor
                      • May 2007
                      • 348

                      #11
                      Originally posted by pbmods
                      Heya, Frank.

                      Ok. So you want the User to get some kind of a notification outside of the context of your website. Perhaps you want your site to send out an email?

                      The easy way to do this would be to add your mailer to the end of any script that modifies the database.
                      Hey Pbmods...

                      When I initially read this reply, it didn't make sense to me but now a little lightbulb went on..... Are you saying to *simply* put at the end of my php script, something that would, say, in javascript, pop up on the screen after the data has been inserted??????

                      Beautiful man!!!!!!!!!!!! !!!!!!! You guys rock! BTW: I'm sooooo embarrased... :blush:

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Heya, Frank.

                        We aim to please.

                        Good luck with your project, and as always, if you ever need anything, post back anytime :)

                        Comment

                        • fjm
                          Contributor
                          • May 2007
                          • 348

                          #13
                          Originally posted by pbmods
                          Heya, Frank.

                          We aim to please.

                          Good luck with your project, and as always, if you ever need anything, post back anytime :)
                          Hey Pbmods.. Thanks again and you can bet I will be back. ;)

                          Comment

                          • ronnil
                            Recognized Expert New Member
                            • Jun 2007
                            • 134

                            #14
                            Originally posted by fjm
                            Jx2,

                            Thanks for the snippet.. I never thought about using an infinate loop. :) My solution doesn't have to provide true "real time" but has to provide something that would alert or notify the computer user when there has been new data inserted into the database within a reasonable amount of time. Say between 1 and 2 minutes.

                            Thanks!

                            Frank
                            Yep that is infact a pretty good solution. Haven't needed this type of thing for an instant messenger or anything, but when i do.... hope you don't mind me borrowing your idea ;)

                            Comment

                            • fjm
                              Contributor
                              • May 2007
                              • 348

                              #15
                              Originally posted by ronnil
                              Yep that is infact a pretty good solution. Haven't needed this type of thing for an instant messenger or anything, but when i do.... hope you don't mind me borrowing your idea ;)
                              Hey Ronnil,

                              I liked that idea too. I also liked Pbmods idea as well. After all it seemed so easy. I am thinking about how to implement Pbmods javascript solution into my project but it is dawning on me that it is not going to be possible.

                              Consider this...

                              My server is a UNIX box that is off site. The person using the php program is on a windows machine; we will call him User1. When another user (User2) inserts data to the database how would it be possible to use javascript which we all know is a client side language to alert User1 on a completely different machine?

                              I really don't care one way or the other as long as I can come up with a solution. If javascript is not possible, I am wondering how I could implement an infinate loop as Jx2 suggested.

                              Comment

                              Working...