Jump to content

Syntax errors


Xtremer360

Recommended Posts

Any ideas on how to fix this line with syntax errors present?

 

<?php $image_properties=array( 'src'=> if ( ! empty ($this->session->userdata('avatar') ) ) { 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar').'; } else { 'assets/peach/img/sprites/userinfo/avatars/avatar.png'; } , 'height' => '80px', 'width' => '80px', 'alt' => 'Avatar' ); ?>

Link to comment
Share on other sites

Yeah, it's called debugging and you don't do it by sticking the whole thing on one line.

 

Split everything up as follows:

 

<?php 

$image_properties=array(
     'src'=> 
          if(!empty ($this->session->userdata('avatar') ) ) {
                'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar').';
           } else {
                'assets/peach/img/sprites/userinfo/avatars/avatar.png';
           }, 
     'height' => '80px',
     'width' => '80px',
     'alt' => 'Avatar' 
); 

?>

 

Having spaced it out I can see you have a wack-off if statement in the middle of your bloody array. Not allowed, you've got to use an inline-if statement:

 

<?php 

$image_properties=array(
     'src'=> (!empty ($this->session->userdata('avatar') ) ) 
          : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
          ? 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),
     'height' => '80px',
     'width' => '80px',
     'alt' => 'Avatar' 
); 

?>

Link to comment
Share on other sites

<?php

 

$image_properties=array(

    'src'=> (!empty ($this->session->userdata('avatar') ) )

          ? 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar')

          : 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),

    'height' => '80px',

    'width' => '80px',

    'alt' => 'Avatar'

);

 

?>

 

Sorry I put the colon and question mark the wrong way round. You also had an addition concatenation symbol and single apostrophe somewhere around the previous if statement; that's now been taken out.

Link to comment
Share on other sites

That's not corrected. After 1,300 posts, you really should be getting this kinda stuff.

 

This is an issue that's pretty much solved for you if you're using an editor that matches brackets.

Link to comment
Share on other sites

Then you're doing something wrong.

 

<?php

$check = TRUE;

$array = array(
'key' => ($check ? 'foo' : 'bar'),
'cas' => 'bah'
);

print_r($array);

?>

 

returns

 

Array
(
    [key] => foo
    [cas] => bah
)

 

It's a PEBCAK issue. You haven't even told us what error you're getting, nor the code you're trying. Best of luck.

Link to comment
Share on other sites

I'm still trying to figure out the syntax error in this code just like my post has said:

 

$image_properties = array(
                        'src' => (!empty ($this->session->userdata('avatar')                   
                        : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
                        ? 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' 
                    ); 
                ?>	

Link to comment
Share on other sites

Okay so now there's an issue with : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar')

 

<?php 
                    $image_properties = array(
                        'src' => (!empty ($this->session->userdata('avatar')                   
                        : 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
                        ? 'assets/peach/img/sprites/userinfo/avatars/avatar.png'),
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' )
                    ); 

Link to comment
Share on other sites

No, there isn't.

 

You've thrown a closing bracket in the wrong place. You're more than welcome to keep guessing the solution, but I think I'm through with saying you've guessed wrong. Best of luck, my suggestion is to hire a programmer if you want the job done, or go back to the basics if you actually want to learn.

Link to comment
Share on other sites

If you're having that much trouble with this one, try formatting it more simply at first. This is a complex line of code the way you have it and you aren't able to spot the error.

 

Here's your code made more simple IMO. I left in your error, and moved the ternary stuff out of the array and into an if. Do you see the error now?

 

<?php 
$src = 'assets/peach/img/sprites/userinfo/avatars/avatar.png';
if(!empty($this->session->userdata('avatar'))){
    $src = 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar');
}

$image_properties = array(
   'src' => $src,
   'height' => '80px',
   'width' => '80px',
   'alt' => 'Avatar' )
); 

Link to comment
Share on other sites

I tried this but got a "cannot use method return value" error on that src line:

 

$image_properties = array(
                       'src' => (!empty ($this->session->userdata('avatar') ) )        
                        ? 'assets/peach/img/sprites/userinfo/'.$this->session->userdata('avatar') 
                        : 'assets/peach/img/sprites/userinfo/avatars/avatar.png')  
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' 
                    );  

Link to comment
Share on other sites

Well that's an entirely new error, and you should be able to compare the old one to see the original error.

 

empty() works on variables, and you're using a method which probably returns nothing if it's empty. So there's no variable.

Link to comment
Share on other sites

Well that's an entirely new error, and you should be able to compare the old one to see the original error.

 

empty() works on variables, and you're using a method which probably returns nothing if it's empty. So there's no variable.

 

Or he's not counting the parentheses

Link to comment
Share on other sites

Topic solved!

 

Solution:

<?php 
                    $avatar = $this->session->userdata('avatar');
                    $image_properties = array(
                       'src' => (!empty ($avatar) )        
                        ? 'assets/peach/img/sprites/userinfo/avatars/'.$this->session->userdata('avatar') 
                        : 'assets/peach/img/sprites/userinfo/avatars/avatar.png',
                        'height' => '80px',
                        'width' => '80px',
                        'alt' => 'Avatar' 
                    );  
                    ?>

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.