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

syntax error

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

Re: syntax error

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

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

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

Post 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?
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

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

or even better use single quotes

Code: Select all

$output .= 'html html html html'.list_f();
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

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

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

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

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

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

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

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

Post by John Cartwright »

Check your variables
Post Reply