I have a request (mysql) in PHP who gives me many results and it works like a charm. Here is an example of a request who gives me 115 results :
public static function getInfo($start, $limit) {
$sql = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(
SELECT blablabla FROM blabla ORDER BY bla LIMIT $start,$limit
);
return $sql;
}
In my php file, i've got :
$rq = getInfo (0, 0);
$count_rq = (int)count($rq);
for ($i = 0; $i < $count_rq; ++$i) {
.... do my things ....
}
Here is the problem : in this configuration, it shows only results 1 to 42...
When i do some modifications/tests, it shows strange results :
Modification => for ($i = 42; $i < $count_rq; ++$i) {.......}
I got 0 result.
Modification => for ($i = 43; $i < $count_rq; ++$i) {.......}
I got results 43 from 115 !..
Modification => for ($i = 60; $i < $count_rq; ++$i) {.......}
I got results 60 from 115 !..
I've tried to modify the "LIMIT" but i don't understand. Here is the result when i do modifications/test :
(i'm using for ($i = 0; $i < $count_rq; ++$i) {.......})
Modification => $rq = getInfo (0, 20);
I got results 1 to 20.
Modification => $rq = getInfo (0, 50);
I got results 1 to 42.
Modification => $rq = getInfo (10, 0);
I got results 1 to 42.
Modification => $rq = getInfo (10, 30);
I got results 11 to 40.
I don't know how I can show all of my results? I want to have a code who can show 115 results when request got 115 results, or 10 results when request got 10 results. How can I do that? Why am I limited to first 42 results?
You use limit in your request and check that MySQL SELECT is retrieving good results.
Then, why do you start for loop in any other thing than 0??
If your request is getting 115 rows you should use this sentence to iterate through all of them:
for ($i = 0; $i < $count_rq; ++$i)
Good luck.
Related
I have an array like this:
$datas = array(54,12,61,98,88,
92,45,22,13,36);
I want to write a loop which can deduct values of an array like below and show it with echo:
$datas[5]-$datas[0] for this line the result will be 92-54 "38"
$datas[6]-$datas[1] for this line the result will be 45-12 "33"
$datas[7]-$datas[2] ... "-39"
my codes are:
<?php
$smonth1= 0;
$emonth1=5;
for ($i = 5; $i > 0; $i-- ) {
$result = array_diff($datas[$emonth1], $datas[$smonth1]);
echo (implode ($result))."<br/>" ;
$smonth1++ ;
$emonth1++;
}
?>
but I couldn't get the result I don't know why. I am fresh in php. Can you help me??
Assuming the input array always has an even number of values in it (which I think is the only way this scenario could logically work), then you can simply count how many items are in the array, and then loop through it, taking the nth item and subtracting it from the n+(total / 2)th item.
$data = array(54,12,61,98,88,
92,45,22,13,36);
$halfway = count($data)/ 2;
for ($i = 0; $i < $halfway; $i++)
{
$j = $i + $halfway;
echo $data[$j] - $data[$i].PHP_EOL;
}
Demo: https://3v4l.org/ictDT
Basically, you want something like this
<?php
$data = [
54, 12, 61, 98, 88,
92, 45, 22, 13, 36
];
$offset = 5;
for ($i = 0; $i + $offset < count($data); $i++) {
echo $data[$i + $offset] - $data[$i];
echo "\n"; // or <br/> if you run it in browser
}
function M1($x, $y){}
function M2(&$x, $y){}
function M3(&$x, &$y){}
$arr = ['a' => ['b' => range(1, 1000)]];
$ref_arr = &$arr['a'];
$var = $ref_arr['b'];
$ref = &$ref_arr['b'];
//N->N, N->N
//0.003000020980835 sec.
for($i = 0; $i < 10000; ++$i)
M1($var, $var);
//N->R, N->N (slow)
//0.59903407096863 sec.
for($i = 0; $i < 10000; ++$i)
M2($var, $var);
//N->R, N->R
//0.003000020980835 sec.
for($i = 0; $i < 10000; ++$i)
M3($var, $var);
//R->N, R->N (very slow)
//1.1980690956116 sec.
for($i = 0; $i < 10000; ++$i)
M1($ref, $ref);
//R->R, R->N (slow)
//0.58603405952454 sec.
for($i = 0; $i < 10000; ++$i)
M2($ref, $ref);
//R->R, R->R
//0.003000020980835 sec.
for($i = 0; $i < 10000; ++$i)
M3($ref, $ref);
As I know, when pass a reference variable as a non-reference parameter, PHP will copy the value. So this line takes the longest time.
M1($ref, $ref); //1.1980690956116 sec.
But how to explain the performance of these 2 lines?
M2($var, $var); //0.59903407096863 sec.
M3($var, $var); //0.003000020980835 sec.
After serveral years since no one give me an answer, so I decide to answer by myself.
The reason is still the COPY-ON-WRITE mechanism in PHP, but when pass a parameter with different reference type, the copy happens immediately before the function call starts.
Define:
N for non-ref variable
R for ref variable
-> for parameter passing
These 3 forms won't trigger parameter copy:
N -> N
R -> R
N (ref count = 1) -> R
And for these 2 forms a parameter copy is always happens.
R -> N
N (ref count > 1) -> R
So when you decide to call a function with reference parameter (usually array functions like sort, reset), pass an array with refcount more than 1 is not a good idea.
Bad example:
$arr1 = [];
$arr2 = $arr1;
sort($arr1);
Good example:
$arr1 = [];
$arr2 = $arr1;
unset($arr2);
sort($arr1);
I have an array filled with data over the period of a month. The data is computed for every 15 minutes over that period, meaning it's got about 2880 entries.
I need to reduce it by about 10% in order to display the data in a chart (288 data points will render much more nicely than 2880).
Here's what I've tried (it works, but it might be a very bad method):
$count = count($this->Data1Month);
for($i = 0; $i < $count; $i += 10) {
$tempArray[] = $this->DataMonth[$i];
}
$this->Data1Month = $tempArray;
I think you have the most efficient solution, but you do have a mistake though. Array indexes start at zero so 0+10 needs to be 9, like so:
$count = count($this->Data1Month);
for($i = 0; $i < $count; $i += 9) {
$tempArray[] = $this->DataMonth[$i];
}
$this->Data1Month = $tempArray;
I am having a bit of a problem with the following code, instead of looping through 20 times it just loops through once:
$length = min($length, 20);
for ($i=0; $i<=$length; $i++)
{
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
echo ($item_title);
}
I need the loop to look through an RSS feed and display 20 items, but not spit out non-object errors if there is less than 20 items.
Thank you.
Your problem is that $length is 0 initially, and the min() of 0 and 20 is 0, so the loop only continues while $i <= 0.
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.