Page 1 of 2

syntax error

Posted: Sat Apr 09, 2005 2:12 pm
by pinehead18

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_f = explode(',', $f);
        
        for ($i=0; $i<7; $i++) {
         $list_f[$i];                 
        }
}

that is my function. My output is prodcuted using a function as well. I would like to somehow get it to display $list_f[$i] by callig it like this. Please help....

Code: Select all

$output .= "".list_f()."";
thank you
Anthony

Re: syntax error

Posted: Sat Apr 09, 2005 2:49 pm
by Chris Corbyn
Ok sorry - editted the post, I had misunderstood you.

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_f = explode(',', $f);
        $string = ''; //Empty
        for ($i=0; $i<7; $i++) {
         $string .= $list_f[$i].'<br />'; //Add to it                 
        }
        return $string; //Make it the output of the function
}

Posted: Sat Apr 09, 2005 6:04 pm
by pinehead18
That does help, but there is another part to the problem. I need to be able to call the fucntion by doing this $output .="".fucntion().""; will that actually work
?

Posted: Sat Apr 09, 2005 6:07 pm
by Chris Corbyn
pinehead18 wrote:That does help, but there is another part to the problem. I need to be able to call the fucntion by doing this $output .="".fucntion().""; will that actually work
?
Why are the "" quotes there either side? They serve no purpose :?

But yes - if $output is an existing string it will append to it.

Code: Select all

$output .= list_f(); //Lose the quotes

Posted: Sat Apr 09, 2005 6:29 pm
by pinehead18
Well problem is it looks more like this..


$output .= "html html html html".list_f().""; i have to have that html part first.. how might i go about htat?

Posted: Sat Apr 09, 2005 6:32 pm
by John Cartwright

Code: Select all

$output .= "html html html html".list_f();

or even better use single quotes

Code: Select all

$output .= 'html html html html'.list_f();

Posted: Sat Apr 09, 2005 6:33 pm
by Chris Corbyn
The exact way you've just done it....

Are you getting an error or something?

BTW I'm assuming that $output is already a string before you do

.=

since this tries to append a string onto an existing string.

Posted: Sat Apr 09, 2005 6:37 pm
by pinehead18
well ic an't just do $output .= "html html html"; $output .= list_f(); $output .=" rest of the html for this pagE"

Can i?

Posted: Sat Apr 09, 2005 6:41 pm
by Chris Corbyn
pinehead18 wrote:well ic an't just do $output .= "html html html"; $output .= list_f(); $output .=" rest of the html for this pagE"

Can i?
You sure can... so long as $output is a string to start with. Just use = for the first part and .= for the rest otherwise.

Code: Select all

$output = ""; //Empty string
$output .= "html html html";
$output .= list_f();
$output .= "rest of HTML";
echo $output;
If you need line breaks use \r\n (or just \n on a *nix) or look into heredoc syntax

Posted: Sat Apr 09, 2005 6:51 pm
by pinehead18
Thank you so much for all of your help, i have one last question for you.

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(',', $f);
        
        for ($i=0; $i<7; $i++) {
         return $list_f[$i];
        }
}
Ok this is my function...

This is my code

Code: Select all

$output .= "html";
$output .= list_f();
$output .= "finish html";

However list_f does not display the results.

Thank you
Anthony

Posted: Sat Apr 09, 2005 6:54 pm
by John Cartwright
You cannot use a return in a loop, because it will end the instance of the function.
Simply return an array and do what you will with the array.

Posted: Sat Apr 09, 2005 7:01 pm
by pinehead18
Ok, i'm a retarted newb.. what do you meab by return in an array and then do what i want in the array?

The whole goal of this is to take the contents in the field which are seperated by , and display them like this
1
2
3
4


and they are 1,2,3,4 in the field.

Thank you so much for your patients and help.A
Anthony

Posted: Sat Apr 09, 2005 7:05 pm
by Chris Corbyn
Change this

Code: Select all

for ($i=0; $i<7; $i++) {
         return $list_f[$i];
        }
to this

Code: Select all

$string = "";
for ($i=0; $i<7; $i++) {
         $string .= $list_friends[$i].'<br />';
        }
        return $string;
Put the return outside of the loop.

Fixed the fact you switched from $list_friends to $list_f in the loop :)

Jcart| Fixed parse error

Posted: Sat Apr 09, 2005 7:45 pm
by pinehead18
am i even calling the fucntion right? becuase it will not print anything, even out of $output.. however if i take the array out of the function and echo it.. it echo's the correct info.

Shouldn't it work if i just call list_f(); ?

Posted: Sat Apr 09, 2005 7:47 pm
by John Cartwright
Check your variables