Skip to main content
Community Expert
November 29, 2012
Question

Specifying a column range for sorting data

  • November 29, 2012
  • 1 reply
  • 1032 views

I have a dabase called 'docSecurity' of permissions and categories for a list of documents stored on a server. Each document is may be assigned one or more categories and each page has several levels of permissions from read only to edit. Categories are stored as a string or a null in column 6 through 16 and permissions are stored in row 17 through 35. Each user on the system is also tied to the list of permissions and categories by their user ID using the same docSecurity table. User 10  may be able to read QC and QA documents associated with the Marketing and Followup category by having his permissions set to Read for QC and QA but may be able to Edit docs in  QA docs in the Followup category because his Editing permissions are only set to yes in QA / Marketing. All of the permissions and categories are working just fine and the logic is worked out.

Here's my problem. I want to now scan the 'docSecurity' table to find where matches occur. Here's what I'd like to do in English.

From the docSecurity table compare the categories and permissions for dockSecurity record 'currentUserID' with docSecurity record 'currentDocID' and show every field where categories and permissions match.

I have the query written successfully to pull out the currentUserID and report the row and pull out the currentDocID row. It's the comparrison that I'm having problems with. The solution I have come up with is a huge string of if statements and echos. It's very cumbersome and looks like this:

if ($row_currentUser[6] == $row_currentDoc[6]) {echo '<li>', $row_currentDoc[6], '</li>'; }

if ($row_currentUser[7] == $row_currentDoc[6]) {echo '<li>', $row_currentDoc[7], '</li>'; }

I repeat this for each of the rows until I get to the end. This makes a very big block of code and as documents are added to this list there can be additional columns added to the table. Eventually there could be hundreds of columns of categories. I can't be going back into the code every time a new column is added to write a new if statement to echo the matches.

I'd like to write something like this:

foreach($currentUser[6] as $key => $val) {

  echo "{$key} => {$val}\n";

}

but have it modified to do the comparrison described above.  Maybe something like this but I haven't been able to get it to work.

For x = 0 to Len(row1[])-1

    If row1 = row2 then

        echo the value

But I haven't been able to figure out how to put this into working PHP code.

This topic has been closed for replies.

1 reply

Participating Frequently
November 29, 2012

>Categories are stored as a string or a null in column 6 through 16

>and permissions are stored in row 17 through 35

Really!?  Why not create a separate table for these - will make your life much easier. Anytime I hear a request to search multiple columns for an occurance, I have to believe the data is not properly normalized.

Community Expert
November 29, 2012

It actually doesn't matter that the categories and permissions are in separate categories. They all have to be searched and each record has both. The problem I am having is that I want to get the results from column 6 through the last column. The total number of columns changes. I don't want to look through any of the data in column 1 thru 5. It's all housekeeping and matches there are common and irrelevant. the only unique column is the docSecurity id (first column and primary key).

I guess the simple question is how can echo the values from column 6 through the end. If I put

foreach($currentUser[6] as $key => $val) {   echo "{$key} => {$val}\n"; } in the setup I only get column 6, not 6 through the end of the list...


Participating Frequently
November 29, 2012

>They all have to be searched and each record has both.

Rick, what has me concerned is this statement:

>Categories are stored as a string or a null in column 6 through 16

Anytime you are storing the same type of data in more than one column, your data is not sufficiently normalized. Without knowing the complete schema, I would thing a much better option would be to create another table that stores each assigned category  in rows, not columns - this is much more efficient and allows much simpler queries and joins. For example, you would have a document table, category table and a document_category table. The document_category would have foreign keys to the document and category tables.

Anyway, getting back to your problem at hand, probably need to see all of the code to better understand what you are doing. What is $currentUser. Is that a result set or just one row?