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.
Related
I have a table with five rows for example, and my while loop will display the five rows for me as per normal.
I‘m trying to get the loop to keep looping records until let's say $count = 12. I want to start the counter at 0 and it will end at 12. So, I pretty much want the loop to keep showing the same records randomly until it shows me 12 or so results and then stops.
Simple as while ($a != 12)
$counter = 0;
while ($counter != 12) {
// Do stuff
$counter = rand(0, 24);
}
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.
for ($x = 0; $x <= 3; $x++) {
for ($y = 0; $y <= 4; $y++) {
if ($y == 0) {
mysql_query("insert into tb_weight_rate_management
(nation,zone_id,rate) values ('Domestic',1,'$del_100')");
}
}
}
hello i am little troubled using for loop....in the code above i have two loops ...1st loop will work 3 times and the the inside loop will work 4 times....
now when i click on submit button then it checks loop one and then enter second loop and inserts data 4 times in the database....which is wrong...i want if $y=0;
then it should insert data only once but it is inserting data 4 times can anyone please correct the above condition
You should use == instead of =. Like this:
if ($y == 0) {
the first loop runs 4 times, hence 4 inserts
In the inner loop you declare $y = 0 on every iteration of the outer loop
for each x iteration, there are one time y=0. and since x executes 4 times (0, 1, 2 and 3) y gets 0 value 4 times as well. if you want to do the insertion only once then you must add x value as well like if($y == 0 and x == 0).
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.
}
}
}
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.