user2570995
user2570995

Reputation: 29

new column after every 10 results

I am getting result as...............

Column 1

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

.

.

So On

I need to display result like this ...... new column after every 10 rows.

col 1  |    col 2  | .....
abc    |    11     | 21
jjj    |    12     |  .
jhjjk  |    13     |  .
jhbjj  |    ...    |  .
.....  |    ...    |  .  
.....  |    ...    |  .
10     |    20     | 30

have done this much .....

<?php

mysql_select_db("$database");

    $sql = "select test, rate FROM product";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
   echo <<<EOL
<input type="checkbox" name="name[]" value="{$row['test']}-{$row['rate']}" /> {$row['test']}-{$row['rate']}<br />

EOL;
}
?>

Any help will be appreciated.

Upvotes: 0

Views: 156

Answers (2)

jetblack
jetblack

Reputation: 590

You'll need to implement your logic in PHP code like this:

<?php
$input_fields = array();
$rows_per_column = 10;

mysql_select_db("$database");

$sql = "select test, rate FROM product";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
    $input_fields[] =<<<EOL
<input type="checkbox" name="name[]" value="{$row['test']}-{$row['rate']}" /> {$row['test']}-{$row['rate']}
EOL;
}

echo "<table><tr>";

foreach ($input_fields as $i => $value){
    if ($i % $rows_per_column == 0){
        echo '<td valign="top">';
    }
    else{
        echo '<br>';
    }

    echo $value;

    if ($i % $rows_per_column == $rows_per_column - 1 || $i == count($input_fields) - 1){
        echo '</td>';
    }
}

echo "</tr></table>";

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269603

If you could survive with lists rather than separate columns, then this is not too bad in SQL. That would be:

1,11,21,31
2,12,22,32

But the values would all be in one column.

The idea is to enumerate the values (using variables) and then aggregate:

select group_concat(column order by rn)
from (select t.column, (@rn := @rn + 1) as rn
      from t cross join
           (select @rn := 0) params
      order by ??  -- do you want an ordering?
     ) t
group by (rn - 1) % 10
order by min(rn);

Upvotes: 1

Related Questions