Jump to content

While loops from function returns


EchoFool

Recommended Posts

Hey,

 

I have a function which returns a query result like this :

 

<?php return(mysql_fetch_assoc($Query); ?> 

 

Then my script has:

 

<?php
While($row = functioname()) { 
// do stuff
}
?>

 

But for some reason this method does not work... does any one know how i can do the idea im trying to do cos currently it just loops for infinity..

Link to comment
Share on other sites

Ok tried this:

 

But i get unlimited / endless while looping :(

<?php
function listfriends($UserID){
$SELECT = mysql_query("SELECT ContactID FROM social WHERE UserID='$UserID' AND Type='Friend'")
   Or die(mysql_error());
$row = mysql_fetch_assoc($SELECT);
return ($row);
}


while($row = listfriends($_SESSION['Current_User'])){
getusername($row['ContactID']).'<a href="#">[Mail]</a> <a href="#">[Remove]</a><br>';
}
?>

Link to comment
Share on other sites

That code isn't going to work because each time you call it, it executes completely from the beginning again, hence you'll get a never ending loop. You would need to use a static variable, though I'm not going to really recommend this approach either. The code is terrible.

 

If your listfreinds() function was part of a class / object this would be much simpler, as then you can implement the state you need.

Link to comment
Share on other sites

Just have it return an array and loop through that.

 

I'm interested in seeing this getusername() function as well as it would appear to me that this is going to make yet more queries to the database. This code is terribly inefficient if that is indeed the case.

Link to comment
Share on other sites

Well "getusername" checks if userid exists in usertable and checks if they are staff (as staff are different coloured username) and also gets the username. unless i could some how check across all 3 tables in one query but i always thought calling a function was the efficient method as i can call it all over the site without duplicated lines of code =/

Link to comment
Share on other sites

You'd probably be better off returning the mysql_query() output.  If you return the mysql_fetch_ functions... you'll only get the first row.. a single-dimensional array...  i.e: there's no reason to loop through it.

function listfriends($UserID){
return mysql_query("SELECT ContactID FROM social WHERE UserID='$UserID' AND Type='Friend'")
   Or die(mysql_error());
}
$row = mysql_fetch_assoc(listfriends($_SESSION['Current_User'])));
echo getusername($row['ContactID']).'[Mail] [Remove]
';
?>

 

Link to comment
Share on other sites

Thanks Zanus - so would an inner join be more efficient then =/

 

Because if so i have to re-assess my use of functions :(

 

Of course an inner join is going to be more efficient. Functions simply wrap pieces of code, if those pieces of code are making unnecessary queries they are inefficient.

Link to comment
Share on other sites

You'd probably be better off returning the mysql_query() output.

actually I take that back.. if you return the mysql_query function it'll just do the same thing.  Since your starting a new resource each time you call the function.  I guess you could get away with passing it as a reference or something though....  (anything but global)

 

function listfriends($UserID){
$SELECT = mysql_query("SELECT ContactID FROM social WHERE UserID='$UserID' AND Type='Friend'")
   Or die(mysql_error());
while($row[] = mysql_fetch_assoc ($_SESSION['Current_User']));
return ($row);
}

$allFriends = listfriends($_SESSION['Current_User']);

foreach($allfriends as $id=>$firend) {
getusername($allfriends['ContactID']).'[Mail] [Remove]
';
}
?>

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.