Link to home
Start Free TrialLog in
Avatar of any_
any_Flag for France

asked on

mysql tables php select where issue

Hi
I've got two tables : one concerns projects informations, the other one many documents. User can attach a document to a project.

projects table
project_id       | project_name       | project_description       | project_country
1                  | A                        | AAAAAAAAAAAAA      | France
2                  | B                        | BBBBBBBBBBBBB      | Guinea
3                  | C                        | CCCCCCCCCCCCC      | Cameroun
4                  | D                        | DDDDDDDDDDDDD      | Senegal
5                  | E                        | EEEEEEEEEEEEE             France

documents table
doc_id             | doc_name                   | doc_description             | doc_project_id
1                  | DOCA                        | AAAAAAAAAAAAA      | 1
2                  | DOCB                        | BBBBBBBBBBBBB      | 2
3                  | DOCC                        | CCCCCCCCCCCCC      | 1
4                  | DOCD                        | DDDDDDDDDDDDD      | 5
5                  | DOCE                        | EEEEEEEEEEEEE            | 4

I've got no problem here. User put project id inside input field doc-project_id in the "document.php" form. That allow me to display all document attached to a project in table :
project-documents.php?id=(project_id)

Well, the problem is that use should be able to attach several documents to more than one project. Example :

doc_id             | doc_name                   | doc_description             | doc_project_id
1                  | DOCA                        | AAAAAAAAAAAAA      | 1, 5, 3
2                  | DOCB                        | BBBBBBBBBBBBB      | 2
3                  | DOCC                        | CCCCCCCCCCCCC      | 1
4                  | DOCD                        | DDDDDDDDDDDDD      | 5, 4, 1

What kind of ajax code can i use in my "document.php" form to add or delete project_id values inside the doc_project_id input field

What kind of php code  can i use in order to select documents attached to a project if in my documents table  the doc_project_id column contains multiples values. For instance in my last table example

the project 1 should return doc 1 3 and 4
the project 5 should return doc 1 and 4
the project 4 should return only doc 4
the project 2 should return only doc 2

Thank for your help
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

I guess this would be helpful for selecting multiple projects for a one document.
http://www.siteexperts.com/tips/html/ts16/page1.asp

And about the column: you can use explode function to get the values of the assigned projects to an array:
$projects = explode(",",$doc_project_id);
ASKER CERTIFIED SOLUTION
Avatar of Ovid Burke
Ovid Burke
Flag of Saint Vincent and the Grenadines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of any_

ASKER

Thanks to your all

madaboutasp and Ray_Paseur propose to create a junction or "pivot" table. That was I thinking about too at the beginning . But that's mean for example :
If 1000 documents
and each documents is attached to only 2 projects
I will have a junction/"pivot" table with 2000 rows. Is that correct ?

Of course it seems to me easier to add / delete document attachement.
But i was thinking at the beginning  it was an easiest way to store the "attachement" values in the document table even it seems difficult to manage the possibilty to delete or add document attachement inside a value "list" inside one field.

That approach is doable, but you will find it quite a bit more tricky. If you are comfortable with converting strings to arrays, arrays to strings, and adding/subtracting keys/values, etc., then you could approach it that way.
A table with x documents and y projects will not necessarily have the number of rows determined by the product of x and y.  You would want to estimate the intersection of the sets to estimate the number of rows.  BTW, a junction or pivot table of 2,000 rows (or 20,000 rows) is nothing to modern data base servers.  The key issue here is that the "relational" nature of the data base is exploited by using this technique.  I would not propose any other way of establishing a many-to-many relationship, and I would not propose having a column that contains a list of data elements.

This search will lead you to some interesting and informative thinking about data base structure:
http://lmgtfy.com?q=Should+I+Normalize+my+Database
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.