Jump to content

How do I pass a variable out of a function into main script?


patrickcurl

Recommended Posts

Here's what I have :

function downlow($spidz, $level){
// make $session_id and $up_num variables global.
global $session_id;
global $up_num;

/* Create query where spidz is the level that person is on. So spid1 will pull all 
id's that have $session_id's username on level 1 or spid1. */
$result = mysql_query("SELECT twid FROM users WHERE $spidz = '${session_id}'")
or die (mysql_error());
// Set the number of results for the downline.
$up_num = mysql_num_rows($result);
.......more code...yada yada...

 

The rest of the function basically pulls up the referral information it's a multilevel twitter referral script.

 

What I'm trying to do is use $up_num which is basically how many referrals a person has on their x level.

 

The function works like so:

downlow('spid1','Level 1');
$up_num1 = $up_num;
downlow('spid2','Level 2');
$up_num2 = $up_num;

 

in the users db each user has a field showing their upline - up ten levels. This searches for the twitter users who's sponsor_id level is 1 and displays them in a table.

 

Well everything works fine...

I also want to take $up_num1 and add it to $up_num2 and so on to get $up_num total and echo the total number of downline members on all levels.

 

Well the way I have it coded now works....BUT Zend Studio is throwing up a warning, and I want to learn proper coding and how not to code ugly code so I'm wondering what's the best way to do this?

 

The error I'm getting from Zend Studio is:

var-use-before-def-global : Global variable $up_num was used before it was defined

 

 

Link to comment
Share on other sites

It's just complaining that you haven't assigned anything to $up_num before using it in the function.  Before your very first call to the function put:

$up_num = 0;

 

We call that variable initialization.  It goes back to early languages where you had to define variables before you used them.  Like in the programming language C:

int main( void ) {
  int up_num;

  up_num = up_num + 1;
  return 0;
}

 

See?  I had to write int up_num; and declare up_num to hold integer values before I could use it!

 

In older languages like that, variables weren't automatically set to default values.  For example, in PHP if you use a variable without first setting a value in it, it will be initialized to an empty string or the number zero or a null.  But the variable has to have something assigned before you can use it; PHP does this automatically but it issues warnings if you rely on it because it's bad programming behavior to use unititialized variables.

 

Going back to my short C example, C does not (or did not) initialize variables.  When I declared up_num, it was placed into memory and the value in the variable is whatever is in that memory.  In my C code, up_num could be 0, 42,302, -42, or any integer value!

 

Therefore I should have done:

int main( void ) {
  int up_num = 0; /* ALWAYS initialize variables before using them! */

  up_num = up_num + 1;
  return 0;
}

 

And if you really want to learn good programming practice, learn how to write programs without global variables.  You don't need them!

Link to comment
Share on other sites

Simple answer. Dont use global variables! There is a time and place for them but not in your case. You should return a value from your function i.e.

 

<?php

function setVal($x) {
	$x = $x+1;
	return $x;
}

$value = 10;
$newVal = setVal($value);
// will print 11
print $newVal."<br />";

$newVal = setVal($newVal);
// will print 12
print $newVal;
?>

Link to comment
Share on other sites

Can you give me an example how I could rewrite this code - w/out using Globals?

I'm learning still and could definitely use some tutoring on good coding.

Thanks!

 

// insert the header
include ("members_header.php");

// Begin unique page content
print "<p><center><span style=\"font-size: 24px; color: #3188B1;\"> 
Welcome @".$_SESSION['twid']. " to #twtFollow Members Area!</span></center></p>";

// Register username as variable.
$session_id = $_SESSION['twid'];

// Create Function to show downline stats.

function downlow($spidz, $level){
// make $session_id and $up_num variables global.
global $session_id;
global $up_num;

/* Create query where spidz is the level that person is on. So spid1 will pull all 
id's that have $session_id's username on level 1 or spid1. */
$result = mysql_query("SELECT twid FROM users WHERE $spidz = '${session_id}'")
or die (mysql_error());

// Set the number of results for the downline.
$up_num = mysql_num_rows($result);

// Start table to echo what level, and how many recruits on the level.

echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">$level: $up_num Twitter Followers via #TwtFollow! </div></td></tr>";

// Create While Loop / w/ fetch array. 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

// Pull the username of the downline members
$star = $row['twid'];

// create a query to get their cached twitter profile fields from mysql.
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);

// Grab img from array and set it's variable.
$star_image = $star_image_row['pimage'];

// grab description from array and set it in a variable.
$star_desc = $star_image_row['pdesc'];

// grab url from array and set it in a variable.
$star_url = $star_image_row['purl'];

// echo out the table rows show the user's picture, description, and url.
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";
}
echo "</table>";
}


downlow('spid1','Level 1');
$up_num1 = $up_num;
downlow('spid2','Level 2');
$up_num2 = $up_num;
downlow('spid3','Level 3');
$up_num3 = $up_num;
downlow('spid4','Level 4');
$up_num4 = $up_num;
downlow('spid5','Level 5');
$up_num5 = $up_num;
downlow('spid6','Level 6');
$up_num6 = $up_num;
downlow('spid7','Level 7');
$up_num7 = $up_num;
downlow('spid8','Level 8');
$up_num8 = $up_num;
downlow('spid9','Level 9');
$up_num9 = $up_num;
downlow('spid10','Level 10');
$up_num10 = $up_num;

// Add up the $up_num's and get the total.
$up_num_total = $up_num1 + $up_num2 + $up_num3 + $up_num4 + $up_num5 + $up_num6 + $up_num7 + $up_num8 + $up_num9 + $up_num10;
echo "<tr><td colspan='3' style='width: 100%;' > </td></tr><tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">
You have $up_num_total Total Followers via TwtFollow!</div></td></tr><tr><td colspan='3' style='width: 100%;' > </td></tr></table>";
// }
include ("members_footer.php");
}

else{

//the session variable isn't registered, send them back to the login page
include ("members_header.php");
include ("login.php");
include ("members_footer.php");
}
}

Link to comment
Share on other sites

$stat_num = 0;
function downlow($spidz, $level, $stat_num){
global $session_id;
$result = mysql_query("SELECT twid FROM users WHERE $spidz = '${session_id}'")
or die (mysql_error());
$up_num = mysql_num_rows($result);
$stat_num = $up_num;
}
downlow('spid1','Level 1',$stat_num);
$stat1 = $stat_num;
downlow('spid2','Level 2',$stat_num);
$stat2 = $stat_num;

 

Is this what you mean by doing it w/out globals? I'm very confuzzled.

Link to comment
Share on other sites

There is?

Well, honestly no there isn't really as there are better alternatives. If global variables in the function are used at all then they should be limited to 1 or 2 only and not a mass of variables. 

 

Things like database connection handles or file handles are sometimes seen as globals in functions from open source scripts that I have experience with.

Link to comment
Share on other sites

You need to return a value from the function and remove the global variable. Pass the value into the function instead $session_id

<?php
function downlow($spidz, $level, $stat_num, $session_id){
$result = mysql_query("SELECT twid FROM users WHERE $spidz = '${session_id}'")
or die (mysql_error());
$up_num = mysql_num_rows($result);
return $up_num;
}
$stat1 = downlow('spid1','Level 1',$stat_num,$session_id);
?>

Link to comment
Share on other sites

Well I'm actually trying to get the function to more than just echo the stats I want it to do the following:

1. Pull numerical Stats.

2. Pull user data from users table.

3. Pull cached image, description, and url from twitter_cache table.

4. Create an html table like so:

  $level ~ image ~ description ~ url

 

Then I want to take ALL the numerical stats from step 1 and add them up to create total_stats. If I can do this all w/ one function I believe it uses a lot less db resources than another method I tried that didn't use a function at all (I'm new to functions as if you couldn't telll...)

 

i've fixed the syntax a little -- does this look better coded?

 

$session_id = $_SESSION['twid'];
$up_num = 0;

function downlow($spidz, $level, $up_num, $session_id){

$result = mysql_query("SELECT twid FROM users WHERE $spidz = '${session_id}'")
or die (mysql_error());

$up_num = mysql_num_rows($result);

return $up_num;

echo "<table class=\"table_stats\">
<tr>
<td colspan='3' style='width: 100%;' > </td>
</tr>
<tr>
<td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">$level: $up_num Twitter Followers via #TwtFollow! </div></td></tr>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$star = $row['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");

$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);

$star_image = $star_image_row['pimage'];

$star_desc = $star_image_row['pdesc'];

$star_url = $star_image_row['purl'];

echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";
}

echo "</table>";
}
downlow('spid1','Level 1', $session_id, $up_num);
$up_num1 = $up_num;

downlow('spid2','Level 2', $session_id, $up_num);
$up_num2 = $up_num;

$total_num = $up_num1 + $up_num2;
echo "you have $total_num total downline members!";

Link to comment
Share on other sites

I am unsure why you are even putting this into a function. Do you use this code in more than 1 section of your application. IMO functions are not used to print output. They are used to return data by using given input (parameters). The global scope of the application should output the data returned from a function.

 

I think you need to read up on functions. Once the return syntax is used in a function it ends and the data is returned to the global scope!

Link to comment
Share on other sites

so you're saying instead of using the function I should do something more verbose like this:

 

$session_id = $_SESSION['twid'];
$r = mysql_query("SELECT twid FROM users WHERE spid1 = '$session_id'")
or die (mysql_error());
$up_num1 = mysql_num_rows($r);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 1: $up_num1 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row = mysql_fetch_array($r, MYSQL_ASSOC))
{
$star = $row['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r2 = mysql_query("SELECT twid FROM users WHERE spid2 = '$session_id'")
or die (mysql_error());
$up_num2 = mysql_num_rows($r2);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 2: $up_num2 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row2 = mysql_fetch_array($r2, MYSQL_ASSOC))
{
$star = $row2['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r3 = mysql_query("SELECT twid FROM users WHERE spid3 = '$session_id'")
or die (mysql_error());
$up_num3 = mysql_num_rows($r3);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 3: $up_num3 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row3 = mysql_fetch_array($r3, MYSQL_ASSOC))
{
$star = $row3['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r4 = mysql_query("SELECT twid FROM users WHERE spid4 = '$session_id'")
or die (mysql_error());
$up_num4 = mysql_num_rows($r4);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 4: $up_num4 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row4 = mysql_fetch_array($r4, MYSQL_ASSOC))
{
$star = $row4['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r5 = mysql_query("SELECT twid FROM users WHERE spid5 = '$session_id'")
or die (mysql_error());
$up_num5 = mysql_num_rows($r5);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 5: $up_num5 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row5 = mysql_fetch_array($r5, MYSQL_ASSOC))
{
$star = $row5['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r6 = mysql_query("SELECT twid FROM users WHERE spid6 = '$session_id'")
or die (mysql_error());
$up_num6 = mysql_num_rows($r6);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 6: $up_num6 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row6 = mysql_fetch_array($r6, MYSQL_ASSOC))
{
$star = $row6['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r7 = mysql_query("SELECT twid FROM users WHERE spid7 = '$session_id'")
or die (mysql_error());
$up_num7 = mysql_num_rows($r7);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 7: $up_num7 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row7 = mysql_fetch_array($r7, MYSQL_ASSOC))
{
$star = $row7['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r8 = mysql_query("SELECT twid FROM users WHERE spid8 = '$session_id'")
or die (mysql_error());
$up_num8 = mysql_num_rows($r8);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 8: $up_num8 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row8 = mysql_fetch_array($r8, MYSQL_ASSOC))
{
$star = $row8['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r9 = mysql_query("SELECT twid FROM users WHERE spid9 = '$session_id'")
or die (mysql_error());
$up_num9 = mysql_num_rows($r9);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 9: $up_num9 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row9 = mysql_fetch_array($r9, MYSQL_ASSOC))
{
$star = $row9['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$r10 = mysql_query("SELECT twid FROM users WHERE spid10 = '$session_id'")
or die (mysql_error());
$up_num10 = mysql_num_rows($r10);

echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 10: $up_num10 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row10 = mysql_fetch_array($r10, MYSQL_ASSOC))
{
$star = $row10['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

$up_num_total = $up_num1 + $up_num2 + $up_num3 + $up_num4 + $up_num5 + $up_num6 + $up_num7 + $up_num8 + $up_num9 + $up_num10;
echo "<tr><td colspan='3' style='width: 100%;' > </td></tr><tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">
You have $up_num_total Total Followers via TwtFollow!</div></td></tr><tr><td colspan='3' style='width: 100%;' > </td></tr></table>";

 

as you can see with this version I'm basically using this same code 10 different times just changing a few variables:

$r9 = mysql_query("SELECT twid FROM users WHERE spid9 = '$session_id'")
or die (mysql_error());
$up_num9 = mysql_num_rows($r9);
echo "<table class=\"table_stats\"><tr><td colspan='3' style='width: 100%;' > </td></tr>
<tr><td colspan='3' align='center' style='border-top: 1px solid grey;border-bottom: 1px solid grey; width: 100%;' >
<div style=\"font-size: 24px; background-color: #EF1D12; color: #E6E552;\">Level 9: $up_num9 Twitter Followers via #TwtFollow! </div></td></tr>";

while ($row9 = mysql_fetch_array($r9, MYSQL_ASSOC))
{
$star = $row9['twid'];
$star_image_result = mysql_query("select pimage, pdesc, purl from twitter_cache where twitter_user = '$star'");
$star_image_row = mysql_fetch_array($star_image_result, MYSQL_ASSOC);
$star_image = $star_image_row['pimage'];
$star_desc = $star_image_row['pdesc'];
$star_url = $star_image_row['purl'];
echo "<tr><td class=\"stats_td_image\"><a href='http://www.twitter.com/$star'>
<img src='$star_image' class=\"img_stats\"></a></td>
<td class=\"stats_td_desc\">$star_desc</td><td class=\"stats_td_url\">$star_url</td></tr>";

}
echo "</table>";

I've attached an image showing the end result of the page I'm creating...

 

My main concern is what is the best method to do this - because I am learning and I don't just want something that works - I want something that works well, uses the least amount of resources, and has the best syntax structure that I can come up with.

 

I'm really interested in knowing if anyone else knows of a better method to get this same info printed.

 

Appreciate your help in advanced...

 

[attachment deleted by admin]

Link to comment
Share on other sites

From looking at your code I can see bad database design which is why you are having to use so many queries. You are looking up a session_id record against 10 fields. For this you should have used a many to many relationship i.e.

 

Instead of

 

users

*********

user_id

name

spid1

spid2

spid3

 

etc.

 

Your db design should take the form of

 

users

*********

user_id

name

 

spid

*********

spid_id

name

 

users_to_spid

*************

uts_id

user_id

spid_id

 

i.e

If user_id 1 is related to spid_id 1,2,3 then your recordset would look like

 

1 1 1

2 1 2

3 1 3

 

Then you can achieve your report using 1 simple query. What spids is my user joined to?

 

SELECT s.name AS spidName FROM spid s, users_to_spid uts WHERE s.spid_id=uts.spid_id AND uts.userId='1'

Link to comment
Share on other sites

Things like database connection handles or file handles are sometimes seen as globals in functions from open source scripts that I have experience with.

 

They're poorly coded.  A registry (or singleton factory) accomplishes the same thing without the problems.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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