Page 2 of 2

Posted: Sat Apr 09, 2005 7:49 pm
by pinehead18
Ok i see the prob.. I can't return the array and the contents if its put into a var.. it will not return or echo the var..

Any other ideas?

Posted: Sat Apr 09, 2005 7:54 pm
by John Cartwright

Code: Select all

function list_f() {
        $sql = "SELECT f FROM users WHERE user='$name'";
        $result = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($result);
        
        $friends = $row['f'];
 
        $list_friends = explode(',', $friends );
        
        $string = "";
        
        for ($i=0; $i<7; $i++) 
        {
             $string .= $list_friends[$i].'<br />';
        }
        return $string;
}
On line 8 you were using $f instead of $friends .........
By the way, where does $name come from. To use outside variables within a function you must pass it with the function name. For example

Code: Select all

function list_f($name)

Posted: Sat Apr 09, 2005 9:44 pm
by pinehead18
yeah i don't think this is ever going to work.. I've tried everything you guys have asked even did some trouble shooting and it will not output anything when i call the function I'll show you what i have now .

Code: Select all

function list_friends($name) {
        $sql = "SELECT friends FROM users WHERE user='$name'";
        $result = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($result);
        
                $friends = $row['friends'];

        $list_friends = explode(',', $friends);
        
        for ($i=0; $i<7; $i++) {
          $friends = $list_friends[$i];
        }
        return $friends;
        
}
$output .="html html";
$output .= list_friends($name);
$output .= "rest of html";

this will not return any information where i call it.. In fact when i call it, it does nothing. What it is suppsoe to do is list all the contents from the field/array in the db.

thanks
anthony

Posted: Sun Apr 10, 2005 1:07 am
by John Cartwright
look at the code in my last page, and compare it to yours.
Secondly, is that column supposed to be 'f' or 'friends'.. I am assuming friends.

Posted: Sun Apr 10, 2005 11:15 am
by pinehead18
It works great thank you for your help.. Learning alot here today. I do have one final question, something came up that i did not expect. Do you see the loop? It runs 7 times no matter what it is really cuasing a problem mainly becuase it takes up alot of extra space. waht i want to accomplish is i just want it to show the vars in the array up too 7 times and after 7 times don't display anymore. Do i just take out the for loop and use a while loop.. Or even just pull it from the db and use a limit? How might i go about that?

thank you
Anthony

Posted: Sun Apr 10, 2005 2:05 pm
by Chris Corbyn
Use a foreach() loop

Otherwise use count($list_friends) in place of 7.

Posted: Sun Apr 10, 2005 3:11 pm
by John Cartwright

Code: Select all

function list_f() {
        $sql = "SELECT f FROM users WHERE user='$name'";
        $result = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($result);
        
        $friends = $row['f'];
 
        $list_friends = explode(',', $friends );
        
        $string = "";
        
        for ($i=0; $i >= count($list_friends); $i++) 
        {
             $string .= $list_friends[$i].'<br />';
        }
        return $string;
}