Solved

PHP Comparing values in SQL tables

Posted on 2010-11-30
3
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

733 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