Equalizing content between columns - php

I am looking for an optimized way of displaying dynamically generated items sorted and grouped based on initials (like the image below). Groups can have different number of items and therefore also the total number of items is not known.
What would be the best solution to spread the groups across columns(they should remain sorted)?
Currently the groups and items are generated from php and spread across columns(divs) which contain the groups and items within ul and li items.
https://www.dropbox.com/s/ub3mg3twm0eg8b6/columns.jpg
Thanks in advance,

You can use CSS3 columns https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_multi-column_layouts and this will work as a polyfill https://github.com/BetleyWhitehorne/CSS3MultiColumn

Since it looks you just need a push to get started he's what I would do. Since your columns are dynamically generated I assume you're using PHP for that you should:
Count each item in the columns
Count all items on the whole page
Divide that by three
Next you'd output the first third until the last item of the last group
Count all the items in the first column
Next output starting at the first item of the new group and output as many items as in the first group excluding the last group
Output the rest of the items in de last column
Needs some tweaking of course. Good luck!

Related

Iterate throw checkbox list, even if not checked, in Laravel

I have a table of items, and if the user selects an item I need to know how many units are assigned to this item. So I created a check box list coming back with the selected items ids and another one for units.
<td>{{$item->name}}</td>
<td>{{Form::checkbox('itemchks[]', $item->id)}}</td>
<td>{{Form::text('units[]','0')}}</td>
The problem is that, unless the user checks all items we get 2 different-size lists.
For ex, when choosing 3 items, and assigning them some units values, the itemchks come back with these ids
["4","15","23"]
but the units list returns
["15","0","18","0","0","0","0","0","20"]
so I can't know, exactly, each id and its correspondent units.
I'm using laravel 4.2
Any ideas will be appreciated.
Instead of letting the browser set the key in your units[] array you could do that yourself using the $item->id:
<td>{{$item->name}}</td>
<td>{{Form::checkbox('itemchks[]', $item->id)}}</td>
<td>{{Form::text('units['.$item->id.']','0')}}</td>

MySQL editable sort index in CakePHP

I am trying to implement a way to sort elements in a list based on the order they were added from oldest to newest. This would be easy to do in MySQL using an ORDER BY on a created DATETIME. The problem is I then want to be able to move elements up and down the list and have that order saved in the database, but also make sure that new elements get put on the end of the list.
I have thought of using an INT index and just increment that for each new item that is added to the list. Then when an existing item is moved up or down in the list, swap the index numbers. Does this sound like the best way to achieve this result? If not, would anyone be able to provide some insight of a better way. Thanks
So I ended up solving this by using an INT field in the MySQL table called order. When new items are added, the order field is just incremented from the previous largest order in the table. When items are moved up or down in order, all other items orders are shifted up or down accordingly so that the order numbers are always continuous. On delete, orders are also shifted down.

Creating columns of equal width

So I got a problem that I can't wrap my mind around.
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.
This is what the list will look like when the code is working.
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!
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.
Your approach is most ideal in today's context. Let me explain...
The ideal thing to do right now is do your little calculation and split the list into the number of rows & columns.
The alternative is a CSS3 approach. i.e., you can create the whole list in ONE column through PHP. And on the CSS side, you can specify the new property "column-count".
But there are issues. This is not yet properly standardised. So you've got to specify the -moze- prefix and -webkit- prefix depending on your browser. But the reason I wouldn't go for this is that IE still does not support this. And it's too early to consider an upgrade by all users even if they did.
Going one step further, you ought to modify your splitting algorithm to take into account the category headings.
Hope this helps :)

MySQL query result split

This is somewhat of a multipart question, but..
I am looking to query a MySQL table to get fields from a event category table.
Each category has a specific calendar assigned to it, in the "calendar" field in the category table.
I am planning to have a HTML list box for each of the different types of calendars (only 4, and they wont change).
Is there a way to query the category table once, and split the results into different arrays?
Ex.
Sports (only categories assigned to the sports calendar appear here):
(in list box):
Basketball
Baseball
Golf
etc.
then,
General:
(only categories assigned to the general calendar appear here)
etc.
etc.
etc.
I thought to do this in one query, instead of querying the whole table for each calendar type, but will there be that much difference in speed?
I am using PHP, by the way.
Thanks for the help.
You can query the table once and use mysql_data_seek to reset the rowset pointer back to the beginning after having read through it - i.e. iterate over the rowset for category 1, reset the pointer, iterate over for category 2, etc. You need only query once, and iterating over the results is very fast vs. querying.
Alternatively, have four strings each containing the HTML for the content of one of the listboxes, and iterate over the rowset once, appending to the relevant string based on the category of the current record.

How to find "related items" in PHP

we often see 'related items'. For instance in blogs we have related posts, in books we have related books, etc. My question is how do we compile those relevency? If it's just tag, I often see related items that does not have the same tag. For instance, when search for 'pink', a related item could have a 'purple' tag.
Anyone has any idea?
There are many ways to calculate similarity of two items, but for a straightforward method, take a look at the Jaccard Coefficient.
http://en.wikipedia.org/wiki/Jaccard_index
Which is: J(a,b) = intersection(a,b)/union(a,b)
So lets say you want to compute the coefficient of two items:
Item A, which has the tags "books, school, pencil, textbook, reading"
Item B, which has the tags "books, reading, autobiography"
intersection(A,B) = books, reading
union(A,B) = books, school, pencil, textbook, reading, autobiography
so J(a,b) = 2/6 = .333
So the most related item to A would be the item which results in the highest Jaccard Coefficient when paired with A.
Here are some of the ways:
Manually connecting them. Put up a table with the fields item_id and related_item_id, then make an interface to insert the connections. Useful to relate two items that are related but have no resemblance or do not belong to the same category/tag (or in an uncategorized entry table). Example: Bath tub and rubber ducky
Pull up some items that belong to the same category or have a similar tag. The idea is that those items must be somewhat related since they are in the same category. Example: in the page viewing LCD monitors, there are random LCD monitors (with same price range/manufacturer/resolution) in the "Related items" section.
Do a text search matching current item's name (and or description) against other items in the table. You get the idea.
To get a simple list of related items based on tags, the basic solutions goes like this:
3 tables, one with items, one with tags and one with the connection. The connection table consists of two columns, one for each id from the remaining tables. An entry in the connection table links a tag with an item by putting their respective ids in a row.
Now, to get that list of related items.
fetch all items which share at least one tag with the original item. be sure to fetch the tags along with the items, and then use a simple rating mechanism to determine, which item shares the most tags with the original one. each tag increases the relation-relevancy by one.
Depending on your tagging-habits, it might be smart to add some counter-mechanism to prevent large overarching tags from mixing up the relevancy. to achieve this, you could give greater weight to tags below a certain threshold of appliances. A threshold which has generally worked nicely for me, is total_number_of_tag_appliances/total_number_of_tags, which results in the average number of appliances. If the tags appliance-count is smaller than average, the relation-relevancy is increased double.
It can be more than a tag, for example it can be average of each work appearing in a paragraph, and then titles, etc
I would say they use ontology for that which adds more great features to the application.
it can also be based on "people who bought this book also bought"
No matter how, you will need some dort of connection between your items, and they will mostly be made by human beings
This is my implementation(GIST) of Jaccard index with PostgreSQL, and Ruby on Rails...
Here is an implementation of jaccard index between two texts based on bigrams.
https://packagist.org/packages/darkopetreski/textcategorization

Categories