Need to repeat MySQL query results X number of times - Returns "Line Item" Need to echo per Quantity Count

Posted on 2008-10-12
Last Modified: 2013-12-13
Query pulls records from 5 db tables and returns Account Receivable by line item (e.g. First Name || Last Name || Company || Product Part Number || Line Item QUANTITY

Bill Smith - AJAX Inc.
p/n: 123-X555 10 EA
I am working on a routine to print labels and need to output something similar to:

Bill Smith - AJAX, Inc.
p/n: 123-X555 1 of 10
Bill Smith - AJAX, Inc.
p/n: 123-X555 2 of 10
Bill Smith - AJAX, Inc.
p/n: 123-X555 3 of 10
And So On...can't get this to output right...I must be missing something.

Some but not all queries will have more than one line item. So, My SELECT/WHILE statements are pulling correctly, I've just not nailed how to get an echo per "Quantity" count


PS - I've not added a snippet because (for one its actually rather confusing) but mainly because I know its got to be a modification to the while loop and I'm hoping someone can give me this in principle or at least cite a ref and I'll look it up.
Question by:Rapturer
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
  • 2
  • 2
LVL 39

Accepted Solution

Roger Baklund earned 500 total points
ID: 22699113
Try something like this:
foreach($dataset as $row) {
  for($counter=1;$counter<=$row['Quantity'];$counter++) {
    echo $row['First'].' '.$row['Last'].' - '.$row['Company']."\n" 
         'p/n:'.$row['PartNbr'].' '.$counter.' of '.$row['Quantity']."\n";

Open in new window


Author Comment

ID: 22699130
Thank you CXR - I'll fire it up now and get back!

LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
ID: 22709730
I answered the question:


You got the same answer in question 23810852, this from  angellll:

$quantity = $therecord["quantity"];
for ($loop_quantity=1, $loop_quantity <= $quantity, $loop_quantity++)

... and this from nahime, which you accepted:

for ($i = 1; $i <= $therecord["quantity"]; $i++)

I gave the correct answer first.

Author Closing Comment

ID: 31505496
I dont wish to argue...however Nahime's answer was the most complete...he provided the entire function. My first question did not include the code snippet hence the fault is largely mine. Awarding points.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pass foreach data in php into jquery ajax 10 51
Integrating blogs between to separate WordPress websites 4 33
PHP SMTP authentication 6 28
deprecated mysql extensions 1 30
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

726 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