简体   繁体   English

多列动态PHP列表

[英]Multi column dynamic PHP list

So what I'm trying to do is select all the distinct months from my database and then print them in a list. 所以我想做的是从数据库中选择所有不同的月份,然后将它们打印在列表中。 That, I can accomplish. 那我可以完成。 The problem lies in the fact that I need my list to be two column. 问题在于我需要将我的列表分为两列。 The way that I achieve this with CSS is by using 2 different div's "left" and "right" which are floated next to each other. 我用CSS实现此目的的方法是使用两个不同的div彼此相邻浮动的“ left”和“ right”。 This poses a problem with PHP because it needs to echo a div close and a new div open after it echoes the sixth month. 这给PHP带来了一个问题,因为它需要在回显第六个月之后回显div关闭和新的div打开。 Then it needs to start again from where it left off and finish. 然后,它需要从中断处重新开始并结束。 I can't just list all of the months in the HTML, either because I don't want it to list a month if I don't have any records in the DB for that month, yet. 我不能只列出HTML中的所有月份,因为我还不想在数据库中没有该月的任何记录的情况下列出该月份。 Any ideas? 有任何想法吗? I hope I was clear enough! 我希望我足够清楚!

Thanks! 谢谢!

-williamg -williamg

Something like this should work (the basic idea being to just keep a count of the months an increment it as you loop through them): 这样的事情应该起作用(基本思想是在循环浏览时,仅对月份进行计数):

<div class="left">

<?php
$x = 1;
foreach($months as $month) {

   # switch to the right div on the 7th month
   if ($x == 7) {
      echo '</div><div class="right">';
   }

   echo "<div class=\"row\">{$month}</div>";

   # increment x for each row
   $x++;

}

</div>
<?php
$numberOfMonths = count($months);
$halfwayPoint = ceil($numberOfMonths / 2);

echo "<div class=\"left\">";
for($i=0; $i<$halfwayPoint; $i++){
        echo $months[$i] . "<br />";
}
echo "</div><div class=\"right\">";

for($i=$halfwayPoint; $i<$numberOfMonths; $i++){
        echo $months[$i] . "<br />";
}
echo "</div>";
?>

Rant: on 兰特:

When displaying tabular data, use table instead of floating div . 显示表格数据时,请使用table而不是float div It will make sense when viewing the page with css disabled. 在禁用css的情况下查看页面时,这将很有意义。 If you use floated div , then you data will displayed all way down. 如果使用float div ,那么数据将一直向下显示。 Not all table usage is bad. 并非所有table使用情况都不好。 People often hate table so much, so using floated div . 人们经常讨厌table ,所以使用float div Table only bad when used for page layout. 仅当用于页面布局时, Table不好。

Rant: off Rant:关闭

When I need to have certain content displayed with some open, close, and in-between extra character, I will make use of implode . 当我需要以某些打开,关闭以及介于中间的额外字符显示某些内容时,我将使用implode This is the example: 这是示例:

$data = array('column 1', 'column 2');
$output = '<div>'.implode('</div><div>', $data).'</div>';
//result: <div>column 1</div><div>column 2</div>

You can extends this to almost anything. 您可以将其扩展到几乎所有内容。 Array and implode is the power that php have for many years. Array和内implode是php多年以来的力量。 You will never needed any if to check if it last element, then insert the closing character, or check if it first element, then insert opening character, or print the additional character between elements. 你将永远不会需要任何if检查它是否最后一个元素,然后将结束字符,或检查它是否第一个元素,然后插入开幕字符,或打印元素之间的额外字符。

Hope this help. 希望对您有所帮助。

Update: 更新:

My bad for misread the main problems asked. 我的错读了主要问题。 Sorry for the rant ;) Here is my code to make a data displayed in 2 column: 对不起,我的代码;这是我的代码,使数据显示在第2列中:

//for example, I use array. This should be a result from database
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
//should be 12 month, but this case there are only 9 of it

for ( $i = 0; $i <= 5; $i++)
{
  //here I do a half loop, since there a fixed number of data and the item for first column
  $output = '<div class="left">'.$data[$i].'</div>';
  if ( isset($data[$i+6] )
  {
    $output = '<div class="right">'.$data[$i+6].'</div>';
  }
  echo $output."\n";
}
//the result should be
//<div class="left">1</div><div class="right">7</div>
//<div class="left">2</div><div class="right">8</div>
//<div class="left">3</div><div class="right">9</div>
//<div class="left">4</div>
//<div class="left">5</div>
//<div class="left">6</div>

Other solution is using CSS to format the output, so you just put the div top to down, then the css make the parent container only fit the 6 item vertically, and put the rest to the right of existing content. 另一种解决方案是使用CSS格式化输出,因此只需将div顶部向下放置,然后使CSS使父容器仅垂直适合6项,然后将其余部分置于现有内容的右侧。 I don't know much about it, since it usually provided by fellow css designer or my client. 我对此了解不多,因为它通常是由CSS设计师或我的客户提供的。

Example assumes you have an array of objects. 示例假定您有一个对象数组。

<div style="width:150px; float:left;">
<ul>
<?php

$c = count($categories);

$s = ($c / 3); // change 3 to the number of columns you want to have.

$i=1;
foreach($categories as $category)
{
    echo '<li>' . $category->CategoryLabel . '</a></li>';
    if($i != 0 && $i % $s == 0)
    {
        ?>
        </ul>
        </div>
        <div style="width:150px; float:left;">
        <ul>
        <?php
    }
    $i++;
}

?>
</ul>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM