Page 1 of 1

compatibility matrix

Posted: Mon Jan 24, 2011 4:36 pm
by adc123
Hi ,

i have 3 tables,

1st table is a list of item
2nd table is a list of other item
and the 3rd is a item1 to item2 list

what i want to be able to do dynamically is create a html table

with items 1 down the left items 2 along the top and then using the 3rd table create a check box grid where if item 1 works with item 2 the box is checked if no record is found in table 3 then its unchecked.

i have tried many ways to do this and im hitting a dead end if i knew the limits of one of the tables id be fine, but as both table 1 and 2 are dynamic and items being added to them daily i don't see how to do this, anyone know where i should start

Re: compatibility matrix

Posted: Mon Jan 24, 2011 5:18 pm
by Neilos
This looks like fun! :D Like Minesweeper! BOOM!!!

Ok assuming that table 3 has the format;

Code: Select all

+--------------+--------------+
| table_1_item | table_2_item |
+--------------+--------------+
| 1            | 5            |
| 2            | 1            |
| 3            | 3            |
| 3            | 5            |
| 3            | 1            |
+--------------+--------------+
Where the first column is the id of the item in table 1 and the second column is the id of another item in table 2. Lets imagine that each table has 5 items giving;

Code: Select all

+-----------------------+
|   | 1 | 2 | 3 | 4 | 5 |
+-----------------------+
| 1 |   |   |   |   | x |
+-----------------------+
| 2 | x |   |   |   |   |
+-----------------------+
| 3 | x |   | x |   | x |
+-----------------------+
| 4 |   |   |   |   |   |
+-----------------------+
| 5 |   |   |   |   |   |
+-----------------------+
With table 1 vertical and table 2 horizontal.

Then I would select all from table 1 and find the number of rows, then select all from table 2 and find the number of rows;

Code: Select all

$table1_rows;
$table2_rows;
These would be used to build the total table with out any check marks (the x's).

Then select all from table 3 and use a while loop to go through all the rows, reading off first the table_1_item and then the table_2_item and build some html like;

Code: Select all

echo '<ul class="row_' . $table_1_item . ' col_' . $table_2_item . '">x</ul>';
You would then have a <ul> with its column and row stored as classes, you could use css to place these where you wanted using the row and col as a template. Or even JavaScript, your choice.

It's one way, probably not the most efficient, but it will work.

There are a few subtleties that make this work. Give it a go, if you get stuck I'll try help you out. Or you could always... Wait for a better solution lol, this was the first to come to me in a flash of inspiration.

Re: compatibility matrix

Posted: Mon Jan 24, 2011 5:33 pm
by Neilos
Or if you want to figure out your own way then;

Code: Select all

$query = "SELECT * FROM table_1";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
Will help you get number of entries.

also;

Code: Select all

while ($row = mysql_fetch_array($result, MYSQL_ASSOC) {

// some code 

}
Will help you iterate through table 3.

Re: compatibility matrix

Posted: Mon Jan 24, 2011 6:00 pm
by adc123
Thank you ,
defiantly will help ill post and share my end results