Show image while in background PHP script executes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • realin
    Contributor
    • Feb 2007
    • 254

    Show image while in background PHP script executes

    hiya guys,

    I have a question, i want to display the image which is above the page, while my script loads in the background.
    This is a simple script which check for new messages in my mailbox, so it is more like GMAIL's loading bar which is shown just before we land into our mailboxes.

    Guys, how do i do it ? Because i have tried ob_start and ob_end_flush, but with no luck i am unable to see the image before script terminates. The whole data is shown into browser in one go. How do i slice it ..

    The piece of code is like this ::

    [PHP]
    <?php
    require_once("l ib/POP3.php");
    include('lib/mimeDecode.php' );
    require_once(". ./config/config.inc.php" );

    echo '<img src="loader.gif " />';



    function gmail($em){
    $start=strrpos( $em,"&lt;");
    $end=strrpos($e m,"&gt;");
    $len=$start+4;
    $email=substr($ em,$len,$end-$len);
    return $email;
    }

    /*************** *** Function to parse message and splits the needful into parts *************** ****/
    function parse_output(&$ obj, &$parts){

    if(!empty($obj->parts)){
    for($i=0; $i<count($obj->parts); $i++)
    parse_output($o bj->parts[$i], $parts);

    }else{
    $ctype = $obj->ctype_primary. '/'.$obj->ctype_secondar y;
    switch($ctype){
    case 'text/plain':
    if(!empty($obj->disposition) AND $obj->disposition == 'attachment'){
    $parts['attachments'][] = $obj->body;
    }else{
    $parts['text'][] = $obj->body;
    }
    break;

    case 'text/html':
    if(!empty($obj->disposition) AND $obj->disposition == 'attachment'){
    $parts['attachments'][] = $obj->body;
    }else{
    $parts['html'][] = $obj->body;
    }
    break;

    default:

    $parts['attachments'][] = $obj->body;
    $parts['attachments']['name']=$obj->ctype_paramete rs['name'];
    }
    }
    }
    /*************** Variable Initialization ************/

    $user='realin@d omain.com';
    $pass='realin';
    $host='mail.dom ain.com';
    $port="110";
    $commid=(int)"6 ";
    /*@ var total number of messages
    in the inbox */
    $nTotal="";

    /* @var Messages which are
    being retrieved */
    $retr="";


    $my=array();
    $data=array();
    $obj=&new Net_POP3();

    $obj->connect("$host ","$port");
    $ret=$obj->login($user , $pass,'USER' );
    if(is_object($r et))
    echo "Invalid Username/Password";

    /********** Gets the office status of the mailbox, viz number of messages,size,U ID *************/
    $listing=$obj->getListing() ;



    foreach($listin g as $l){
    $id=$l["msg_id"];
    $head[$id]=$obj->getParsedHeade rs($id);
    $body[$id]=$obj->getMsg($id);
    }
    ?>
    [/PHP]
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    This is a JavaScript thing, something like this...

    [php]<?php

    //start sessions or whatever...

    //send headers.....

    echo "<script type=\"text/javascript\">do cument.write('< div id="loading">Lo ading...</div>')";

    //do your php processing....

    //and send the body tag like this..
    echo "<body onload=\"this.s tyle.display='' ; document.remove Child('loading' );\" style=\"display :none;\">[/php]

    Not tested, but this may work.

    Comment

    • realin
      Contributor
      • Feb 2007
      • 254

      #3
      Originally posted by hsriat
      This is a JavaScript thing, something like this...

      [php]<?php

      //start sessions or whatever...

      //send headers.....

      echo "<script type=\"text/javascript\">do cument.write('< div id="loading">Lo ading...</div>')";

      //do your php processing....

      //and send the body tag like this..
      echo "<body onload=\"this.s tyle.display='' ; document.remove Child('loading' );\" style=\"display :none;\">[/php]

      Not tested, but this may work.

      thanks buddy,

      gonna try now ..

      cheers !!

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Originally posted by realin
        thanks buddy,

        gonna try now ..

        cheers !!
        Do some changes if it didn't work, but I think this thing will work.

        Regards

        Comment

        • realin
          Contributor
          • Feb 2007
          • 254

          #5
          Originally posted by hsriat
          Not tested, but this may work.
          Din work for me mate :((

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Originally posted by realin
            Din work for me mate :((
            What did you try?

            Comment

            • rpnew
              New Member
              • Aug 2007
              • 189

              #7
              Originally posted by hsriat
              This is a JavaScript thing, something like this...

              [php]<?php

              //start sessions or whatever...

              //send headers.....

              echo "<script type=\"text/javascript\">do cument.write('< div id="loading">Lo ading...</div>')";

              //do your php processing....

              //and send the body tag like this..
              echo "<body onload=\"this.s tyle.display='' ; document.remove Child('loading' );\" style=\"display :none;\">[/php]

              Not tested, but this may work.
              Hi,
              Can you tell me how would you execute javascript if the PHP hasn't finished executing. As it hasn't finished executing PHP it wont appear even on client browser and as per my knowledge javascript will work on client side only and that too after the javascript code is loaded..

              Let me know if i'm wrong somewhere...

              Regards,
              RP

              Comment

              • rpnew
                New Member
                • Aug 2007
                • 189

                #8
                Hi,
                According to me you need to use Ajax for that.
                Load whole page with Ajax. Display whatever you want to display on page on loading and in backgraound call your PHP script and then when reply comes back replace/hide your image and load the result...
                And as per my knowledge even Google has used Ajax....

                Regards,
                RP

                Comment

                • realin
                  Contributor
                  • Feb 2007
                  • 254

                  #9
                  Originally posted by rpnew
                  Hi,
                  According to me you need to use Ajax for that.
                  Load whole page with Ajax. Display whatever you want to display on page on loading and in backgraound call your PHP script and then when reply comes back replace/hide your image and load the result...
                  And as per my knowledge even Google has used Ajax....

                  Regards,
                  RP

                  kewll..
                  that is the only option left now.. and you are right in saying taht first php will execute and then the compiled thing is returned to client i.e. browser thru RESPONSE headers and then JS comes into play

                  true :)

                  Comment

                  • rpnew
                    New Member
                    • Aug 2007
                    • 189

                    #10
                    Hi,
                    I came up with this solution.
                    AJAX+PHP Solution....
                    HTML page which calls the PHP script
                    PHPAjax.html
                    [HTML]
                    <html>
                    <head>
                    <script type="text/javascript">
                    document.write( '<div id="loading">Lo ading...</div>');
                    //Ajax Function
                    function getHTTPObject()
                    {
                    var xmlhttp;
                    if (window.ActiveX Object)
                    {
                    try
                    {
                    xmlhttp = new ActiveXObject(" Msxml2.XMLHTTP" );
                    }
                    catch (e)
                    {
                    try
                    {
                    xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
                    }
                    catch (E)
                    {
                    xmlhttp = false;
                    }
                    }
                    }
                    else
                    {
                    xmlhttp = false;
                    }
                    if (window.XMLHttp Request)
                    {
                    try
                    {
                    xmlhttp = new XMLHttpRequest( );
                    }
                    catch (e)
                    {
                    xmlhttp = false;
                    }
                    }
                    return xmlhttp;
                    }
                    //HTTP Objects..
                    var http = getHTTPObject() ;

                    //Function which we are calling...
                    function AjaxFunction()
                    {
                    url='PHPScript. php';
                    http.open("GET" ,url, true);
                    http.onreadysta techange = function()
                    {
                    if (http.readyStat e == 4)
                    {
                    //Change the text when result comes.....
                    document.getEle mentById("loadi ng").innerHTML= http.responseTe xt;
                    }
                    }
                    http.send(null) ;
                    }
                    </script>
                    </head>
                    <body onload="AjaxFun ction()">
                    </body>
                    </html>
                    [/HTML]

                    Background PHP Script
                    PHPScript.php
                    [PHP]
                    <?php
                    sleep(10);
                    echo "I'm from PHP Script";
                    ?>
                    [/PHP]

                    Save both the file in same directory. From browser execute the HTML file... It will show you 'loading...' at first wait for 10 seconds.. and you will see the message changing to "I'm from PHP Script".. if everything works fine....

                    Let me know if you find any problem or if it helps...

                    Regards,
                    RP

                    Comment

                    • rpnew
                      New Member
                      • Aug 2007
                      • 189

                      #11
                      And can anyone tell me.. what is wrong with code tags here on site..
                      I cant get any tag working... everything shows up as

                      [HTML]
                      something
                      [/HTML]
                      eventhough i'm using Mysql, PHP or HTML tags...
                      Anyways... Going home.. see you tomorrow... bbye...
                      Regards,
                      RP

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        Originally posted by rpnew
                        And can anyone tell me.. what is wrong with code tags here on site..
                        The highlighting software was causing problems and was removed.
                        I do believe they are searching for alternatives tho.

                        Comment

                        • hsriat
                          Recognized Expert Top Contributor
                          • Jan 2008
                          • 1653

                          #13
                          And gmail came back with a loading bar...
                          I was expecting that though....

                          Now how to make that?

                          Comment

                          • dlite922
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1586

                            #14
                            Originally posted by hsriat
                            And gmail came back with a loading bar...
                            I was expecting that though....

                            Now how to make that?
                            To me this is not possible because PHP comes back after the processing is done.

                            I guess you'd split the PHP calling into multiple and call each one with Ajax and just update the bar with each call. A very stupid way of doing it, but that would have that affect; it will most definetly not be a smooth bar though (like vista style)

                            The most common thing is having a looping ajax loader .gif image.

                            Comment

                            • hsriat
                              Recognized Expert Top Contributor
                              • Jan 2008
                              • 1653

                              #15
                              Well, I have another idea for realin, if he's interested.

                              [code=html]
                              <html>
                              <head><title>re alin</title>
                              <style type="text/css">
                              body {background-image:url('load ing_image.gif') ;}
                              </style>
                              </head>
                              <body onload="documen t.getElementByI d('body').style .display=''; this.style.back groundImage='no ne';">
                              <div id="body" style="display: none;">
                              <!--
                              body goes here
                              -->
                              </div>
                              </body>
                              </html>[/code]

                              Comment

                              Working...