I'm creating a multi-level twitter app. People refer others and get points etc....

What I'm trying to do is create the detailed statistics page of the members area and I'm running into a hitch writing the function I want to use.

Basically what I want to happen is this:
I have the following fields in the 'users' table of my db: spid1, spid2, spid3, spid4, etc...(upto spid 10.), these are the sponsor's twitter ids. Twid is the twitter id field.

I want to create a function that will echo a table row with the user's twitter image, description, and url. I have a table for that too. When they register that information is pulled from twitter and cached in a table called: twitter_cache.

My problem is organzing the function I want it to work like this:
downline (spid1);
While spid1 is the twitter user echo the following table rows:

<tr><td><img src="$pimage"></td><td>$pdesc</td><td><a href="$purl">$purl</a></td></tr>
Here's what I have so far...

function downline($spid) {
$query = mysql_query("SELECT twid FROM users WHERE $spid='$session_id'");
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$spid_p = $row['$spid'];
// This is where I'm having problems - - should I use foreach, or while to load I've just started with php, but have come a long way in the past month or so... would something like this work? : 

while($spid_p > 0){
$query2 = mysql_query("SELECT pimage, pdesc, purl FROM twitter_cache WHERE twitter_user='$spiid_p'");

$row2 = mysql_fetch_array($query2, MYSQL_ASSOC);
$pimage = $row['pimage'];
$pdesc = $row['pdesc'];
$purl = $row['purl'];

echo "<tr><td><img src="$pimage"></td><td>$pdesc</td><td><a href="$purl">$purl</a></td></tr>"
echo "<table width="80%">";
downline (spid1);
echo "</table>";
Would this work? or should I use the foreach loop statement instead? If so could you give an example of the best way to do it? Thanks