How to check if number of rows = 6n-1 [closed] - php

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 8 years ago.
Improve this question
I wish to count a number of rows in my sql database, and if the number of rows equal 6n-1 perform a function, if not, do nothing.
I know I can specify the integers to look for, but there would be thousands, so I wish to find a way where I can just define the math 6n-1
for example, when I count the rows in a table, I want to check whether there is 5,11,17 and so on.
If the number of rows match, do something, else do nothing.
Since 5,11,17.... is equal to every 6th row starting from 5, this can be expressed as 6n-1.
IS there a way to do this without defining every integer to check?

($rowcount + 1) % 6 == 0
perhaps...

for ( $i=1 ; 6*$i-1 <= $database_rows ; $i++ ) {
if ( 6*$i-1 == $database_rows ) {
do_something();
}
}

Related

SUM OF NUMBERS IN WHILE LOOP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I'm trying to find the sum of numbers by querying a database using PHP and MYSQL. I need a while loop or any other solution that can add numbers that are >= 40 from a sequence of numbers. I'm only able to make the query but don't know how to go about the rest.
$selectMARK="SELECT mark FROM resultstbl WHERE mark >= 40";
$queryMARK=mysqli_query($dbCon, $select);
$sum=0;
while($rowz=mysqli_fetch_assoc($queryMARK))
{
// need some code to add the numbers here
}
You maybe need an
$iterator = 0;
outside the loop, that you can increment inside of it.
$sum += $rowz[$iterator];
$iterator++:
Then do your condition around it by checking $rowz[$iterator] <= 40

Which string(in query) are empty? [closed]

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 5 years ago.
Improve this question
So how to detect which string are empty in the query? I mean:
I have few WHERE clause in one query
$query = SELECT var FROM table WHERE var = '$y';
and how can I detect which "$y" has no result?
I know I can use if($y), but how can I detect which was empty?
Declare an array of your Y variables:
var $myYs =array($y1, $y2, ... $yn);
Then make a loop to count the query results of each of your Y. Then check if the count of that particular query was equal to 0, still inside the loop.
foreach ($myYs as $checkThsYnow){
$query = SELECT COUNT(var) FROM table WHERE var = '$checkThsYnow';
if ($query =0) {echo $checkThsYnow." is empty"}
}
Based on your comments additionally you need to check all combinations of your Ys.. as it is possible, that every Y brings back a result, but Y1 and Y2 AND together resulting the 0 results. Its a mathematical combination issue. Need to check all combinations..Loop in a loop in a loop..etc.

Optimized solution for a rolling dice code puzzle [closed]

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 8 years ago.
Improve this question
I was asked to provide the solution of this rolling dice problem in an interview. When I showed him my solution, he said that there is another way to find the solution. I am looking for answer in PHP script only.
Question:
Two persons are playing a game of rolling 2 dices. Each person rolls the two dices n times and records the outcomes (sum of value of two dices) of all the n attempts. So after n attempts of both the player we have two list corresponding to the n outcomes of two players.
They want to know that whether they have got all possible outcomes( 1 to 12) same number of times or not. If they got all the possible outcomes same number of times then they are called lucky otherwise unlucky.
Input: Two Integer Arrays (L1, L2) corresponding to outcomes of two players.
Output: Lucky or Unlucky depending on the case
My Answer:
<?php
function rollingdice($input1,$input2)
{
foreach($input1 as $k=>$a)
{
if(in_array($a,$input2))
{$p = array_search($a, $input2);
unset($input2[$p]);
}
else
{ return 'Unlucky';}
}
return 'Lucky';
}
?>
<?php
function rollingdice($input1,$input2)
{
$a = array_count_values($input1);
$b = array_count_values($input2);
if(array_diff_assoc($a,$b) || array_diff_assoc($b,$a)) { return 'Unlucky';}
return 'Lucky';
}

'IF' statement doesn't work right? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
When I print out the integers (1 or 0), all the iterations (there are 3 of them in this case) print out the correct number. First and the second one return 1, the last one returns zero. BUT, 'if' statement seems to display everything in it anyway. What am I doing wrong?
All the code below is inside a bigger 'for' loop.
$yn = 0;
if(!in_array($pos, $blocks)){
$blocks[$x] = $pos;
$x++;
$yn = 1;
}
print_r($blocks);
print "YN: ".$yn; # this prints out 1, 1 and 0 on the last iteration
if(yn){
# show some stuff (is displayed in all three iterations, but it shouldn't be on the last)
}
Try:
if($yn){
PHP is interpreting yn as a string, rather than as the variable you should be using.

Interviewstreet coding challenge input constraints [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
A question about interview street input constraints. (http://interviewstreet.com/)
Is it necessary to check the inputs for errors in the interviewstreet challenges?
For example, one challenge details the following constraints for the STDIN content:
1 <= N <= 1,00,000(10^5)
1 <= K <= N
0 <= profit value of any billboard <= 2,000,000,000(2*10^9)
Do I have to write some code to check the values to make sure that they meet these constraints or can I just assume that they do.
Also, if I do have to write the code to check what do I output if the inputs are incorrect?
Thanks
You can take it for graneted that inputs will always be adhering to the given constraints.
You don't need write any extra code for checking if input is within given constraints.
So if they say N will be <= 1,00,000, you can use an array of exactly 1,00,000 to store the elements and you'll be fine.

Categories