If you guys can help me out with this I would really appreciate it.

Here's how it works. The administrator enters the information about various mailing lists into a database.
id
listname
listemail
listowner
category (A, B, C, D, E)
subscribers
adstorun (how many times per month: 5, 10, etc)
listrating (G, PG, R, X)
maxrating (G, PG, R, X)(this dictates what kind of ad the list is willing to run)
commercial (Y, N) (will run commercial ads)
impressions (No. of impressions this list should receive)
adcopy
running (Y, N) (running this month, yes or no)

I'm trying to create a script that will generate an email containing the necessary ads needed to run. For example, I would receive an email with 10 different ads I need to run this month.

I've started with this:
$sql = "SELECT * FROM $table_name WHERE category='A' AND running='Y'";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);

Basically it says gather a list of everyone from category A who is running this month. Now I want it to start with the first list, and fill its email with ads from the other remaining lists. How can I do this?

It would be some form of loop, starting with the first element of the array. Then it would go to the second element, print its ad copy. Go to the third element, and so on. It would only continue this process for a set number of times, specified by adstorun, then drop out of the first elements cycle and start the second elements cycle. However, the second element would need to print the first elements ads.

It sounds like several nested FOR and WHILE loops. Do you have any idea what I'm talking about or how I could go about doing this?