Question

I'm trying to make a simpler version of

$query1 = "SELECT $select FROM HelpDesk ORDER BY createdtime DESC LIMIT 100";
$records1 = $client->doQuery($query);
$query2 = "SELECT $select FROM HelpDesk ORDER BY createdtime DESC LIMIT 100,100";
$records2 = $client->doQuery($query2);

What I'm looking to do is run it so that it's something like

$qnum = 1; qmax = 15; $offset = 100;
while (qnum < $qmax) {
$query# = "SELECT $select FROM Table ORDER BY Field DESC LIMIT 100,$offset";
$records# = $client->doQuery($query#);
}

$records = array_merge($records[]);

Now I know this is horrible, but I'm just trying to get across what I'm trying to do.

I'm fairly new to arrays, and I know I need a for or foreach in there for the offset. I'm just unsure how to do it.

Thanks to Chococroc The code I'm now using is:

$query1 = "SELECT $select FROM HelpDesk ORDER BY createdtime DESC LIMIT 100";
$records1 = $client->doQuery($query1);
$records = array();
for ($i = 100; $i <= 1500; $i+=100 ) {
 $query      = "SELECT $select FROM HelpDesk ORDER BY createdtime DESC LIMIT 100,$i";
 $new_records = $client->doQuery($query);
 $records     = array_merge($records, $new_records);
}
$records     = array_merge($records, $records1);

I did set it originally to $i = 0 but it seemed to skip a row or 3 (depending on the refresh).

Was it helpful?

Solution

If you now where you start, and where you finish, just use a for loop:

$result = array();
for ($i = 100; $i <= 1500; $i+=100 ) {
    $query      = "SELECT $select FROM HelpDesk ORDER BY createdtime DESC LIMIT $i, 100";
    $new_result = $client->doQuery($query);
    $result     = array_merge($result, $new_result);
}

Be aware of the offset, it's the first parameter in the limit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top