Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How do I skip a loop and go to the next one? Lets say my query in a loop returns result 'Nothing', I want to skip updating the base and go to the next loop. If I have 7 loops and my 5th loop returns 'Nothing' from my base, skip it don't do nothing beyond that point and just go to the next, 6th loop.
I have a for loop like so:
for((int) $i = 0; $i < $number_of_updates; $i++)
{
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
if ($name = 'Nothing')
{
go to next loop;
}
updating my database and stuff;
}
EDIT: I have a $_SESSION variable at the end that stores all of my loops and displays them. I also don't want results with 'Nothing' to be stored there.
continue will end the current iteration and start the next one:
if ($name == 'Nothing')
// -------^^ Make sure you're using double (==).
{
continue;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
$vaccinesArr=array("0","0","0","0","0","0","0","0","0","0","0");
for($i = 0; $i < 11 ; $i++){
while($row = mysqli_fetch_assoc($result)){
if ($row['vaccID']== $i){
$vaccineArr[$i]++;
}
}
}
This is the part of my code, I need to increment according to this condition but I keep on getting syntax error, I searched but all the results I find are using foreach and incrementing all the indexes of the array while here I want to increment only one specific index. Can anyone help?
Your code will only work on the first iteration of the for loop, because when the inner while loop finishes there won't be any more rows to fetch from the query. You could rewind the query, but a better way to do it would be without the for loop.
while ($row = mysqli_fetch_assoc($result)) {
$vaccinesArr[$row['vaccID']]++;
}
Also, if the elements of $vaccinesArr are supposed to be numbers, you shouldn't put them in quotes. PHP will convert the strings to numbers when you use ++, but why be confusing?
$vaccinesArr = array(0, 0, 0, ....);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
so I am trying to check if a string contains a specific set of words, and set a variable if they do. I have the code below, first it was working great except that it would not work if one of the key words was the very first word, so I looked at the php manual and found out about === and implemented that, but now it sets the variable to one every time even if none of the words are found!
So basically it reads a text file to an array, the text file contains the key words, then it checks the string to see if any of those key words are present in the sting. If none of the key words are found then wc would equal 0 and so would inc. If it finds any then wc is incremented every time a word is found, and if it is greater than 0 it will set inc to 1 to flag that key words were included.
Hopefully that all makes sense....
Here is my code:
$inc = 0;
$list = file("filter.txt", FILE_IGNORE_NEW_LINES);
$cnt = count($list);
$wc = 0;
for ($i=0; $i<$cnt; $i++)
{
if (strpos($string,$list[$i]) === false)
{
$wc ++;
}
if ($wc > 0)
{
$inc = 1;
}
}
It doesn't work because you are increasing wc if the string is not found.
You have to replace === with !==.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here I am rephrasing the question. Coz some of the people couldnt understand it and started giving negative votes
I have the following PHP code:
$count = 100000;
$array = array();
for($i=0,$i<=$count,$i++)
{
array_push($array,mt_rand(1111111111111111,9999999999999999));
}
As per current code the time to execute this code will increase as I increase $count.
I am looking for a solution / algo / technique by which I can keep the time to execute in seconds no matter whatever is the value for $count. I am running this on 8 cores cpu, I am OK changing language but not ok upgrading hardware.
This should give you approximately constant time for values of $count < $maxcount. However, I wouldn't ever do this - it is just timewasting.
$maxcount = 100000
$count = 100000;
$array = array();
// Create required array
for($i=0,$i<=$count,$i++)
{
array_push($array,mt_rand(1111111111111111,9999999999999999));
}
// Pad out time to $maxcount iterations, to keep time constant
for($i=$count,$i<=$maxcount,$i++)
{
$dummy = mt_rand(1111111111111111,9999999999999999);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there any way of looping through an array and return each result as it is calculated, rather than waiting until all values are calculated before they are all displayed?
Say I have an array with numbers from 1 to 10 in it, is it possible to loop through the array and return 1, then 2, then 3, then 4, then 5, etc. instead of returning 12345678910 all in one go? Obviously I have simplified this request, but basically my loop calls a function that has a 1 second timeout, so for the user to wait 10+ seconds for all results to be returned rather than a result being returned each second is far less annoying.
Yes, new PHP 5.5 provides such a thing as generators.
function one_to_ten() {
for ($i = 1; $i <= 10; $i++) {
yield $i; //It is like a return, but can be done several times.
}
}
$generator = one_to_ten(); //The loop is not executed yet. We just created the generator.
/* Some big code here */
foreach ($generator as $number){//Only here the iteration starts.
echo "{$number} <br/>" ;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a php project where I am trying to loop through an array using a foreach. However, sometimes the array may only contain 1 item so when I try and do a foreach it fails as 1 item is just seen as a normal variable.
Is there a way that I can trick php into thinking that a 1 item array is actually an array and not just a variable so that I don't get this error.
Thanks for your help.
foreach will work fine with arrays of size 0,1 or bigger. I suspect your problem is, that the variable doesn't really contain an array, but some scalar value - in this case use something like
if (!is_array($var)) $var=array($var);
foreach ($var as $item) {
//...
}
if(is_array($arr))
$arr2=$arr
else
$arr2=array($arr)
and then you iterate over $arr2
I would recommend just using a standard for loop. It should work no matter what the length of the array is
for($i = 0, $l = count($myArray); $i < $l; $i+=1){
//code in here
}
But chances are you have an issue with your array to begin. Posting the structure would be helpful, or you should var_dump it to make sure it is indeed an array.