Need help creating a function to loop through a mysql fetch array..

Discussion in 'PHP' started by theresabloginmysoupdotcom, Jun 3, 2009.

  1. #1
    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>
    Code (markup):
    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>";
    Code (markup):
    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
     
  2. jsamdirect

    jsamdirect Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The while should work just find. You may want to add a LIMIT to your sql query and a count limit to your while statement just to assure nothing gets stuck in any kind of loop.
     
    jsamdirect, Jun 3, 2009 IP