syntax error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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)
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Use a foreach() loop

Otherwise use count($list_friends) in place of 7.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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;
}
Post Reply