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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parts and Products table schema in mysql 6 43
PHP7 and Sql Server Windows 2008 R2 13 81
mcrypt_create_iv() is deprecated 4 67
PHP warning 4 27
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
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.

929 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

11 Experts available now in Live!

Get 1:1 Help Now