Solved

mysql tables php select where issue

Posted on 2011-02-18
8
223 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:any_
8 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34925855
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);
0
 
LVL 11

Accepted Solution

by:
Ovid Burke earned 125 total points
ID: 34925952
The approach you are using could become a little cumbersome and problematic when you begin to add/delete project ids from you documents table. I would suggest that you improve the relational model in your database by adding a junction table, say projects_documents that would contain project_id and document_id. This way you can easily create/drop many-to-many relationships between you project and documents.

CREATE TABLE `projects_documents` (
  `project_id` int(10) unsigned DEFAULT NULL,
  `document_id` int(10) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 34925982
The thing you want to use here is called a "pivot table" or something like that.  It is a little table with two columns.  One column is the key of the project and the other column is the key of the document.  You have a many-to-many relationship between projects and documents.  This is sort of like how Facebook knows who you are friends with.

To establish a relationship, you add a row to the table with the two keys.  To remove a relationship, you remove the row(s) that match both the project and document keys.

If the number of projects and documents is large the pivot table can get quite long.  In that case you want to be sure to add indexes on both columns.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:any_
ID: 34929101
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.

0
 
LVL 11

Expert Comment

by:Ovid Burke
ID: 34929250
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.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34929489
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
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37307694
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question