创建宽度相等的列 [英] Creating columns of equal width

查看:96
本文介绍了创建宽度相等的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我就,我不能绕到我心中的一个问题。

So I got a problem that I can't wrap my mind around.

我创建分为十大类各种长度的购物清单。 (所有项目的来自数据库)。我得到了它使用的是单柱时上班,但我不得不名单分成四列。在code应该决定哪些类别应该去的地方,这样的四个列具有项目可能的最平等的数量。

I'm creating a shopping list that is divided into ten categories of various lengths. (All of the items come from a database). I got it to work when using a single column, but I have to divide the list into four columns. The code should decide which categories should go where so that the four columns have the most equal number of items possible.

这是表会是什么样当code工作等。

在这些十类,其中4人,他们属于一个特定的类别。

Out of these ten categories, four of them have a specific category they belong to.

我接近这个问题的方法是计算的总项数和由四个划分它来计算每列项目的平均数目。我把四个特殊类别在各自列,并保持跟踪的很多项目是如何在每列都是现在

The way I've approached this is to count the total number of items and divide it by four to compute the average number of items per column. I put the four special categories in their respective column and kept track of how many items were now in each column.

现在我仍然有剩下的各种尺寸的六列。是什么把它们放在将最适合的列的最佳方法?由于某些类别的人比其他人要大很多,某些列可能有三个或四个类别。

Now I still have six columns remaining of various sizes. What is the best approach to put them in the column that would fit best? Since some categories are much larger than others, some columns could potentially have three or four categories.

更新:紧接着我张贴了这个我又意识到我应该找到用最少的项目列,并添加最大的一类吧。这似乎是它会工作。它看起来像戴夫是在暗示一样!

UPDATE: Right after I posted this I came to the realization that I should find the column with the least items and add the largest category to it. This seems like it will work. And it looks like Dave is suggesting the same!

推荐答案

编写4主类别的列后,使该公司共有每个列的数组:

After writing your 4 "main" categories to the columns, make an array that has a total of each column:

$columnTotals = array(10,6,12,13)
//example - obviously you'd use count or something to get the totals

然后,最大的订单你非特殊类别数组最小的:

Then, order your non-special categories in an array by largest to smallest:

$subcatTotals = array(18,15,13,12,8,4);
//here, you'll have to get the totals, then use an array sort to order them
//probably want an associative array so you know which total matches which cat.

然后,在一个循环中,添加第一(最大)子类的最小列,并获得该列一个新的总

Then, in a loop, add the first(largest) sub-category to the smallest column, and get a new total for that column.

这应该给你最偶数列,你可以得到的 - 至少它在所有的虚构的例子我已经试过了。

This SHOULD give you the most even columns you can get - at least it has in all the made-up examples I've tried it with.

这篇关于创建宽度相等的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆