If I could talk to my database table, I would say "if client_id and salesrep_id are in the same row, do this!
Most of the conditional logic I've got. It's the "in the same row" part I'm struggling with.
Essentially, I'm building a system that allows all sales reps to view the entire data table but will permit each rep to update only their own specific entries.
The idea is to disable everybody else's "EDIT" links in the display.
Like so ...
<?php if ($row_listClients['client_id'] && $row_listClients['salesrep_id']) { ?>
<td><a href="edit_clients.php?client_id=<?php echo $row_listClients['client_id']; ?>">Edit</a>
</td>
<?php } else { ?>
<td>Edit</td>
<?php } ?>
Obviously the initial condition doesn't work ( && will always be true ) but everything else works great. It's just this one bit I'm stuck on.
Hope that makes sense, I know it's probably pretty basic stuff.
Thanks a bunch guys, any pointers would be really appreciated.
--
Here's the query :
$query_listClients = "SELECT client_id, user_id, repname, date, client_type, company,
contact_firstname, contact_lastname, phone, email, property, address, city, province,
repnotes FROM clientdata ORDER BY company ASC";
$listClients = mysql_query($query_listClients, $connAdmin) or die(mysql_error());
$row_listClients = mysql_fetch_assoc($listClients);
Thanks again ...
--
Sample of database data - which I entered as a test only. The first two number are client_id and user_id respectively:
19;"33"; "Ronald Wharton"; "March 20 2013"; "Commercial"; "Bonkers Inc."; "Brett";
"Bonkers"; "987 654 3211"; "brentbonkers@bonkersinc.com"; "Retail Outlet";
"12765 34 Ave"; "Lost Soul City"; "Sask"; "Brett wants to meet with Keith and Dean
before the end of the week. Please call before Friday."
Don't know if that's any help, but there ya go, thanks.
I reread your question. It sounds as if you're querying to get all records, but only want to show edit links for items a particular sales rep should be allowed to edit.
Since you want all records, this shouldn't be done via SQL, and it sounds like all you need to do is change:
<?php if ($row_listClients['client_id'] && $row_listClients['salesrep_id']) { ?>
To something like:
<?php if ($row_listClients['salesrep_id'] == $currentSalesRepId) { ?>
You'll obviously need to use the appropriate variable for the $currentSalesRepId
.
You should store the current salesrep_id (the one who is logged in) in a session and then compare it with the salesrep_id that is returned by mySql query (in the example below i have taken the current logged in salesrep_id equal to 1)
add a WHERE clause to your SQL statement like so:
SELECT * FROM $tableName WHERE salesrep_id = 1
that you only get the rows where salesrep_id = 1
OR if you want to get all the rows from the database and then
SELECT * FROM $tableName
and then
// check if returned salesrep_id = current Sales Rep. Id (1 in this example)
<?php if ($row_listClients['client_id'] && $row_listClients['salesrep_id'] == 1) : ?>
// Show the edit link
<?php endif; ?>