Detecting columns in a document (table) via php - Algorithm - php

Given a table like the one below, what would be the best way to detect the two columns separately?
So what I would need the total colspans for the first column.
What is important to remember is that the nr of columns can change.
In the case of this example, the second column starts at "10 euro" (second row). The first section is equal to 2 colspans. The other section is 5 colspans.
Any (abstract) ideas on how to do this?

You must consider the gaps in between the table cells and mark their positions, like this::
0 1 2 3 4 7
0 2 3 4 5 6 7
0 1 2 4 5 7
...
0 2 7
Once you have built an array with above information, you iterate over them and mark the common gap locations:
0 2 7
Since 0 and 7 are both at the edges of your table, you can strip those off. Then you're left with position 2 as the common gap between your rows.
Done :)

Related

Multidimensional-splfixedarray for integers in PHP

My question is organizing a one-dimensional list of numbers into a two-dimensional table with a variable number of columns to the right.
The lengths of the column should be balanced so that all rows except the last are filled. The empty space in the last row should be at the right end of the row.
EXAMPLE:
List: 1,5,1,3,6,2,8,7,4,9,5,6,25,4,8,5,63
COLUMNS: 5
TABLE: 1 6 4 6 8
5 2 9 25 5
1 8 5 4 63
3 7
I have to write a program (using PHP) to read a collection of records (<100), each with a positive integer in columns 1-5. A zero value terminates the list. A second set of records should be read next, with various positive integers values for C, the number of columns, in columns 1-5. After each value of C is read, the sum of the elements in each row of the table should be computed and printed. Sums will be less than 9 digits. The two-dimensional table itself should not be printed.
Using the sample data shown above for various values of C, output similar to the following should be produced:
For example:
C ROW SUM
5 25 41 81 10
***************************
1 153
***************************
Either vertical or horizontal printing of row sums is acceptable, but a line of asterisks should separate output generated for different values of C. A zero value for C should cause the program to terminate.

Sorting horizontal and vertical query from mysql

I realy need help to make this work coz i dont know how sorting works and i am begginer in this field guys.
I have table like this:
jobs - hours - user
I am trying to sort and output table like this:
Find all jobs that exist in table horizontal
and then, in vertical menu show all users with hours (if exist) on all jobs add 0 if not
joob 1 - joob2 - joob 3 - joob 4
mark 0 3 1 0
benny 7 5 0 0
john 0 0 0 3
How do i query and sort like this output as html ?
I know latter to group and calculate but this i am having problem with.

MySQLi- How to get number of records in one table that do not match values in another table [duplicate]

This question already has answers here:
How to Find Missing Value Between Two Mysql Tables
(4 answers)
Closed 7 years ago.
Sorry for the obscure title... I don't know how best to explain this.
I have two tables, valid_sizes and items
valid_sizes:
ID SizeID Description
1 40 Small
2 41 Medium
3 42 Large
items:
ID Size
1 41
2 41
3 40
4 99
5 42
6 98
I am attempting to perform a query that finds how many items exist whose size does not exist in the valid_sizes table. In this instance, a query that would return 2. (Items 4 and 6 do not exist as a SizeID)
How would this be done?
Something like this should work. You want to do a left join, and check for NULL results.
SELECT * FROM items
LEFT JOIN valid_sizes ON items.SizeID = valid_sizes.Size
WHERE
Size IS NULL;

Mysql return only items that have n number of property matches

I have a database of items and each item has various number of properties. Is it possible for MySql only to return items that have a certain number of matches (not properties) when a search is run?
Example: I am searching for any item with a wheel that is red and has a tire.
This would return all items with these three matches even if they have more properties and would automatically exclude anything that has less than 3 matches.
I have tried playing with the COUNT + GROUP BY + HAVING but I was unable to put together a meaningful working code. Before I spend more time on this I would like to know if it is possible at all.
TABLE DESIGN
ID ITEM PROPERTY
1 1 red
2 1 wheel
3 1 tire
4 2 red
5 2 wheel
6 2 tire
7 2 lamp
8 3 red
9 3 wheel
10 4 red
I would like it to return ITEM 1 and 2
You would do this with a group by and having. You really provide no information about your data structure, but the basic idea is:
select ip.item
from design ip
where ip.property in ('wheel', 'red', 'tire')
group by ip.item
having count(distinct ip.property) = 3;

sudoku algorithm explanation formula

I'm implementing a sudoku solver using human way algorithm. Which have 3 constraint, different number ini row, cell and box.
I googled and I got http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/. But I cannot understand how this guy get floor($cell / 9) for return_row function or floor(return_row($cell) / 3) * 3 + floor(return_col($cell) / 3) for return_block.
I try to figure it out by write down the data in excel and I know there is some pattern like this :
[cell] [column]
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 0
But how did he figure it out that the formula was $cell % 9 ?
I want to know, if I don't know the answer for the formula, how can I calculate that ? How can I determine that formula ? What method should I use?
Thanks
This comes from the way the cells are counted, which we could call row-major.
You can see block and cell numbers with their respective row and column numbers on this image :
rows and columns
The first row (0) contains cells 0 to 8, the second row cells 9 to 17, and so on until row 8, which contains cells 72 to 80.
If you number rows 0 to 8 and columns 0 to 8 as well, we can see that the formula for a cell that corresponds to this numbering is then cell = 9 * row + col, which should explain the formulas for get_row and get_col.
When moving right from any cell by one column, you add 1 to the cell count, which means the formula for the cell number looks like something + col.
When moving down one row, you add to the cell number the amount of cells per row, which here is 9, so the formula also looks like 9 * row + something.
Putting those together, you get a formula that is 9 * row + col + offset : the "+ something"s dependencies is row and col are determined, but maybe they still contain a constant value.
In our case, the formula gives the numbering we want with offset=0, but if you started numbering from 1 you formula would be 9 * row + col + 1.
However you do not have to do this reasoning every time. Just now that when you have a rectangle where you count items row by row, the formula for an item's number is always row * row_size + col + number at (0,0). This is also how contiguous memory is allocated for double arrays in C, for example, a very common pattern. If you count column by column, then you have col * col_size + row + number at (0,0)
Blocks
Now blocks are numbered the same way, but there are only 3 rows and columns. You can replace one by one the elements in the get_block formula to understand it : floor(row / 3) * 3 + floor(col / 3)
Since there are 3 rows of blocks but 9 of cells, the (cell-)rows 0, 1 and 2 correspond to the first row of blocks, 3 to 5 to the second row of blocks and 6 to 8 to the final and third row of blocks. What we get out of this is that a row of blocks rb contains the rows of cells 3 * rb, 3 * rb +1 and 3 * rb + 2. The opposite operation is dividing by 3 and flooring, which gets you rb for any of the expressions above.
This works exactly the same for the columns.
Thus when replacing in the expression, we now have : block_row * 3 + block_col. This I'd exactly the same formula (with 3 instead of 9) than we had for the numbering of cells, and thus gets you the number of the block from its row and column.

Categories