dounei5721 2015-07-19 18:24
浏览 45

PHP问题与一组函数发送和工作var一个到另一个

This is the problem I'm having with code:

  1. I have a function that creates an updating bar;
  2. I have a function that creates a loop, each number of loop makes some other stuffs and the same number represent the progress of the bar (from 1 to 155);
  3. this loop works when user make a "search" on the site for "all" countries;
  4. BUT user can also make a "search" only choosing 1-5 different countries;
  5. this way we have a function that makes the PROGRESS BAR, another one that makes the PROGRESS PART of the BAR that should understand how to progress.

So the point is that I don't know how make the function for progress bar to interpret the situation in order to know that user choosed only some countries and not "all" countries (the option will post to the function some "numbers of the loop" like "3", "4", "56"... corresponding to choosed country) - and this is the easy part (with maybe isset($var)).

The next hard part is that after that it will be called the PROGRESS BAR function with those "country-numbers" so that if you choose "Canada-USA-France" the numbers will be "34-12-45" and the progress bar will write "34%-12%-45%". But it's incorrect because if you have 3 options choosed, it should be 33%-66%-99% (and not 34%-12%-45%)...

What I don't know is how to make that function understand that everytime the function receive the "country-number", and it comes from user choice different from "all", the function should get that number and modify it (and this can be made with an array) in order to adapt it to the correct bar progression so that if you have: 34, than 12 than 45, the function should understand that the 1st time 34 should become 33, the 2nd time 12 should become 66 and the 3rd time 45 should become 99 (or 100)...

Here is the code part of interest:

1. receiving _POST and verifying it:

   function getData() {
     $a = strtolower($_POST["CountryOne"]);
     $b = strtolower($_POST["CountryTwo"]);
     $c = strtolower($_POST["CountryThree"]);
     $d = strtolower($_POST["CountryFour"]);
     $d = strtolower($_POST["CountryFive"]);

     $numPost = count($_POST);
     if ($numPost == 0) {
        echo("<p class='Verify'>Select an option to start search...<br></p>");
     } else { 
        echo "<div class='Verify' id='progressbar' style='width:620px;height:16px'></div>
    <div class='VerifyBar' id='information' style='width'></div>";
     };

2. Jump to next part after checking it user choosed "all" or "some countries" - here is the loop:

  function loopNum($x) {
    $i = 0+$x;
    $y = $x+3;
    for ($x = $i; $x <= $y; $x++) {
    $iscountryID = ("$country".$x."");  
    createUrl($iscountryID);
  } if($y != 155) {
    return loopNum($x);
  } else if($y < 155) {
    echo("<font size='2' face='Tahoma, Geneva, sans-serif' style='font-variant: small-caps' color='#FF0000'><i>ERROR: unxepected data extraction interruption</font></i><br>");
  } else {
    echo("<font size='2' face='Tahoma, Geneva, sans-serif' style='font-variant: small-caps' color='#00CC00'><i>FINISHED: data extraction ended</font></i><br>");
  }};

3. than it makes a lot of other stuff and call progress bar function

 function completeBar($iscountryID) {

 //***** PART UNDER CONSTRUCTION WHERE I NEED HELP *****    
    $countryOne = strtolower($_POST["CountryOne"]);
    $countryTwo = strtolower($_POST["CountryTwo"]);
    $countryThree = strtolower($_POST["CountryThree"]);
    $countryFour = strtolower($_POST["CountryFour"]);
    $countryFive = strtolower($_POST["CountryFive"]);

    if (($countryOne !== 'all') and ($countryTwo !== 'all') and ($countryThree !== 'all') and ($countryFour !== 'all') and ($countryFive !== 'all')) {

    $numQuery = array("$countryOne","$countryTwo","$countryThree","$countryFour","$countryFive");

    foreach ($numQuery as $value) {
        if(is_numeric($value)) {    
            $j++;
            }}; // AT THIS POINT I WOULD KNOW HOW MANY VALUES IN THE ARREY ARE DIFFERENT FROM "ALL" AND "NO_COUNTRY" OPTIONS AND SO ARE NUMERIC VALUES

  //***** FROM HERE I DUNNO HOW TO PRECEED *****        

    } else { //the next part works with "all" option choosed    
       if ($iscountryID < 155) {
          $i = $iscountryID;

          $percent = $i."%";
          $pxbar = 4*$i."px";

          $percentage = round((($pxbar*100)/624),0); //bar is 624px long

          // Javascript for updating the progress bar and information
          echo '<script language="javascript">
          document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'%&nbsp;</div>";
          document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';

         // This is for the buffer achieve the minimum size in order to flush data
         echo str_repeat(' ',1024*64);

         // Send output to browser immediately
         flush();
         sleep(1); 
   } else if ($iscountryID == 155) { //so on loop complete 
         $i = $iscountryID; 
         $percent = $i."%";
         $pxbar = 4*$i."px";
         $percentage = round((($pxbar*100)/624),0);

         echo '<script language="javascript">
         document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'%&nbsp;</div>";
         document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';

         echo str_repeat(' ',1024*64);
         flush();
         sleep(1); 
         // Tell user that the process is completed
         echo '<script language="javascript">
         document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">100%&nbsp;</div>";
         document.getElementById("information").innerHTML="<div>Process completed, all countries verified...</div>";

         document.getElementById("information").style.color="green";
         </script>';
         echo str_repeat(' ',1024*64);
         flush();
         sleep(1);
      }
  }};   

Ah... I've tried to make the following working but the problem is that with the following, I'll have the function updating the bar getting values from the array too fast. Using the VAR sent from the rest of the file, I'll have an updating bar that follows the updating output of the server.

 function partialBar() {
    $countryOne = strtolower($_POST["CountryOne"]);
    $countryTwo = strtolower($_POST["CountryTwo"]);
    $countryThree = strtolower($_POST["CountryThree"]);
    $countryFour = strtolower($_POST["CountryFour"]);
    $countryFive = strtolower($_POST["CountryFive"]);

    $numQuery = array("$countryOne","$countryTwo","$countryThree","$countryFour","$countryFive");

    foreach ($numQuery as $value) {
        if(is_numeric($value)) {    
            $j++;
            };
        };

    if ($j == 1) {
        $iscountryID = 155;
        completeBar($iscountryID); 
    } else if ($j == 2) {
        $iscountryID = array("78","155"); 
        completeBar($iscountryID[0]); 
        completeBar($iscountryID[1]); 
    } else if ($j == 3) {
        $iscountryID = array("52","104","155"); 
        completeBar($iscountryID[0]);
        completeBar($iscountryID[1]);
        completeBar($iscountryID[2]); 
    } else if ($j == 4) {
        $iscountryID = array("39","78","117","155"); 
        completeBar($iscountryID[0]);
        completeBar($iscountryID[1]);
        completeBar($iscountryID[2]);
        completeBar($iscountryID[3]);
    } else if ($j == 5) {
        $iscountryID = array("31","62","93","124","155"); //these numbers are the corresponding loop number in other to get 20%-40%-60%-80%-100% from the completeBar() function 
        completeBar($iscountryID[0]);
        completeBar($iscountryID[1]);
        completeBar($iscountryID[2]);
        completeBar($iscountryID[3]);
        completeBar($iscountryID[4]);
    } else echo ("<div class='Verify'>Something went wrong!</div>");
 }; 
  • 写回答

1条回答 默认 最新

  • douchi0028 2015-07-19 23:48
    关注

    Here is solution:

    make a counter in the central function in order to know how many times have been called, so you know which number of the array you have to call for the correct calculation of the % of progress bar.

    Here is where the following functions are called:

     if (($countryNameTitOne == 'all') or ($countryNameTitTwo == 'all') 
     or ($countryNameTitThree == 'all') or ($countryNameTitFour == 'all') 
     or ($countryNameTitFive == 'all')) {
     completeBar($iscountryID); 
     } else {
     $callCounter = callCounterFunc();
     partialbar($iscountryID,$callCounter);
     }};
    

    Than here are the functions called:

     function completeBar($iscountryID) {
       if ($iscountryID < 155) {
       $i = $iscountryID;
    
       $percent = $i."%";
       $pxbar = 4*$i."px";
       $percentage = round((($pxbar*100)/624),0);
       echo '<script language="javascript">
       document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'%&nbsp;</div>";
       document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';
       echo str_repeat(' ',1024*64);
       flush();
       sleep(1); 
       } else if ($iscountryID == 155) { 
       $i = $iscountryID;
       $percent = $i."%";
       $pxbar = 4*$i."px";  
       $percentage = round((($pxbar*100)/624),0);
       echo '<script language="javascript">
       document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">'.$percentage.'%&nbsp;</div>";
       document.getElementById("information").innerHTML="'.$i.'/155 country(s) processed... loading your data, hold on..."</script>';
       echo str_repeat(' ',1024*64);
       flush();
       sleep(1); 
       echo '<script language="javascript">
       document.getElementById("progressbar").innerHTML="<div style=\"width:'.$pxbar.';background-color:#ddd;\">100%&nbsp;</div>";
       document.getElementById("information").innerHTML="<div>Process completed, all countries verified...</div>";
    
       document.getElementById("information").style.color="green";
       </script>';
    
       echo str_repeat(' ',1024*64);
       flush();
       sleep(1);
       }
     }; 
    
    
     function partialBar($iscountryID,$callCounter) {
    
        $countryOne = strtolower($_POST["CountryOne"]);
        $countryTwo = strtolower($_POST["CountryTwo"]);
        $countryThree = strtolower($_POST["CountryThree"]);
        $countryFour = strtolower($_POST["CountryFour"]);
        $countryFive = strtolower($_POST["CountryFive"]);
    
        $numQuery = array("$countryOne","$countryTwo","$countryThree","$countryFour","$countryFive");
    
        foreach ($numQuery as $value) {
            if(is_numeric($value)) {    
                $j++;
            }};
    
        if ($j == 1) {                              
            if ($callCounter == 1) {        
                $iscountryID = 155;
                completeBar($iscountryID);
                };
        } else if ($j == 2) {
            $iscountryID = array("78","155"); 
                if ($callCounter == 1) {
                    completeBar($iscountryID[0]);
                } else if ($callCounter == 2) {
                    completeBar($iscountryID[1]); 
                };
        } else if ($j == 3) {
            $iscountryID = array("52","104","155"); 
                if ($callCounter == 1) {
                    completeBar($iscountryID[0]);
                } else if ($callCounter == 2) {
                    completeBar($iscountryID[1]);
                } else if ($callCounter == 3) {
                    completeBar($iscountryID[2]);
                };
        } else if ($j == 4) {
            $iscountryID = array("39","78","117","155"); 
                if ($callCounter == 1) {
                    completeBar($iscountryID[0]);
                } else if ($callCounter == 2) {
                    completeBar($iscountryID[1]);
                } else if ($callCounter == 3) {
                    completeBar($iscountryID[2]);
                } else if ($callCounter == 4) { 
                    completeBar($iscountryID[3]);
                }; 
        } else if ($j == 5) {
            $iscountryID = array("31","62","93","124","155"); 
                if ($callCounter == 1) {
                    completeBar($iscountryID[0]);
                } else if ($callCounter == 2) {
                    completeBar($iscountryID[1]);
                } else if ($callCounter == 3) {
                    completeBar($iscountryID[2]);
                } else if ($callCounter == 4) { 
                    completeBar($iscountryID[3]);
                } else if ($callCounter == 5) {
                    completeBar($iscountryID[4]);
                }; 
        } else echo ("<div class='Verify'>Something went wrong!</div>");
     }; 
    
     function callCounterFunc() {
     static $calls = 0;
     ++$calls;
     return $calls;
     };
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程