I have a problem with my script im using. the if $addcount is not working. it continues to fire the insertMessage function when i dont want it to. Any ideas why?
if(($result->data != '') && ($result->data != null)) {
$wallCount = 0;
$addCount = 0;
foreach($result->data as $thread){
$wallCount++;
$fromId = $thread->from->id;
$message = $thread;
if($fromId == $user['Id']){
if($addCount < 100) {
insertMessage($user['Id'], $message, $user['Num']);
$addCount++;
sleep(1);
}
}
}
}
From your script, it will call insertMessage 100 times (0-99). Before the if statement, echo the counter to see what it is doing:
...
echo $addCount."<br />";
if($addCount < 100) {
...
Related
I have 31 rows in my DB and I want to load them with 2 seconds interval. But when sleep function is live for 60 seconds, main function repeats and $count resets.
My code:
$count = 0;
$files_count = count($files);
foreach($files as $file) {
$count++;
$bot_file_name = $file['bot_file_name'];
$bot_file_tg_id = $file['bot_file_tg_id'];
$bot_file_format = $file['bot_file_format'];
if ($bot_file_format == "pdf") {
// do something
} else if ($bot_file_format == "video") {
// do something
} else if ($bot_file_format == "audio") {
// do something
}
if ($count == $files_count) {
break;
}
sleep(2);
}
I can't understand what is wrong here.
php max_execution_time is 120.
I have one issue that I can't solve. So I have for loop.
So here is little code:
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($sms->mobio_check($servID,$request->input("code$int"))) {
continue;
$cart->success($product->id,$product->server->name);
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
I want if three ifs return true to run function $sms->success();.
What is wrong here?
You could rely on the fact that if the loop finished, then it's OK, any failures will cause the return in the loop to exit...
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if( ! $sms->mobio_check($servID,$request->input("code$int"))) {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
$cart->success($product->id,$product->server->name);
You can do it like this:
$success = true;
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($success = $success || $sms->mobio_check($servID,$request->input("code$int"))) {
continue;
$cart->success($product->id,$product->server->name);
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
if ($success) $sms->success();
you can try this
$count = 0;
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($sms->mobio_check($servID,$request->input("code$int"))) {
$count++;
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
if($count == 3)
$cart->success($product->id,$product->server->name);
This is the problem I'm having with code:
I have a function that creates an updating bar;
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);
this loop works when user make a "search" on the site for "all" countries;
BUT user can also make a "search" only choosing 1-5 different countries;
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.'% </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.'% </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% </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>");
};
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.'% </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.'% </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% </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;
};
I'm using a include file to translate my text. it works pretty good, but now I need to on button click translate some mor words, and return doesn't work anymore, but echo does.
so what I'm searching is a way of know if return is possible or not, code example
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
return $traducao_1[$i];
}
if($lingua == 2) {
return $traducao_2[$i];
}
}
}
this one works good first time page is executed, since this is included file.
how to make this?
if(!return $traducao_1[$i]) {
thanks
ok, tryed to answer, but always got an error, so I'm editing this as answer
Thank you all for help, I manage a way of make it work like adding one action to the function and checking if action == , then do something, like this
function test($palavra, $lingua, $accao) {
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
if($accao != "2_chamada") {
return $traducao_1[$i];
} else {
echo $traducao_1[$i];
}
}
}
}
}
Again, thanks for help
maybe check to see if it's set first before returning
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
$return = $traducao_1[$i];
if($return != false) {
return $return;
} else {
echo 'something';
}
}
if($lingua == 2) {
$return = $traducao_2[$i];
if($return != '') {
return $return;
}
}
}
}
return can only be used in functions
i reccommend this for you
function test($palavras,$palavra,$traducao_1,$traducao_2){
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
return $traducao_1[$i];
}
if($lingua == 2) {
return $traducao_2[$i];
}
}
}
}
if(!test($palavras)) {
I have the following code:
//generate 10 top tags
$tagSQL = mysql_fetch_array(mysql_query("SELECT * FROM tags"));
$topArray = array();
foreach($tagSQL as $poland)
{
if($poland["tagID"] == 1)
{
$topArray[0] ++;
}
if($poland["tagID"] == 2)
{
$topArray[1] ++;
}
if($poland["tagID"] == 3)
{
$topArray[2] ++;
}
if($poland["tagID"] == 4)
{
$topArray[3] ++;
}
}
function printTopTags()
{
$n = 0;
foreach($topArray as $buddha)
{
$n = $n + 1;
if(sizeOf($topArray) > $n)
{
$hersheyBar = " ";
}
else
{
$hersheyBar = "";
}
$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'"));
foreach($finalFinalEndArray as $waterBottle)
{
echo $waterBottle . $hersheyBar;
}
}
}
I get the error Warning: Invalid argument supplied for foreach() on line 93
Line 93 is foreach($topArray as $buddha).
Any help?
http://ru.php.net/manual/en/language.variables.scope.php
Also
if($poland["tagID"] == 1)
{
$topArray[0] ++;
}
if($poland["tagID"] == 2)
{
$topArray[1] ++;
}
if($poland["tagID"] == 3)
{
$topArray[2] ++;
}
if($poland["tagID"] == 4)
{
$topArray[3] ++;
}
===
if ($poland["tagID"] >= 1 && $poland["tagID"] <= 4)
$topArray[$poland["tagID"] - 1]++;
$tagSQL = mysql_fetch_array(mysql_query("SELECT * FROM tags"));
This is very bad practice. If the query fails for any reason whatsoever, mysql_query returns boolean FALSE, which you then blindly pass to mysql_fetch_array, which will then fail in turn because it's expect a mysql result handle, not a boolean, and return a boolean itself.
You then use all this failed data in a foreach loop, and wonder why you're not getting anything but errors?
Looks like $topArray just not defined in printTopTags() function.
You can pass it as a parameter:
function printTopTags($topArray) {
...
}
$topArray is a global variable.
To use it inside a function you either have to pass it as a parameter or use the global keyword to import it:
function printTopTags()
{
global $topArray; // <---- Here!
$n = 0;
foreach($topArray as $buddha)