Solved

PHP Comparing values in SQL tables

Posted on 2010-11-30
3
220 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

20 Experts available now in Live!

Get 1:1 Help Now