dpmpa26468 2013-06-07 11:05
浏览 18
已采纳

如果没有其他的话,如何将序列号从水平转换为垂直?

I am working on a web-page for my company about an on-line survey page, our customer service team will simply input some check-box questions into a database. and my job is to display them on a page and gather the player's answers back to our database.

for example if I have 12 questions in the database, and I need it to be displayed in a table as:

1 5 9
2 6 10
3 7 11
4 8 12

or, if I have 13 questions, it should be displayed as
1 6 10
2 7 11
3 8 12
4 9 13
5

this can be done with some if else in PHP, but i would like to do it in mathematical ways just to enrich my knowledge.

any suggestion or hints are appreciate if I can pass the total questions(in this case 13) and vertical column counts(in this case 3) in a formula, and it simply return the orders of the numbers in this way {1, 6, 10, 2, 7, 11, 3, 8, 12, 4, 9, 13, 5}

honestly, I will not rewrite the survey page. I am just curious if this can be done with some math without if/else in the programming.

  • 写回答

4条回答 默认 最新

  • doushang1880 2013-06-07 12:29
    关注

    EDIT:

    Here's my latest solution for generating the array which is even more mathematical than before:

    $q = 27;//number of questions
    $c = 4;//number of columns
    $m =  $q%$c;//modulus
    $step = floor($q/$c);
    $arr = array();
    for ($n=0; $n<$q; $n++){
        $arr[] = 1 + $step*($n%$c) + ($m + $n%$c - abs($m - $n%$c))/2 + floor($n/$c);
    }
    

    The output is as follows:

    array(1, 8, 15, 22, 2, 9, 16, 23, 3, 10, 17, 24, 4, 11, 18, 25, 5, 12, 19, 26, 6, 13, 20, 27, 7, 14, 21);
    

    ORIGINAL ANSWER:

    $q = 13;//number of questions
    $c = 3;//number of columns
    $mod = array_fill(0, $c, 0);
    for ($i=0; $i<($q%$c); $i++){
        $mod[$i] = 1; 
    }
    $step = floor($q/$c);
    $arr = array();
    $cum = 0;
    for ($i=0; $i<$c; $i++){
        $cum += @$mod[$i-1];
        $arr[] = 1 + $step*$i + $cum;
    }
    $next = $arr;
    for ($i=1; $i<$step; $i++){
        foreach ($next as $key => $value){
            $next[$key]++;
        }
        $arr = array_merge($arr, $next);
    }
    for ($i=0; $i<$c; $i++){
        $next[$i] = ($next[$i] + 1)*$mod[$i] ;
    }
    $arr = array_filter(array_merge($arr, $next));
    var_dump($arr);
    

    The output is:

    array(1, 6, 10, 2, 7, 11, 3, 8, 12, 4, 9, 13, 5);
    

    Here's my solution for generating a table:

    $q = 27;//number of questions
    $c = 4;//number of columns
    $cell = '<td>i</td>';
    $table = '<table>';
    $mod = array_fill(0, $c, 0);
    for ($i=0; $i<($q%$c); $i++){
        $mod[$i] = 1; 
    }
    $step = floor($q/$c);
    $arr = array();
    $cum = 0;
    $table .= '<tr>';
    for ($i=0; $i<$c; $i++){
        $cum += @$mod[$i-1];
        $num = 1 + $step*$i + $cum;
        $arr[] = $num;
        $table .= str_replace('i', $num, $cell);
    }
    $table .= '</tr><tr>';
    $next = $arr;
    for ($i=1; $i<$step; $i++){
        foreach ($next as $key => $value){
            $num = ++$next[$key];
            $table .= str_replace('i', $num, $cell);
        }
        //$arr = array_merge($arr, $next);
        $table .= '</tr><tr>';
    }
    $table = substr($table, 0 , -4);
    for ($i=0; $i<$c; $i++){
        $next[$i] = ($next[$i] + 1)*$mod[$i] ;
    }
    $next = array_filter($next);
    $span = $c - count($next);
    $ini = '';
    $fin = '';
    $last = '';
    foreach ($next as $key => $value){
        $ini = '<tr>';
        $last .= str_replace('i', $value, $cell);
        $fin = '<td span="'.$span.'">&nbsp;</td></tr>';
    }
    //$arr = array_merge($arr, $next);
    $table .= $ini.$last.$fin.'</table>';
    echo $table;
    

    With 27 questions and 4 columns, the output is:

    <table>
        <tr><td>1</td><td>8</td><td>15</td><td>22</td></tr>
        <tr><td>2</td><td>9</td><td>16</td><td>23</td></tr>
        <tr><td>3</td><td>10</td><td>17</td><td>24</td></tr>
        <tr><td>4</td><td>11</td><td>18</td><td>25</td></tr>
        <tr><td>5</td><td>12</td><td>19</td><td>26</td></tr>
        <tr><td>6</td><td>13</td><td>20</td><td>27</td></tr>
        <tr><td>7</td><td>14</td><td>21</td><td span="1">&nbsp;</td></tr>
    </table>
    

    You can, of course, modify the value of $cell to insert a link or whatever you want into the table cells.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来