Insert line break after every two rows of database

前端 未结 6 1241
清酒与你
清酒与你 2021-01-16 08:31

I have a little script that prints a certain amount of rows in a mysql database.

Is there any way to make it so that after every second row it prints, ther

相关标签:
6条回答
  • 2021-01-16 09:11
    $i=1;
    while ($row = mysql_fetch_array($query))
    {
        //your code
        if ($i % 2 == 0)
            echo '<br>';
        $i++;
    }
    
    0 讨论(0)
  • 2021-01-16 09:22

    Depending on the language, something like this should do it: (in php) (where $arr is an array of results)

    $str = '';
    $i = 0;
    
    for ($i=0; $i<count( $arr ); $i++)
    {
       if ( ( $i + 1 ) % 2 === 0 )
       {
          $str .= $arr[$i] . '<br />';
       }
       else
       {
          $str .= $arr[$i];
       }
    }
    
    echo $str;
    
    0 讨论(0)
  • 2021-01-16 09:29

    Use php and modulo.

    such as

    if($i % 3)
      {
        echo '<br />'..
    
    0 讨论(0)
  • 2021-01-16 09:29

    If you need to do this inside the query for some reason, you could use something like

    SELECT
      <your fields>,
      IF (((@rn:=@rn+1) % 3)=0,'<br>','') as brornot
    FROM
      <your tables and joins>,
      (@rn:=0)
    
    0 讨论(0)
  • 2021-01-16 09:33

    You write "script" but in tags you have PHP, so I suppose you need PHP code:

    foreach ($rows as $row) {
        if ($i++ % 2) { 
             // this code will only run for every even row
        }
        ...
    }
    
    0 讨论(0)
  • 2021-01-16 09:37

    add new variable before the loop

    $i = 0;
    

    then in your loop add

    if ($i != 0 && $i%2 == 0)
       echo '<br/>';
    
    0 讨论(0)
提交回复
热议问题