i am going to divide a sql query result into two parts. the reason why i want to do this is that i just want my server handle first only 20 items and then after sleep the rest part.
how is it possible in sql to part the query into multiple queries after mysql_query so that i can process the parts separately one after another?
my vision is this:
if(mysql_row_nums($sql)>20){
handle only 20 items
} else {
handle all
}
thanks for reading.
you can do this by setting limit 0,20 in query
Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results.
Check out the SQL LIMIT argument:
LIMIT
try
if(mysql_num_rows($sql)>20){
$i = 0;
while($result = mysql_fetch_array($sql)){
$i++;
if($i <= 20){
// do some thing with first 20 result
var_dump($result);
}
if($i == 20){
// if we have arrived to result #20 stop excuting script for minute
sleep(60); // 60 = 1 minute ;
}
if($i > 20){
// continue dealing with result after first 20 result.
}
}
}
Related
So I'm having a small problem with a piece of my code, I have a system that is adding checkpoints to a program and I want it to add a checkpoint every 14 mins, but I also want to limit it to only add x amount of checkpoints based on how many hours a course as. So for an example, the first course has 12 hours so I only want 12 checkpoints created, but it's creating 16 based on the $time_in_secs, so that variable will always be different as its check the time between the start and end date of the program. For the second program, it has 24 hours but its creating 33 checkpoints and so on and so forth. $checkpoint_limit is what I want to limit the for loop by, but I still need it add the 840 as that is the time in seconds and I need a checkpoint created every 14 mins
I have done a few different things but none of them seemed to work. Tried setting a min() also tried doing an if but also does not seem to work.
$checkpoint_limit = (abs($numHours) * 3);
//840 = 14 minute interval * 60
for ($i = 840; $i < $time_in_secs; $i += 840) {
//Code here that adds the checkpoints
}
In for loop check for no of checkpoints created and if they exceed $checkpoint_limit then break the loop.
Try:
$checkpoint_limit = (abs($numHours) * 3);
$no_of_checkpoints = 0;
//840 = 14 minute interval * 60
for ($i = 840; $i < $time_in_secs; $i += 840) {
//Code here that adds the checkpoints
$no_of_checkpoints++;
if($no_of_checkpoints > $checkpoint_limit){
break;
}
}
Use a second condition inside for loop with && operator.For example;
$j=0
for($i=840;$i<$timeInSecs && $j<12;$i +=840)
{
//example code here
$j++
}
I am sorry for the low quality answer i am using SO mobile and could not manage to post the code.So j variable is like a checkpoint time.You have added 14 minute checkpoint and what i get is u want it to loop for a specific count you can do it with that way.And you should increase $j in the end of the for loop.
How to burst the following rows into 3 groups where the sum of "dollars" is 10. All rows must be used and none more than once.
row|dollars
1|1
2|1
3|1
4|1
5|1
6|1
7|3
8|4
9|7
10|10
One (of many possible) desired outcomes would be...
Row Group 1 = 10
Row Group 2 = 7,9
Row Group 3 = 1,2,3,4,5,6,8
Extra Credit:
When it's not mathematically possible to get a sum of exactly $10 in each group, is there a formula for getting us closest to that?
I though maybe " HAVING sum(dollar) = 10 " or, for a close solution, just sorting and distributing one row to one group, but that didn't get me close.
Group Row By Sum of Specific Column equal to Specific Value sort of touches on this, but, assuming their could be millions of rows, there would be performance issues.
I'm stumped.
If it helps, I'm using php and mysql. A sql-only solution would be ideal, but some combination would be great too. Thanks for any suggestions.
Edit: If I wasn't clear, I want to return the rows that make this possible, not just "group by" them.
I don't think you could do it with just sql, but it could certainly help out. I'd start by having a query like select * from data where dollars <= 10 order by dollars desc and then make an algorithm in php that went through the results and added up dollars until it found three sets that add up to 10.
It would start out adding from larger to smaller until it found a correct sum, then store it, remove those items from the list and start again, three times. I'm on my phone, but will update the answer with a working example when I get to my computer.
EDIT: got to my computer. This is very messy code, but it should lead you in the right direction.
$dataset = [10,7,4,3,1,1,1,1,1,1];
$results = [];
function add_to_ten(&$data) {
$sum = 0;
$results = [];
foreach ($data as $index => &$datum) {
if ($datum == 0) {
continue;
}
if ($sum + $datum <= 10) {
$sum += $datum;
$results[] = $index;
$datum = 0;
}
if ($sum == 10) {
return $results;
}
}
}
print_r(add_to_ten($dataset));
print_r(add_to_ten($dataset));
print_r(add_to_ten($dataset));
Good afternoon, good people of SO!
I'm trying to make a binary calculator, which adds two binary strings together and returns the result in binary.
However, my loop (used for checking the input consists of 0 or 1) seems to falter and return either a 503 (Service Temporarily Unavilable) or suggests I've hit the maximum execution time (30 secs).
I can't figure out why. It seems to bypass this problem if I change && to ||, however this returns a false positive for a bad input.
Here is the code:
// Spring cleaning - add some variables
$submit = htmlspecialchars(strip_tags(stripslashes($_POST['submit'])));
$val1 = htmlspecialchars(strip_tags(stripslashes($_POST['val1'])));
$val2 = htmlspecialchars(strip_tags(stripslashes($_POST['val2'])));
$val1_length = strlen($val1);
$val2_length = strlen($val1);
$val1 = str_split($val1, 1);
$val2 = str_split($val2, 1);
// Val 1 - Checking
$count = 0; // count variable counts how many times the loop recurs and stops it appropriately
while ($count <= $val1_length) {
if(($val1[$count] != 0) || ($val1[$count] != 1)) { // checks if input is comprised of 0 or 1
showInputError();
exit(); // input does not contain 0 or 1, abort script and do not attempt further calculations
$count = $count + 1; // increment the count variable after one successful loop
}
} // Val1 was fine
Thanks in advance! :)
As bwoebi said in the comments, put the bracket one line higher in the if statement, as you're not actually counting up, so the loop will go on forever if no value is found..
$count = 0; // count variable counts how many times the loop recurs and stops it appropriately
while ($count <= $val1_length) {
if(($val1[$count] != 0) || ($val1[$count] != 1)) { // checks if input is comprised of 0 or 1
showInputError();
exit(); // input does not contain 0 or 1, abort script and do not attempt further calculations
}
$count = $count + 1; // increment the count variable after one successful loop
} // Val1 was fine
Why don't you simply use a simple regex like
$input= '1101010001010101110101';
preg_match('/^[01]+$/', $input);
It seems to me you are not verifying your $val1_length in the while loop. What happens if your POST values are empty? You will get an infinite loop, so you might want to replace the while like this:
while ($count <= $val1_length && !empty($val1_length) {...}
I need help. I need a loop that can execute some codes every 10 rows.
Suppose this is the scenario:
$rows = 15; // The row is for a generated report. I need to put a Thick Border, Horizontally every 10 rows.
This is the loop, and inside it I want another loop or any idea how can I execute some command every 10 rows assuming $row = 15, obviously the command should be executed once since the rows is only 15 and command will execute every 10 rows. Thanks everyone :)
$rows = 15;
for($c=0;$c<$size3;$c++)
{
//Location I want to execute a command every 10 rows.
}
Try for loop for this
for($i = 1;$i <= 40;$i++) {
if($i % 10 == 0) {
// your horizontal code here
} else {
// non horizontal code here
}
}
Edit Remember start the loop from 1 not from 0. See codepad
With 0
With 1
seems there is problem in $rows value, insted of adding condition on $rows %10 ==0 you may try setting one counter $i in loop which will get increment for each row and then you can try adding condition on $i %10 ==0
$startrow = 10 //10 if base 1 or 9 if base 0
$endrow = 15 //you said 15 so lets go with this
for($row = $startrow; $row < $endrow; $row+=10)
{
//do your thing here
}
Since you only want to perform the action once every 10 rows, why not start at row 10, then go by increments of 10 instead of incrementing the row 1 by 1.
This should be more efficient since there will be no wasted loops given your circumstance, it also eliminates the need for checking if the row is divisible by 10.
Sorry for the rather unhelpful title, I am trying to make multiple pages for my foreach statement, as it reaches to a database with 100's of rows. Would I limit the query 1,30 for the first page, then 31-60 the second? If so how would I code that?
$totalRows = 100;// Total rows
$perPage = 30;
$pages = ceil($totalRows/$perPage);
for ($i = 0; $i<=$pages; $i++)
{
$sql = sprintf("SELECT * FROM table LIMIT %s, %s", $i*$perPage, $perPage);
// Execute
}
Queries can carry a significant overhead, so don't do multiple queries. Rather, do a single query, then use PHP to break the results up as you wish. You can use a loop counter along with the % operator to check if it's time to do a break.
For instance, if you are looping through results and your loop variable is $i, then you can do:
if ( $i > 0 && $i % 30 == 0 ) {
// Breaking action here ...
}
To detect every 30th iteration of your loop and implement your logic.