Question

I have two table's like

  1. container

    it contains

    • refid as primarykey
    • rows // number of rows in a matrix
    • columns // number of columns in a matrix
  2. data_on_matrix

    it contains

    • refid as primarykey
    • cell_id // the cell of the data placed on matrix
    • text_data // data on matrix
    • container_id // data on matrix

image here

I am just make this UI by container column X rows. If someone click on any cell that cell was filled by data by inserting one row on data_on_matrix table.

My Problem is

How to recomment free cells on the container for users. How to achive by this whether SQL or Any UI tricks available for that?

Was it helpful?

Solution

(repeating from comment)

If you look for cells in a matrix which are not there, you need a "numbers" table since SQL can't create something out of nothing.

create table numbers (nr int(11) not null primary key auto_increment);
insert into numbers values (1),(2),(3),(4),(5);

(and so on until you maximal ever possible or thinkable dimension).

Then you need the row number and column number of your data_on_matrix entry:

alter table data_on_matrix add column row_id int(11) not null, add column col_id int(11) not null;

If you want to read the cells of a matrix which are already there, you just read

select row_id, col_id, text_data from data_on_matrix where refid = {$refid};

If you look for free cells, just select

select r.nr as row_id, c.nr as col_id
from numbers r, numbers c
left join data_on_matrix d on d.col_id = c.id and d.row_id = r.id and d.refid = {$refId}
where d.ref_id is null and r.id <= {$rows} and c.id <= {$columns}

with refid, rows and columns coming out of table container. It would also be easy to join container directly, but probably you already have a list of containers with their properties in php.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top