There is a much easier way to look through results.
I don't understand why you are running the query twice either, once would be enough. Maybe a copy paste error here?
Main Topics
Browse All TopicsI have a table called "customers". This has all the customer information, such as billing and service addresses and other information about the customer.
I also have a table called "customerservicedate". This table holds all service data that is done to any set of customers. Basically, it has a CustomerID (which is tied in with the customers table), and also has information such as service notes (in this case how much a customer has paid), the amount the service costed the customer, and if the service was provided, and if it was paid for, among other things.
This is where I'm having some issues. I think I might have the SQL syntax pretty much correct, however, that's 1/2 of the issue:
SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.Custom
This code DOES do what it is suppose to, however, it is a little slow - but it DOES return a good 3200 rows of information. This is for a report though, so that is okay, since it's not like its going to be used every day - its more of a monthly type ordeal. Let me know if I'm doing this query correct or not (as in if there are better ways about going about it).
So, what I'm actually trying to do is basically get a list of all the customers that have had service (up until the current date) and have not paid, then I need to join the customers table, so that I can get the Service Address/Billing address, etc. That's what the above statement does. Now, my problem is getting this to output correctly for PHP.
I REALLY need help on the PHP logic. I'm basically invoicing these, for late payments, and output to PDF using fPDF. I'm getting the hang of fPDF, however, my issue is this: I need to be able to loop through the SQL query and output everything that is late for each customer, and loop through for each instance. So basically run the query, and then output "Customer 1" and each result from the database, and then it loops again, and does "Customer 2", and each result from the database that pertains from this customer, and so on, until its all done. So what is unique would be the CustomerID.
Here's the loop, but I don't think it covers what I really need - I belive I might need a for each loop, and that I need help with:
$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.Custom
$num = mysql_num_rows($sql);
$i=0; // row count
while ($i < $num) {
$row = mysql_fetch_assoc($sql);
// this is where each output will happen
$i++
}
I'm not sure if a while look is what I want, or if I should have a while loop, but also have a foreach loop inside of it. If anyone could help out, that would be great.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
NerdsOfTech:
Ok partially works. Your code does output, however, the problem I am having is this. How could I take each "chunk" of a particular customer (for instance all the results for CustomerID 22) and place those results into a list, and do it for each customer. So, in theory, if I were to do an itemized list from it, I'm looking to do something like the following:
Customer 22:
-- output
-- output
-- output
-- output
-- output
-- output
Customer 23:
-- output
-- output
-- output
-- output
-- output
-- output
Customer 24:
-- output
-- output
-- output
-- output
-- output
-- output
So, what PHP would need to be written to iterate through the mySQL results to get this type of result?
@NerdsOfTech,
This code does output "Customer 1" ... "Customer 2" ... "Customer 3", however, I only used that as an example. So you did exactly what I said, but not as I wanted - I do thank you for that. I'll explain a little better :)
I would like the output to be as follows:
Customer ID FROM DB (ex. ID 9):
--- output of all results from database (if there are 10 results with ID 9, then list them all here)
Another Customer ID from DB (ex. ID 23):
--- out of all the results from the database (if there are 10 results with ID 23, then list them all here).
... etc etc...
Is there a way to do this?
Thanks for your help so far! :)
@NerdsOfTech:
OK, we are SOO close on this one. This is what happens with your code:
Let's say that there is 10 results for Customer ID 9 ...
Customer ID from DB (customer id 9)
-- output results
Customer ID from DB (customer id 9):
-- output results
...... 8 more just like this for customer id 9...
Then it does the same for the next record.
What I am really wanting to do is output ALL of one customer, in a group (the customer ID) and then all the results under that.. like this:
Customer ID from DB (customer ID 9):
-- all output from all the results ID 9
Customer ID from DB (customer ID 23):
-- all output from all the results for ID 23
.. etc etc...
Is this possible?
Ya, this still does the same thing:
Customer ID 9:
-- output
Customer ID 9:
--output
...all the way down to the next record...
...then...
Customer ID 23:
--output
Customer ID 23:
-- output
...all the way down to the next record...
Hmmm. Thanks for the help, do you need me to provide any other information to help?
@NerdsOfTech:
One more question about this output, then I'm assigning out points.
This section:
foreach ($row as $key => $value){
if ($key != 'CustomerID'){
$output = ' <tr>';
$output .= ' <td>' . $key . '</td>'; // comment out this line if you dont want fieldnames for each block and add colspan="2" for next lines <td>
$output .= ' <td>' . $value . '</td>';
$output .= ' </tr>';
echo $output;
}
}
How would I be able to get the same funtionality, however, choose what information I want to output. so instead of outputing everything, say I just want to output a certain table entry or 5 table entries, for ex - service date/type/fee/customer name/customer phone.
As of right now, it basically goes through and outputs everything. I don't want everything outputed and I fumbled around with the code somewhat but was never able to figure it out.
You could do this multiple ways:
1. (RECCOMMENDED METHOD) SQL:
change the select statement to SELECT the desired fields
instead of:
SELECT * FROM ...
you would do
SELECT CustomerID, CustomerName, ...etc FROM
2. PHP: inside of the foreach you could add additional exemptions from the loop by including the fields in the if statement
if ($key != 'CustomerID' || $key != 'SomeOtherField1' || $key != 'SomeOtherField2'){
-OR-
3.PHP: instead of a foreach () you could just reference the fieldnames via $row[] array
without foreach...
{not recommend becuase of the formatting needs of this case}
$row['CustomerID]';
$row['CustomerName'];
NerdsOfTech:
Let me explain my situation real quick and see if you can point me into the right direction. Based of your information above, it seems like the "recommended" way is more "tabular". I could be totally wrong here. However, it doesn't make sense to me for what I'm trying to do. I actually need the following fields from the database (for output):
CustomerID, ServiceAddress, Billing ADdress, Date of service, ServiceFee, fuel surcharge - also i'm going to be grabbing each service fee and adding it together for a total.
This SQL statement is actually for a report I'm creating for an on-the-fly PDF creation. So, basically, I'm going to be formatting this "output" a certain way. The way you recommended above seems too "tabular" as in you will basically have a "row" and a "value" side by side. I'm not sure if I just don't get it or if there's a better way to go about this, or if I just need to be told "this is how you do it".
I appreciate your help and await your response.
Thanks for the update.
The recommendation was how to "filter out" fields you didn't want outputted. It would be the same output but less fields shown.
Nonetheless, you can have the data "outputted" any way you want...we have the data. Just let me know how you want it to look:
one row per data row
| Customer ID | Service Address | Billing Address | Date of Service | Service Fee | Fuel Surcharge|
| 12345 | 111 some st | 111 some str | 8-21-2009 | 65.00 | 5.00 |
| 12346 | 111 some st | 111 some str | 8-21-2009 | 1215.00 | 15.00 |
| 12347 | 111 some st | 111 some str | 8-21-2009 | 135.00 | 5.00 |
two rows per data row
| 12345 | 111 some st | 111 some str
| 8-21-2009 | 65.00 | 5.00 |
| 12346 | 111 some st | 111 some str
| 8-21-2009 | 1215.00 | 15.00 |
| 12347 | 111 some st | 111 some str
| 8-21-2009 | 135.00 | 5.00 |
one row per field (current output)
| 12345
| 111 some st
| 111 some str
| 8-21-2009
| 65.00
| 5.00 |
| 12346
| 111 some st
| 111 some str
| 8-21-2009
| 1215.00 | 15.00 |
| 12347
| 111 some st
| 111 some str
| 8-21-2009
| 135.00
| 5.00 |
etc, etc.
Again, just let me know.
Thank you,
NOT:
I'm not sure how I want it to "look". I will tell you this. I will want the customer ID at the top of the PDF page, then to the right, I'll want the billing information for that particular customer. Then the the left of that I Want the service address. Below all of this, I'll want the service dates - I'll want "Date of service" - then the service fee at the end of that line, and then each date of service not paid for, I want it to be a line item.
Then after all this, I want a total. Which would be all the service fee's added together from SQL.
So the layout of where I want the individual items is going to be all laid out on the PDF doc, so I'll need to be able to store output into a variable and "stick" it wherever I want it.
As for the adding up totals, I will need to add all the ServiceAmount values for that certain CustomerID (for ex CustomerID 22), plus the FuelSurcharge value (right now we don't have any fuel surcharges, but when used, they will need to be factored in).
Thats what I'm needing. I just basically need a way to take the results, place them in a variable, and place them into a PDF, and create line items of the service details on a PER CUSTOMER ID basis.
Let me know :)
Of course. I can actually handle the PDF stuff - just getting the output into variables. You tell me the type of questions you want me to open with somewhat the information you are wanting. I'll make them 500 points a pop - and I'll work with you to get you the points you've deserved. Just tell me how many questions and what they need to entitle, and I'm more than willing.
I apologize. I meant to follow up on this but got extremely busy with a full-allocation-lock programming schedule (that is why I haven't answered questions for many weeks) and was unable to answer further.
@drewrockshard, Please add a "related question" for the pdf formatting to get help from other experts; so they can use the information from here.
Angeliii, I suggest close with points refunded to asker for his patience: @drewrockshard.
=NerdsOfTech
Business Accounts
Answer for Membership
by: cedlinxPosted on 2009-08-15 at 10:05:39ID: 25106087
I don't understand why you are repeating the same query. can you explain why because I think ONE instance of the SELECT query should suffice.