wiitohzjeh
wiitohzjeh

Reputation: 347

Spliting mysql data in 3 columns error

Morning,

We have a script for spliting the data we get from a mysql database (based on this answer: https://stackoverflow.com/a/8660624/1067100) in a table with three columns, in the case of the current example, we have a database that returns 8 values ​​but appear 9 (latter completely empty).

The current image can clarify the problem if it has become abundantly clear, and also a link to the code we think is wrong. We have reviewed but we are not able to find out what the problem.. :/

the code: https://gist.github.com/99df1288cd1d998c8172

enter image description here

Thanks in advance.

Upvotes: 0

Views: 422

Answers (1)

Robbie
Robbie

Reputation: 17710

The queickest fix is that you need a "break" in the inner loop.

for ($i=0; $i < count($data)/3; $i++){
    for ($j=0; $j<3; $j++){
         if (($i * 3) + $j >= count($data)) {
             break;
         }

Note that you should save a fractional bit of computing power by storing count($data) in a variable - unless you expect the count to change.

$countOfData = count($data);
for ($i=0; $i < $countOfData/3; $i++){
    for ($j=0; $j<3; $j++){
         if (($i * 3) + $j >= $countOfData) {
             break;
         }

Upvotes: 1

Related Questions