代码之家  ›  专栏  ›  技术社区  ›  rory-h

3列布局-如何在每列中设置限制

  •  0
  • rory-h  · 技术社区  · 8 年前

    我有一个3列列表(数据来自api)。每页最多30页。我已将清单分为3列。

    enter image description here

    我的代码有一些问题,当它出现在更大的屏幕上时,它不再是3列布局,每列有10多个数据。

    问题:

    1) 如何确保此布局即使在更大的屏幕上也保持为3列布局?

    2) 如何在每列中设置10的限制?

    php/html:

        <div id="native" class="margin-top-2x">     
             <ul>
                <?php foreach($model as $d) { ?>
                 <li>
                    <?php if(!empty($d['id'])): ?>
                    <a href="<?php echo $this->createUrl('frontend/detailedView', array('id' => $d['id'])) ?>"><?php echo $d['title'];?> </a>
                    <?php endif; ?>
                    <br></li>
                <?php } ?>
            </ul>
        </div>
    

    css:

        #native {
          -webkit-column-width: 400px;
          -moz-column-width: 400px;
          -o-column-width: 400px;
          -ms-column-width: 400px;
          column-width: 400px;
    
        }
        #native ul {
            list-style: none;
        }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Muhammad Omer Aslam    8 年前
    • 你应该画3个不同的 <ul> 以确保获得3列。
    • 移动 <ul> 标签位于 foreach
    • 使用 if(($i % 10) == 0) 哪里 $i 计数器是否限制为10 <li> 在每个 <ul> 并关闭 </ul> 同时开始新的 <ul> 但看看这是否是最后一次迭代 (end ( $model['id'] ) == $d['id']) 然后不要开始新的 <ul>

      参见以下代码

      <div id="native" class="margin-top-2x">  
          <ul>
              <?php 
                 $i=1;
                 foreach ( $model as $d ) { ?>
                      <?php if ( !empty ( $d['id'] ) ): ?>
                          <li>
                              <a href="<?php echo $this->createUrl ( 'frontend/detailedView' , array( 'id' => $d['id'] ) ) ?>"><?php echo $d['title']; ?> </a>
                          </li>
                      <?php endif; ?>
                      <br>
                  <?php
                  if ( ($i % 10) == 0 ) {
                      echo "</ul>";
                      echo (end ( $model['id'] ) == $d['id']) ? "" : "<ul>";
                  }
                  $i++;
                  ?>
              <?php } ?>
      </div>
      

      您需要在一行中显示它们的最小Css

      #native ul {
          display: inline-block;
      }
      
        2
  •  0
  •   Nadir Latif    7 年前

    请尝试以下代码。它在包含固定行数的html表中显示具有x元素的数组的内容。为了使用您自己的数据,请替换以下行:

    $模型=数组填充(0,25,“测试”);

    $最大行数=10;

    <?php
    
    /** The column counter */
    $column_counter = 0;
    /** The row counter */
    $row_counter    = 0;
    /** The total counter */
    $total_counter  = 0;
    /** The maximum number of columns to show */
    $max_columns    = 0;
    /** The maximum number of rows */
    $max_rows       = 10;
    /** A two dimensional array for storing the data */
    $data           = array();
    
    /** The sample data */
    $model          = array_fill(0, 25, "test");
    
    /** Convert the $model array into a two dimensional array */
    foreach($model as $d) {
        /** The data is added to array */
        $data[$row_counter][$column_counter] = $d;
    
        /** The total counter is increased by 1 */
        $total_counter++;
        /** The row counter is increased by 1 */
        $row_counter++;
    
        /** If the total counter is divisible by $max_rows, then column counter is increased by 1 and row counter is set to 0 */
        if ($total_counter % $max_rows == 0) {
            $column_counter++;
            $row_counter=0;
        }
    }
    /** The max columns is set to the column counter */
    $max_columns      = $column_counter;
    /** The start table tag */
    echo "<table>";
    /** Display the two dimensional data in html table. Each row is checked */
    for ($count1 = 0; $count1 < $max_rows; $count1++) {
        /** The row start tag is displayed */
        echo "<tr>\n";
        for ($count2 = 0; $count2 < $max_columns; $count2++) {
            /** If the column does not exist, then empty column is shown */
            $column_data   = (isset($data[$count1][$count2])) ? $data[$count1][$count2] : "&nbsp;";
            /** The column data is shown */
            echo "<td>" . $column_data . "</td>\n";
        }
        /** The row end tag is displayed */
        echo "</tr>";
    }
    /** The end table tag */
    echo "</table>";
    
    ?>
    

    如果要在具有固定列数的表中显示数据,请在上述代码中替换以下内容:

    /** The row counter is increased by 1 */
    $row_counter++;
    
    /** If the total counter is divisible by $max_rows, then column counter is increased by 1 and row counter is set to 0 */
    if ($total_counter % $max_rows == 0) {
        $column_counter++;
        $row_counter=0;
    }
    

    具有

    /** The column counter is increased by 1 */
    $column_counter++;
    
    /** If the total counter is divisible by $max_columns, then row counter is increased by 1 and column counter is set to 0 */
    if ($total_counter % $max_columns == 0) {
       $row_counter++;
       $column_counter=0;
    }
    
    推荐文章