Jump to content

[SOLVED] Need help ASAP w/ function using a while loop through two mysql arrays...


patrickcurl

Recommended Posts

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) {
global $sesssion_id;
$query = mysql_query("SELECT twid FROM users WHERE $spid='$session_id'");
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$spid_p = $row['twid'];
// 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='$spid_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>";

Try this:

 

function downline($spid) {
global $sesssion_id;

$q1sql = "SELECT twid FROM users WHERE $spid='${session_id}'";
$q1res = mysql_query($q1sql);
$q1 = mysql_fetch_array($q1res);

$spid_p = $q1['twid'];

$q2sql = "SELECT pimage, pdesc, purl FROM twitter_cache WHERE twitter_user = '${spid_p}';";
$q2res = mysql_query($q2sql);

while($q2 = mysql_fetch_array($q2res)){
	$fetch = mysql_fetch_array($q2, MYSQL_ASSOC);
	$pimage = $fetch['pimage'];
	$pdesc = $fetch['pdesc'];
	$purl = $fetch['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>';
}

Thanks - I tried your suggestion but am getting the following error:

Notice: Use of undefined constant spid3 - assumed 'spid3' in /home/patrickc/public_html/twtfollow.com/members.php on line 163

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/patrickc/public_html/twtfollow.com/members.php on line 149

 

Line 163: downline(spid3);

Line 149: $fetch = mysql_fetch_array($q2, MYSQL_ASSOC);

 

Try this:

 

function downline($spid) {
global $sesssion_id;

$q1sql = "SELECT twid FROM users WHERE $spid='${session_id}'";
$q1res = mysql_query($q1sql);
$q1 = mysql_fetch_array($q1res);

$spid_p = $q1['twid'];

$q2sql = "SELECT pimage, pdesc, purl FROM twitter_cache WHERE twitter_user = '${spid_p}';";
$q2res = mysql_query($q2sql);

while($q2 = mysql_fetch_array($q2res)){
	$fetch = mysql_fetch_array($q2, MYSQL_ASSOC);
	$pimage = $fetch['pimage'];
	$pdesc = $fetch['pdesc'];
	$purl = $fetch['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>';
}

 

<?php
function downline($spid) {
global $sesssion_id;

$q1sql = "SELECT twid FROM users WHERE $spid='{$session_id}'";
$q1res = mysql_query($q1sql);
$q1 = mysql_fetch_array($q1res);

$spid_p = $q1['twid'];

$q2sql = "SELECT pimage, pdesc, purl FROM twitter_cache WHERE twitter_user = '{$spid_p}';";
$q2res = mysql_query($q2sql);

while($q2 = mysql_fetch_array($q2res)){
	$fetch = mysql_fetch_array($q2, MYSQL_ASSOC);
	$pimage = $fetch['pimage'];
	$pdesc = $fetch['pdesc'];
	$purl = $fetch['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>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.