PHP - How To Make Pages For Overflowing Foreach? - php

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.

Related

How to keep looping in while loop?

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);
}

Execute every 10 rows

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.

how to divide sql query result into small parts

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.
}
}
}

PHP to run at intervals within foreach but with added randomness

So i have a foreach and i want to insert a bit of html every say 6 times or maybe 11 times the loop is run.
So my insert on each 13th record
if ($i % 13 == 0) {
}
trouble is i want to add a bit more randomness to it.
Totally forgotten what this is called as well
Something like this will make some code execute $percentage of the time every loop. For example you could set $percent = 20 then your code will execute only 20% of the time each iteration.
$value = rand( 0, 100 ); // Set your ranges (min/max)
$percent = 0; // Set percentage
if ( $percent >= $value )
{
// Will only execute $percentage of the time each loop
}

PHP loop according to amount of items in array

I'm trying to write a short script that will query my mysql db, and according to the amount of results (dynamic) i want the script on each segment.
For example, $arr is a result of a mysql_fetch_array and it has 872 items, I want to run my function 9 times, 1 for each 100 items and the last one for 72 items.
How can I do that?
Simply use a for loop with an incrementor that increments by 100. You can use array_slice() to get the concerned rows on each loop.
$dbRows = resultsFromDB();
for($i = 0; $i < count($dbRows); $i+=100) {
$concernedRows = array_slice($dbRows, $i, 100);
mySuperFunction($concernedRows);
}
Maybe something like:
$length = count($arr);
for ($i = 0; $i < ceil($length / 100); $i++) {
}
If I understood.

Categories