Only get documents where ID of person logged in in array

I have a table that has a field called Users, and inside that table is an array which contains the ID’s of the users that are allowed to see that document.
What I need to do as you will see Im trying to do below is only get the documents that have the ID that corresponds with the ID of person who has logged in.


<table border="1">
<tr>
<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$t=mysql_query("select * from Categories WHERE (Active=1) ORDER BY ID");
while($g=mysql_fetch_assoc($t)){ 
$for = $g['ID'];
?>
<td><a href="#" title="<?php echo $g['Name']?> - Click to view all documents"><?php echo $g['Name']?></a></td>
<?php } ?>
<?php
var_dump ("select * from Documents Left JOIN UserAdmin on (UserAdmin.ID) WHERE (UserAdmin.ID='$cID') AND (Documents.Corporation='$CORPid') AND (Documents.User = '8' ) AND (Documents.User IN '$acorps4' ) AND (Documents.Active='1') AND (Documents.Category='$for') ORDER BY Documents.ID");
$k=mysql_query("select * from Documents Left JOIN UserAdmin on (UserAdmin.ID) WHERE (UserAdmin.ID='$cID') AND (Documents.Corporation='$CORPid') AND (Documents.User = '8' ) AND (Documents.User IN '$acorps4' ) AND (Documents.Active='1') ORDER BY Documents.ID");
while($d=mysql_fetch_assoc($k)){ ?> 
<tr>
<td><?php echo $d['Title']?></td>
</tr>
<?php } ?>
</tr> 
</table>

The var_dump outputs this -


string(265) "select * from Documents Left JOIN UserAdmin on (UserAdmin.ID) WHERE (UserAdmin.ID='8') AND (Documents.Corporation='6') AND (Documents.User = '8' ) AND (Documents.User IN '1,8,9,32,33' ) AND (Documents.Active='1') AND (Documents.Category='106') ORDER BY Documents.ID"  

So the person who has logged in this time has an ID of which relates to a table called UserAdmin, and then he choose a corporation which in this case had a value of 6, so what I need to do is recognise the 8 in the documents.user field and allow that particular document to show, under the category that in the table that has been created. But its now quite there, and Im not sure how to get there with what I got above.

Sorry if it sounds a bit complicated

Maybe I once again went over the top, so have tried it gain from the start, and basically all I need is this for the time being, just to get it working, but I dont know how to get it to look into the array thats got and look to see if it contains a certain value, and then allow it to show.

In relation to the one below, I need it to recognise the value 8 in the array



string(88) "select * from Documents WHERE (Corporation='6') AND (User IN '1,8,9,32,33' ) ORDER BY ID" 

$k=mysql_query("select * from Documents WHERE (Corporation='$CORPid') AND (User IN '$acorps4' ) ORDER BY ID");

<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$t=mysql_query("select * from Categories WHERE (Active=1) ORDER BY ID");
while($g=mysql_fetch_assoc($t)){ 
$for = $g['ID'];
?>
<td><a href="#" title="<?php echo $g['Name']?> - Click to view all documents"><?php echo $g['Name']?></a></td>
<?php } ?>
<?php
var_dump ("select * from Documents WHERE (Corporation='$CORPid') AND (User IN '$acorps4' ) ORDER BY ID");
$k=mysql_query("select * from Documents WHERE (Corporation='$CORPid') AND (User IN '$acorps4' ) ORDER BY ID");
while($d=mysql_fetch_assoc($k)){ ?> 
<tr>
<td><?php echo $d['Title']?></td>
</tr>
<?php } ?>
</tr> 
</table>


I looked into it a little bit and maybe array_search is what Im looking for as the var_dump is showing it correctly as below


var_dump ("select * from Documents WHERE (Corporation='$CORPid') AND (array_search($cID, $acorps4)) ORDER BY ID");

$k=mysql_query("select * from Documents WHERE (Corporation='$CORPid') AND (array_search($cID, $acorps4)) ORDER BY ID");



string(93) "select * from Documents WHERE (Corporation='6') AND (array_search(8, 1,8,9,32,33) ORDER BY ID" 

Im not getting the output though, so maybe its not right to use it

No I can see whats wrong, im just saying that yes there is a match, but it doesnt relate to anything does it, as it needs to be related to the field User within the table Documents.

mmm, such as:


$k=mysql_query("select * from Documents WHERE (Corporation='$CORPid') AND (User=(array_search($cID, $acorps4))) ORDER BY ID");

I’m thinking

"select * from Documents WHERE Corporation = '$CORPid' AND User IN ($acorps4) ORDER BY ID"

If the values need to be quoted try this.


$acorps4 = (is_array($corpIDs4) ? "'".implode("','", $corpIDs4)."'" : 0);

Note added default 0 so query doesn’t throw error.

Cheers Drummin,

I at least find it funny when I really do take the long road around, and then its always far simpler than I thought, but thanks.

That is sort one column in the table, but I really need it to loop through the category names and output the documents for each category vertically.

Can you see what Im trying to do from the code below.

The tabel has horizontal columns and at the top of each column sits a category name, and what should happens when a user logs in is that in each column should sit the documents associated with the column and the user.

At the moment, the first column has been created but the search in Documents needs to skip through the Categories.


<table border="1">
<tr>
<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$t=mysql_query("select * from Categories WHERE (Active=1) ORDER BY ID");
while($g=mysql_fetch_assoc($t)){ 
$for = $g['ID'];
?>
<td><a href="#" title="<?php echo $g['Name']?> - Click to view all documents"><?php echo $g['Name']?></a></td>
<?php } ?>
<?php
$k=mysql_query("select * from Documents WHERE Corporation = '$CORPid' AND User IN ($acorps4) ORDER BY ID");
while($d=mysql_fetch_assoc($k)){ ?> 
<tr>
<td><?php echo $d['Title']?></td>
</tr>
<?php } ?>
</tr> 
</table>


Hope that makse sense

It’s really a bad idea to be making looped queries like this. It can get out of hand really fast. I was helping someone the other day who had three queries in a loop. They didn’t think it was a problem so I added a counter for each query and they were running between 3000-8000 queries to build the page. It was crazy. You may not be dealing with this amount but I bet you could do what you want with a single query.

Then again, your DB model is not ideal by having comma separated strings in the field.

Just think how much simpler things would be if you kept corporation ID and user ID’s in a separate table.

CREATE TABLE `test`.`corporation_users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`corporation_id` INT NOT NULL ,
`user_id` INT NOT NULL ,
PRIMARY KEY ( `id` )
)

Then storing a single user id in each row, it would be very easy to work with only corporations what have a specific user_id, or to grab all users for a specific corporation.

Is this a new project you are working on, or are you dealing with an existing project/database?

Hi Drummin, sorry ran out of time last night.

I got to be honest and say I havent used anything like that above before, and although I think I get it i wouldnt know where to start with it and how to implement it into what I need.

This is an update on a already exisiting project, so I’ve come to it blind.

I know this is going to sound like the wrong thing to say, but I’m not here long sort of thing, and this was hopefully just a quick update.

And I understand that the loop issue is wrong, big time, but what i got below is sort of what they want, but obviously im not getting it right.

Could below be adapted to get this project ouit of the way.


<table border="1">
<tr>
<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$t=mysql_query("select ID, Name, Active from Categories WHERE (Active=1) ORDER BY ID");
while($g=mysql_fetch_assoc($t)){ 
$for = $g['ID'];
?>
<td><a href="#" title="<?php echo $g['Name']?> - Click to view all documents"><?php echo $g['Name']?></a></td>
<?php } ?>
<?php
$k=mysql_query("select ID, Title, Description, Active, User, Corporation, Category, Document from Documents WHERE Corporation = '$CORPid' AND User IN ($acorps4) ORDER BY ID");
while($d=mysql_fetch_assoc($k)){ ?> 
<tr>
<td><?php echo $d['Title']?></td>
</tr>
<?php } ?>
</tr> 
</table>


I have got log in details etc if you need them

Something like this works, but what happens is they all row up underneath each other, when what I need is for the categories to row up across the page and the documents underneth them.


<table border="1">
<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$t=mysql_query("select ID, Name, Active from Categories WHERE (Active=1) ORDER BY ID");
while($g=mysql_fetch_assoc($t)){ 
$for = $g['ID'];
?>
<tr>
<td><a href="#" title="<?php echo $g['Name']?> - Click to view all documents"><?php echo $g['Name']?></a></td>
<?php
$k=mysql_query("select ID, Title, Description, Active, User, Corporation, Category, Document from Documents WHERE Corporation = '$CORPid' AND User IN ($acorps4) AND Category=$for ORDER BY ID");
while($d=mysql_fetch_assoc($k)){ ?> 
<tr>
<td><?php echo $d['ID']?> - <?php echo $d['Title']?></td>
</tr>
<?php } ?>
</tr> 
<?php } ?>
</table>


Might be a css issue now, as its outputting ok, but the categories are tyling vertically, and the documents horizontally off them.


<table border="1" align="center">
<tr>
<td style="background-color:#ccc;"><a href="#" title="Memorandum and Articles - Click to view all documents">Memorandum and Articles</a></td>
&#12288;
<td>15 - Memorandum & Articles (Keenton International Limited)</td>
<td>17 - Certificate of Incorporation Keenton International Limited</td>
<td>18 - Incorporation form NC1 Keenton International Limited</td>
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Patents - Click to view all documents">Patents</a></td>
&#12288;
<td>16 - Intellectual Property Assignment (Keenton International Limited)</td>
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Shareholders & Share Certificates - Click to view all documents">Shareholders & Share Certificates</a></td>
&#12288;
<td>23 - Shareholder List of Keenton International Limited</td>
<td>40 - Keenton International Limited Shareholders and Directors contact list</td>
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Directors and Secretaries - Click to view all documents">Directors and Secretaries</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Accounting - Click to view all documents">Accounting</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Financials - Click to view all documents">Financials</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Bank Accounts - Click to view all documents">Bank Accounts</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Management - Click to view all documents">Management</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Location - Click to view all documents">Location</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Business Plans - Click to view all documents">Business Plans</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Support Contracts - Click to view all documents">Support Contracts</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Supplier Contracts - Click to view all documents">Supplier Contracts</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Other Contracts - Click to view all documents">Other Contracts</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="NDA Contracts - Click to view all documents">NDA Contracts</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Products - Click to view all documents">Products</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Relaunch Products - Click to view all documents">Relaunch Products</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="New Products - Click to view all documents">New Products</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Accreditations - Click to view all documents">Accreditations</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Marketing and Sales - Click to view all documents">Marketing and Sales</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Marketing (The Channel) - Click to view all documents">Marketing (The Channel)</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Marketing Documents - Click to view all documents">Marketing Documents</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Awards & PR - Click to view all documents">Awards & PR</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="News and Press Releases - Click to view all documents">News and Press Releases</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Stock X Gem - Click to view all documents">Stock X Gem</a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Offers to Buy / Sell - Click to view all documents">Offers to Buy / Sell </a></td>
&#12288;
</tr>
<tr>
<td style="background-color:#ccc;"><a href="#" title="Information Request - Click to view all documents">Information Request</a></td>
&#12288;
</tr>
</table>


With


<table border="1">
<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$t=mysql_query("select ID, Name, Active from Categories WHERE (Active=1) ORDER BY ID");
while($g=mysql_fetch_assoc($t)){ 
$for = $g['ID'];
?>
<tr>
<td style="background-color:#ccc;"><a href="#" title="<?php echo $g['Name']?> - Click to view all documents"><?php echo $g['Name']?></a></td>

<?php
$k=mysql_query("select ID, Title, Description, Active, User, Corporation, Category, Document from Documents WHERE Corporation = '$CORPid' AND User IN ($acorps4) AND Category=$for ORDER BY ID");
while($d=mysql_fetch_assoc($k)){ ?> 
<td><?php echo $d['ID']?> - <?php echo $d['Title']?></td>
<?php } ?>
</tr>
<?php } ?>
</table>


The code below as I have it now, means that the categories line themselves up horizontally across the page, but something strange is happening in that the code for the documents to product the rows isnt being recognised at all. This seems the only way to have the colums line up correctly.

This part up works fine:


<table class="gridtable">
<tr>
<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$t=mysql_query("select ID, Name, Active from Categories WHERE (Active=1) ORDER BY ID");
while($g=mysql_fetch_assoc($t)){ 
$for = $g['ID'];
?>
<th style="background-color:#ccc;"><a href="view.php?corp_ID=<?php echo $CORPid ?>&category=<?php echo $for ?>" title="<?php echo $g['Name']?> - Click to view all documents"><?php echo $g['Name']?></a></th>
<?php } ?>
</tr> 


the second bit doesnt run


<?php
$k=mysql_query("select ID, Title, Description, Active, User, Corporation, Category, Document from Documents WHERE Corporation = '$CORPid' AND User IN ($acorps4) AND Category=$for ORDER BY ID");
while($d=mysql_fetch_assoc($k)){ ?>
<tr>
<td><?php echo $d['ID']?> - <?php echo $d['Title']?></td>
</tr>

<?php } ?>
</table>


All the values in var_dump are fine too.


string(170) "select ID, Title, Description, Active, User, Corporation, Category, Document from Documents WHERE Corporation = '6' AND User IN (1,8,9,32,33) AND Category=106 ORDER BY ID"

Ye there no connection is there, between the data inside the category fields and what to allow to appear below it…

oh my god!

I dont know, times like these, I realise I got a while to go yet to be a decent developer

[QUOTE=Drummin;5658661]Just think how much simpler things would be if you kept corporation ID and user ID’s in a separate table.

CREATE TABLE `test`.`corporation_users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`corporation_id` INT NOT NULL ,
`user_id` INT NOT NULL ,
PRIMARY KEY ( `id` )
)

QUOTE]

My way by the looks is not the way, unless there an option that someone could show, so been looking at Drummins suggestion and cant work it out what to do and how t use it.

Change of tact now then, have put the below together as an attempt at another way, and im getting the correct number of columns created, with the category names.


 
<?php
$query = "select ID, Name, Active from Categories WHERE (Active=1) ORDER BY ID";
 $result = mysql_query($query) or die ("Query failed");
 
//get the number of rows in our result so we can use it in a for loop
 $numrows = (mysql_num_rows ($result));
 
// loop to create rows
 if($numrows >0){
 echo "<table class='gridtable'>";
 // loop to create columns
 echo "<tr>";
 while ($List = mysql_fetch_array($result)){
 echo " <td>".$List['Name']."</td> ";
 }
  echo "</tr> ";
echo $end."</table> ";
 }//if
 ?>


Will see if this works out, but if anyone wants to chip in, it will be great as need now to produce the contracts beneath the category titles.

source



<table class='gridtable'><tr> <td>Memorandum and Articles</td> <td>Patents</td> <td>Shareholders & Share Certificates</td> <td>Directors and Secretaries</td> <td>Accounting</td> <td>Financials</td> <td>Bank Accounts</td> <td>Management</td> <td>Location</td> <td>Business Plans</td> <td>Support Contracts</td> <td>Supplier Contracts</td> <td>Other Contracts</td> <td>NDA Contracts</td> <td>Products</td> <td>Relaunch Products</td> <td>New Products</td> <td>Accreditations</td> <td>Marketing and Sales</td> <td>Marketing (The Channel)</td> <td>Marketing Documents</td> <td>Awards & PR</td> <td>News and Press Releases</td> <td>Stock X Gem</td> <td>Offers to Buy / Sell </td> <td>Information Request</td> </tr> </table> 


The problem is the two queries needing to be connected whilst keeping the structure if the table, which I’m unable to do.

Have re-worked the code, so hopefully thats improved, but just cant get my head around it. The categories are being outputted fine across the top, but the rows beneath each category arent, but only appear when I put the second query inside the first query, but then the structure of the table is lost.


<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$query = "select ID, Name, Active from Categories WHERE (Active=1) ORDER BY ID";
$result = mysql_query($query) or die ("Query failed");
 
//get the number of rows in our result so we can use it in a for loop
 $numrows = (mysql_num_rows ($result));
 
// loop to create rows
 if($numrows >0){
 echo "<table class='gridtable'>";
 // loop to create columns
 echo "<tr>";
 while ($List = mysql_fetch_array($result)){
  $for = $List['ID'];
 echo " <td>".$List['Name']."</td> ";
 }
  echo "</tr> ";
  echo "<tr>";
$k=mysql_query("select ID, Title, Description, Active, User, Corporation, Category, Document from Documents WHERE Corporation = '$CORPid' AND User IN ($acorps4) AND Category='$for' ORDER BY ID");
while($d=mysql_fetch_assoc($k)){
echo " <td>".$d['ID']." - ".$d['Title']."</td> ";
}
echo "</tr> ";
echo $end."</table> ";
 }
 ?>


Thought maybe connecting two fields in 2 tables up, but that didnt do it either.

But then reduced the query on the second one, and I got some results, so it might be to do with that, but again the docs scrolled horizontally rather than vertically underneath the categories.

So all the docs came out but they all outputted in one line horizontally.


<?php
$query4  = "select User from Documents WHERE Corporation = $CORPid";
$result24 = mysql_query($query4);
$rowu4 = mysql_fetch_array($result24, MYSQL_ASSOC); 
$corpIDs4 = explode(";",$rowu4['User']);
$acorps4 = implode(',', $corpIDs4);
$query = "select ID, Name, Active from Categories WHERE (Active=1) ORDER BY ID";
$result = mysql_query($query) or die ("Query failed");
 
//get the number of rows in our result so we can use it in a for loop
 $numrows = (mysql_num_rows ($result));
 
// loop to create rows
 if($numrows >0){
 echo "<table class='gridtable'>";
 // loop to create columns
 echo "<tr>";
 while ($List = mysql_fetch_assoc($result)){
  $for = $List['ID'];
 echo " <th><a href='view.php?corp_ID=$CORPid&category=$for' title='$List[Name]'>$List[Name]</a></th> ";
 }
  echo "</tr> ";
  echo "<tr>";
$k=mysql_query("select Documents.ID, Documents.Title, Documents.Description, Documents.Active, Documents.User, Documents.Corporation, Documents.Category, Documents.Document from Documents  ORDER BY Documents.ID");
while($d=mysql_fetch_assoc($k)){
echo " <td>".$d['ID']." - ".$d['Title']."</td> ";
}
echo "</tr> ";
echo $end."</table> ";
 }
 ?>


I cant take this to where it needs to be now, its just not right, but cant work it out.

So then changed this by adding a field from another table and moved the second lot of <tr>'s inside the loop, which resulted in all the documents listing up underneath the first category, slight progress.


$k=mysql_query("select Documents.ID, Documents.Title, Documents.Description, Documents.Active, Documents.User, Documents.Corporation, Documents.Category, Documents.Document from Documents LEFT Join Categories on (Categories.ID = Documents.Category) ORDER BY Documents.ID");
while($d=mysql_fetch_assoc($k)){
 echo "<tr>";
echo " <td>".$d['ID']." - ".$d['Title']."</td> ";
 echo "</tr> ";
}

But they need to recognise the category and only show the docs associated with it