Solved

PHP Comparing values in SQL tables

Posted on 2010-11-30
3
221 Views
Last Modified: 2012-05-10

I have two tables (A & B). I want to list on my PHP page all the values from table A and a Yes next to each record if there is a matching record in table B.

Suppose we have the following:
Table A      Support Categories                  
| ID      | Description                      |
| 1      | CV Writing Assistance    |
| 2      | Job Search Assistance   |
| 3      | Interview Skills                |

Table B      
| ClientID  |      SupportID  (references table A  ID)  |      SupportStarted     |      SupportStopped
| C1         |      1                                                       |      01/06/10              |            01/06/10
| C2         |      1                                          |      01/06/10      
| C1         |        2                                          |      05/07/10             |             06/07/10
| C1         |      2                                          |       09/09/10      
| C4         |      1                                          |       19/08/10      

I want to show for a given client whether they have started to receive support and whether they have any opened support areas  (not stopped) in my PHP page.

For example Example.php
---------------------------------------------------------------------------------------------------------
CLIENT ID 1

Description                           |     Support Started       |     Open Support

CV Writing Assistance      |           Yes                            |          No
Job Search Assistance      |           Yes                            |         Yes
Interview Skills      |             No                            |          No

_____________________________________________________________________________

Normally I would do the following.
1.      Run a query which returns all rows from Table A
2.      Use a While() loop to display each row from table A.
3.      Within the While() loop query each record from table B to look for matching rows.
See attached code.
I was wondering if there was a better (more efficient) way of doing this without having to repeat the second query each time a row is loaded from the first query?
Is there any way to compare query results whilst they are in the array?

Example.php
0
Comment
Question by:EICT
3 Comments
 
LVL 13

Accepted Solution

by:
darren-w- earned 200 total points
ID: 34239641
Might be better doing this in sql

something like

select ClientID, description, 'yes' as Support_started, 'yes' as 'open_support'
from table_b b
left join table_a a
ON b.SupportID = a.ID
where b.ClientID = c1

then just printing this output

there will need to be a little more logic applied to display the support statuses.

D
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 34239692
There are some things not worth optimizing, and this might be one of them.  If you do not have thousands of rows, and your data base is well-indexed, you might be OK with almost any query structure!
0
 

Author Closing Comment

by:EICT
ID: 34270144
The query was wrong but the principle is there. Try to do as much in mysql first to you only have to run the query once.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now