close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Displaying info from more than one table

Thread began 12/01/2014 6:09 am by Mags | Last modified 12/02/2014 10:18 am by Jason Byrnes | 1398 views | 7 replies |

Mags

Displaying info from more than one table

Having a nightmare trying to get some info to display correctly. I have 3 tables: users, accountants and clients. The common column in all of them is called JFBMicroID and contains a reference number in the format ABCD1234EFGH. It's basically a one to many to many relationship between the three tables: each user can have multiple accountants, and each accountant can have multiple clients.

I have a page set up in the users' members' area (created using SecurityAssist) where I want users to be able to view a list of all their accountants (identified by JFBMicroID), and also display the total number of clients that each accountant has, like this:


Firm Name Contact Name Email Address No. of Clients registered

Test Firm 1 Joe Bloggs test@firm1.com 4
Test Firm 2 John Doe test@firm2.com 8

I have a session variable on this page called JFBMicroID. The variable does work because elsewhere on the page I have a line that says:

Below is a list of all those who have registered so far using your Unique Registration Number: ABCD1234EFGH

However, the variable is not being picked up in the recordset. Instead it's displaying a list of all accountants regardless of their JFBMicroID. Actually, not all - 42 out of 115, not sure why! I'm also not sure how I would display the total number of clients next to each accountant.

The recordset currently looks like this:

$paramSession_rsAccountants = "-1";
if (isset($_SESSION['JFBMicroID'])) {
$paramSession_rsAccountants = $_SESSION['JFBMicroID'];
}
mysql_select_db($database_new_greenroom, $new_greenroom);
$query_rsAccountants = sprintf("SELECT * FROM accountants JOIN clients ON accountants.AccountantID = clients.AccountantID WHERE accountants.JFBMicroID = %s GROUP BY clients.AccountantID ORDER BY accountants.FirmName DESC", GetSQLValueString($paramSession_rsAccountants, "int"));
$rsAccountants = mysql_query($query_rsAccountants, $new_greenroom) or die(mysql_error());
$row_rsAccountants = mysql_fetch_assoc($rsAccountants);
$totalRows_rsAccountants = mysql_num_rows($rsAccountants);

Sign in to reply to this post

Jason ByrnesWebAssist

In the recordset, you have the data type for the paramSession variable set to integer, but it needs to be set to text. the value:
ABCD1234EFGH

is a string, so cannot be treated as an integer. using an integer data type is causing:
ABCD1234EFGH

to ne converted to:
1234

Sign in to reply to this post

Mags

Fantastic Jason, that's sorted out the session variable being recognised. However, I'm still having a problem displaying the correct info, and it's obviously the way I have the recordset set up, i.e. wrongly! I need to display all the accountants whether or not they have any clients - at the moment it's only displaying those accountants who have clients attached to them. I also don't know how to display the total number of clients that belong to each accountant - at the moment I have 3 accountants:

Accountant 1 has 4 clients
Accountant 2 has 2 clients
Accountant 3 has no clients

I need them to display on the page as follows:

Accountant 1 4
Accountant 2 2
Accountant 3 0

Sign in to reply to this post

Jason ByrnesWebAssist

change the join to a LEFT JOIN

see the following page for details on the different join types:
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

to get the number of records use the count function. see this page for more info:

http://www.w3schools.com/sql/sql_func_count.asp

you may also need to use a GROUP BY clause:
http://www.w3schools.com/sql/sql_groupby.asp

Sign in to reply to this post

Mags

Hi Jason

Thanks for the pointers - I've got it working using the following:

$paramSession_rsAccountants = "-1";
if (isset($_SESSION['JFBMicroID'])) {
$paramSession_rsAccountants = $_SESSION['JFBMicroID'];
}

and the recordset:

$query_rsAccountants = sprintf("SELECT *, COUNT(*)
FROM clients RIGHT OUTER JOIN accountants ON clients.AccountantID = accountants.AccountantID WHERE accountants.JFBMicroID = %s GROUP BY accountants.AccountantID, clients.AccountantID ORDER BY accountants.FirmName ASC",

It now displays the correct total number of clients for each accountant - except where there are no clients. If an accountant has no client, it's displaying 1 instead of 0. Any idea why this might be?

Sign in to reply to this post

Jason ByrnesWebAssist

because you counting all, which includes the accountant record, not counting the client IDs. for the count use:
COUNT(accountants.AccountantID)

Sign in to reply to this post

Mags

Thanks Jason, it gives me the correct result if I count on clients.AccountantID - many thanks for your help!

Sign in to reply to this post

Jason ByrnesWebAssist

you're welcome.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...