每10个结果后的新列

时间:2017-02-04 12:35:25

标签: php mysql

我得到结果...............

if request.method == 'POST':
    form_extended = SiteAddFormFull(request.POST)
    if form_extended.is_valid():
        form_extended.save()
        messages.add_message(request, messages.SUCCESS, 'Success!!')
    else:
        messages.add_message(request, messages.ERROR, 'Error!!')
        print(form_extended.errors)

所以

我需要在每10行后显示这样的结果......新列。

Column 1

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

.

.

做了这么多.....

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果您可以使用列表而不是单独的列,那么在SQL中这不是太糟糕。那将是:

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

但是这些值都在一列中。

想法是枚举值(使用变量)然后汇总:

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

答案 1 :(得分:0)

您需要在PHP代码中实现您的逻辑:

<?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>";