Question

PHP/MySQL foreach Unique output

Asked by: drewrockshard

I 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.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';

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.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");

$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.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-15 at 08:13:19ID24655234
Tags

foreach

,

while

,

php

,

mysql

,

database

Topics

PHP and Databases

,

MySQL Server

,

PHP Scripting Language

Participating Experts
4
Points
0
Comments
48

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. foreach -to- while
    I need to covert a threaded (recursive) comment system from PHP/MySQL -to- PHP/ADOdb. However, I am having minor problems with using foreach() loops instead of while() loops, because ADOdb has no "fetch array" functions per say. Instead of "posting" all o...
  2. Foreach in foreach, or next best thing
    How can I use PHP to do a foreach in a foreach without getting an error? Or just as good, how can I accomplish the same thing without nesting foreaches?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

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.

 

by: pmessanaPosted on 2009-08-15 at 10:34:37ID: 25106177

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?

<?php 
do {
 
   $field1 = $row['Customer Field1'];
   $field2 = $row['Customer Field2'];
 
} while ($row = mysql_fetch_assoc($sql));
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: cedlinxPosted on 2009-08-15 at 10:35:12ID: 25106184

Actually with one select query, you have JUST ONE record per customer and the loop you have here should work.

 

by: NerdsOfTechPosted on 2009-08-15 at 11:19:22ID: 25106344

Your sql should only have one query.

Here is the data outputted in a HTML table :)

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';";
$num = mysql_num_rows($sql);
if ($num > 0){
echo '<table>';
$x=0;
 while ($row = mysql_fetch_assoc($sql)){
  if ($x == 0){ // output field names ONCE
   $x=1;
   echo ' <tr>';
   foreach ($row as $key => $value){
     echo '  <td>' . $key . '</td>'; 
   }  
   echo ' </tr>';
  }
 
  echo ' <tr>';
  foreach ($row as $value){
     echo '  <td>' . $value . '</td>'; 
  }
  echo ' </tr>'; 
 }
echo '</table>';
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:

Select allOpen in new window

 

by: NerdsOfTechPosted on 2009-08-15 at 11:20:39ID: 25106349

Typo use this instead:

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");
$num = mysql_num_rows($sql);
if ($num > 0){
 echo '<table>';
 $x=0;
 while ($row = mysql_fetch_assoc($sql)){
  if ($x == 0){ // output field names ONCE
   $x=1;
   echo ' <tr>';
   foreach ($row as $key => $value){
     echo '  <td>' . $key . '</td>'; 
   }  
   echo ' </tr>';
  }
 
  echo ' <tr>';
  foreach ($row as $value){
     echo '  <td>' . $value . '</td>'; 
  }
  echo ' </tr>'; 
 }
 echo '</table>';
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:

Select allOpen in new window

 

by: drewrockshardPosted on 2009-08-15 at 13:57:14ID: 25106893

Guys,

Ya, that was a type.  I'll test and try implementing the code and give results as I get them :)

Thanks!

 

by: drewrockshardPosted on 2009-08-15 at 13:57:46ID: 25106896

** TYPO - I even had a typo on the word typo - sorry (long day).

 

by: drewrockshardPosted on 2009-08-15 at 14:23:40ID: 25106971

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?

 

by: NerdsOfTechPosted on 2009-08-16 at 12:37:20ID: 25110129

Large data friendly (limited echo calls)...

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");
$num = mysql_num_rows($sql);
if ($num > 0){
 echo '<table border="0">';
 $x=0;
 while ($row = mysql_fetch_assoc($sql)){
   $x++;
   $output  = ' <tr>';
   $output .= '  <td colspan="2">Customer ' . $x . '</td>';
   $output .= ' </tr>';
   $output .= ' <tr>';
   echo $output;
   
   foreach ($row as $key => $value){
    $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;
   }  
 
   $output  = ' <tr>';
   $output .= '  <td colspan="2"><br /></td>';
   $output .= ' </tr>';
   echo $output;
 }
 echo '</table>';
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:

Select allOpen in new window

 

by: NerdsOfTechPosted on 2009-08-16 at 12:38:20ID: 25110134

line fixed use this version:

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");
$num = mysql_num_rows($sql);
if ($num > 0){
 echo '<table border="0">';
 $x=0;
 while ($row = mysql_fetch_assoc($sql)){
   $x++;
   $output  = ' <tr>';
   $output .= '  <td colspan="2">Customer ' . $x . '</td>';
   $output .= ' </tr>';
   echo $output;
   
   foreach ($row as $key => $value){
    $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;
   }  
 
   $output  = ' <tr>';
   $output .= '  <td colspan="2"><br /></td>';
   $output .= ' </tr>';
   echo $output;
 }
 echo '</table>';
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen in new window

 

by: drewrockshardPosted on 2009-08-17 at 17:45:26ID: 25119465

Let me try to implement this into my code and I'll let you know. I appriciate the help ;)

 

by: NerdsOfTechPosted on 2009-08-17 at 20:55:08ID: 25120048

No problem let me know if it works so I can help debug if it doesn't

 

by: drewrockshardPosted on 2009-08-18 at 19:00:34ID: 25129129

@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! :)

 

by: NerdsOfTechPosted on 2009-08-19 at 03:28:47ID: 25131109

Sure!

Since I don't know what your id field is called I will just reference this field as id

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");
$num = mysql_num_rows($sql);
if ($num > 0){
 echo '<table border="0">';
 $x=0;
 while ($row = mysql_fetch_assoc($sql)){
   $x++;
   $output  = ' <tr>';
   $output .= '  <td colspan="2">Customer ' . $row['id'] . '</td>'; // change this fieldname accordingly
   $output .= ' </tr>';
   echo $output;
   
   foreach ($row as $key => $value){
    $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;
   }  
 
   $output  = ' <tr>';
   $output .= '  <td colspan="2"><br /></td>';
   $output .= ' </tr>';
   echo $output;
 }
 echo '</table>';
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen in new window

 

by: NerdsOfTechPosted on 2009-08-19 at 03:31:59ID: 25131123

I'll make the header say Customer ID and have a gray bgcolor for readability too :)

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");
$num = mysql_num_rows($sql);
if ($num > 0){
 echo '<table border="0">';
 $x=0;
 while ($row = mysql_fetch_assoc($sql)){
   $x++;
   $output  = ' <tr>';
   $output .= '  <td colspan="2" bgcolor="#cccccc">Customer ID: ' . $row['id'] . '</td>'; // change this fieldname accordingly
   $output .= ' </tr>';
   echo $output;
   
   foreach ($row as $key => $value){
    $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;
   }  
 
   $output  = ' <tr>';
   $output .= '  <td colspan="2"><br /></td>';
   $output .= ' </tr>';
   echo $output;
 }
 echo '</table>';
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen in new window

 

by: drewrockshardPosted on 2009-08-19 at 05:58:49ID: 25132050

@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?

 

by: drewrockshardPosted on 2009-08-19 at 09:36:06ID: 25134607

Ya - I still can't get it to work - any ideas, perhaps?

 

by: NerdsOfTechPosted on 2009-08-19 at 11:05:35ID: 25135523

Yes. BRB

 

by: NerdsOfTechPosted on 2009-08-19 at 11:09:14ID: 25135554

try this:

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");
$num = mysql_num_rows($sql);
if ($num > 0){
 echo '<table border="0">';
 $x='';
 while ($row = mysql_fetch_assoc($sql)){
   if ($x != $row['id']){
    $x = $row['id'];
    $output  = ' <tr>';
    $output .= '  <td colspan="2" bgcolor="#cccccc">Customer ID: ' . $row['id'] . '</td>'; // change this fieldname accordingly
    $output .= ' </tr>';
    echo $output;
   } 
   foreach ($row as $key => $value){
    $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;
   }  
 
   $output  = ' <tr>';
   $output .= '  <td colspan="2"><br /></td>';
   $output .= ' </tr>';
   echo $output;
 }
 echo '</table>';
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:

Select allOpen in new window

 

by: drewrockshardPosted on 2009-08-19 at 12:34:46ID: 25136425

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?

 

by: drewrockshardPosted on 2009-08-19 at 12:37:43ID: 25136464

@NerdsOfTech:

Actually, is that "new" code, the same code from above?  It kinda looks like it from a quick glance.

 

by: NerdsOfTechPosted on 2009-08-19 at 20:21:13ID: 25139167

first, what the field name for the id...is it actually id. thanks

 

by: drewrockshardPosted on 2009-08-20 at 10:37:51ID: 25145131

CustomerID is the actual table name. Thanks!

 

by: NerdsOfTechPosted on 2009-08-20 at 11:07:03ID: 25145466

Thank you.

$sql = mysql_query("SELECT * FROM customerservicedate INNER JOIN customers ON customerservicedate.CustomerID = customers.CustomerID WHERE ServiceDate <= NOW() AND ServiceProvided='Yes' AND ServicePaid='No';");
$num = mysql_num_rows($sql);
if ($num > 0){
 echo '<table border="0">';
 $x='';
 while ($row = mysql_fetch_assoc($sql)){
   if ($x != $row['CustomerID']){ // new customer ID found print title ONCE
    $x = $row['CustomerID'];
    $output  = ' <tr>';
    $output .= '  <td colspan="2" bgcolor="#cccccc">Customer ID: ' . $row['CustomerID'] . '</td>'; // change this fieldname accordingly
    $output .= ' </tr>';
    echo $output;
   } 
   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;
    }
   }  
 
   $output  = ' <tr>';
   $output .= '  <td colspan="2"><br /></td>';
   $output .= ' </tr>';
   echo $output;
 }
 echo '</table>';
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:

Select allOpen in new window

 

by: drewrockshardPosted on 2009-08-20 at 11:13:21ID: 25145523

Nice! Perfect!  I'm going to try to implement this into my code and let you know how it goes.  I'll update either tonight or tomorrow :)

 

by: drewrockshardPosted on 2009-08-21 at 12:46:19ID: 25155169

@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.

 

by: NerdsOfTechPosted on 2009-08-21 at 13:06:30ID: 25155332

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'];

 

by: NerdsOfTechPosted on 2009-08-21 at 13:07:15ID: 25155337

Thanks!

 

by: drewrockshardPosted on 2009-08-21 at 14:27:20ID: 25155989

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.

 

by: NerdsOfTechPosted on 2009-08-21 at 15:00:12ID: 25156250

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,

 

by: NerdsOfTechPosted on 2009-08-21 at 15:01:43ID: 25156258

One more thing... You can add up totals in SQL so let me know what needs to be added up.

Also, you can get group totals

=NOT

 

by: drewrockshardPosted on 2009-08-21 at 15:26:46ID: 25156424

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 :)

 

by: NerdsOfTechPosted on 2009-08-21 at 15:44:07ID: 25156520

Okay... wow that is quite a goal.

Now I think that may constitute as multiple questions here.

I can get you to the output format you desire for this question.

The rest we can attach as related questions

Would that be okay with you?

 

by: drewrockshardPosted on 2009-08-21 at 15:52:01ID: 25156572

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.

 

by: NerdsOfTechPosted on 2009-08-21 at 16:04:28ID: 25156624

First, What are you using to make the pdf file? is it the built in PDFlib ?

 

by: drewrockshardPosted on 2009-08-21 at 16:13:01ID: 25156660

Nope - it's fPDF.

I've already created a report that works great - but has a far less sophisticated SQL statement and doesn't need to be itemized.  So it works perfectly.  But this one i'm doing right now is a little more in depth, as it will be used to create invoices.

 

by: NerdsOfTechPosted on 2009-08-21 at 16:49:38ID: 25156798

Ok cool I'll get back to you tomorrow and I'll go over what you need.

Thanks!

 

by: drewrockshardPosted on 2009-08-21 at 16:56:23ID: 25156817

Sounds great! :) Thanks again.  I should be around and online from 7am CST until 7pm CST - I'll be at work - so I'll be on the computer.

Again- thanks.

 

by: NerdsOfTechPosted on 2009-08-22 at 13:34:22ID: 25160142

Looks like a busy weekend. I'll try to get back to you on Monday.

Best regards,
=NOT

 

by: drewrockshardPosted on 2009-08-22 at 13:53:49ID: 25160217

Will do - thanks for the update.

 

by: drewrockshardPosted on 2009-08-27 at 06:03:04ID: 25197510

NOT:

It's Thursday, and I haven't received any response.  Is this something we can work out?

-Drew

 

by: drewrockshardPosted on 2009-09-06 at 10:46:55ID: 25270802

NOT

Is this project going to be too much to handle and should I just close out this and give you your 500 points?

Please do let me know.

-- Drew

 

by: drewrockshardPosted on 2009-10-14 at 13:36:48ID: 25574887

Angellll,

Not sure how, as I was told that this would be followed up on - and it never was.

See above remarks.

 

by: NerdsOfTechPosted on 2009-10-15 at 12:57:02ID: 25584164

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

 

by: NerdsOfTechPosted on 2009-10-27 at 16:35:28ID: 25678821

Agreed.

 

by: ee_autoPosted on 2009-11-01 at 01:23:40ID: 25713001

Question PAQ'd, 500 points refunded, and stored in the solution database.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...