?
Solved

mysql tables php select where issue

Posted on 2011-02-18
8
Medium Priority
?
227 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 111

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 143

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

800 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