...

View Full Version : Using Return



Crisp
01-06-2010, 08:48 PM
Hey,
Using return in a function will only execute it once, correct?
After it's executed, it will end and won't do it again (Say if you have a while loop)

What I want, is it to return and return the code ever how many times it needs to loop (For loop)
Which is impossible using return.

What I'd like to know, is if there is something very very similar to return that will do that, or another way.
Here's what I have:


class Features
{
function stars($id) //Usergroup stars
{
global $queries;
$usergroups = $queries->groupQuery("`id`='$id'");
$usergroup = mysql_fetch_array($usergroups);
//Value
for ($stars = 1; $stars <= $usergroup['stars']; $stars++)
{
$star = "<img src='images/skins/default/stars/admin_star.png' alt='Star' />";
return $star;
}
}
}


And now to call it,


class Thread
{
function threads()
{
$features->stars($userg['id']);
}
}


Thanks. :)

mlseim
01-06-2010, 09:00 PM
Do you only have "full stars", or do you have some "half stars"?

I'm guessing you are displaying stars like a "rating" system?

You'll need to build the <img> tags into one string ... like this:


class Features
{
function stars($id) //Usergroup stars
{
global $queries;
$usergroups = $queries->groupQuery("`id`='$id'");
$usergroup = mysql_fetch_array($usergroups);
//Value
for ($stars = 1; $stars <= $usergroup['stars']; $stars++)
{
$star .= "<img src='images/skins/default/stars/admin_star.png' alt='Star' />";
}
return $star;
}
}

Crisp
01-06-2010, 10:50 PM
Works great.
Only thing is it says:

Notice: Undefined variable: star in X

Which I'd like to be taken away.
I see the problem, but I don't know how to fix it.

Do you know what I can do to fix it where it still works?
Thanks. (Nice sig btw! :p)

_Aerospace_Eng_
01-06-2010, 10:52 PM
You need to set $star to null or an empty string just before you start your for loop. If you don't then the scope of $star is only local to the for loop and nothing else.

Crisp
01-06-2010, 11:24 PM
Oh, ok, fixed.
Thanks a lot guys. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum