being new to php i am not ble to figure out how to use if else inside php array. i tried to do something like this

function column_title($item){
        $status=false;
        if($item->uTestimonials_approval =='0')
           $status=false;
         else
          $status=true;

        //Build row actions
        $actions = array(
             $status ? 'unapprove'      => sprintf('<a href="?page=%s&action=%s&id=%s">Unapprove</a>',$_REQUEST['page'],'unapprove',$item-> uTestimonials_id),:
            'approve1'      => sprintf('<a href="?page=%s&action=%s&id=%s">Approve</a>',$_REQUEST['page'],'approve',$item-> uTestimonials_id),

            'delete'    => sprintf('<a href="?page=%s&action=%s&id=%s">Delete</a>',$_REQUEST['page'],'delete',$item-> uTestimonials_id),
        );

        //Return the title contents
        return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
            /*$1%s*/ $item-> uTestimonials_message,
            /*$2%s*/ $item-> uTestimonials_id,
            /*$3%s*/ $this->row_actions($actions)
        );
    }

but i am getting following parsing exception

Parse error: syntax error, unexpected T_DOUBLE_ARROW

can any one help me to find what exactly i am doing wrong thanks in advance

I am perfectly fine for negative voting but its always better to specify why some one did that so that person can improve him/herself

share|improve this question
    
What did you intend to do by doing $status ? 'unapprove'? – Jeune Nov 7 '11 at 16:20
    
@Jeune i am working on some wordpress functionality and based on the approval status i need to show certain text to use so i am using this. – Umesh Awasthi Nov 7 '11 at 16:21
4  
To let you know, whole your code, with all this sprintf and ternary being extremely unreadable – Your Common Sense Nov 7 '11 at 16:22
up vote 5 down vote accepted

You cannot do this for a key value:

 $status ? 'unapprove'

Try something like this instead:

    //Build row actions
    $ap = 'approve';
    $key = 'approve1';
    if($status){             
       $ap = 'unapprove';
       $key = 'unapprove';
    }
    $actions = array(
        $key => sprintf('<a href="?page=%s&action=%s&id=%s">'.$ap.'</a>',$_REQUEST['page'],$ap,$item-> uTestimonials_id),:
        'delete' => sprintf('<a href="?page=%s&action=%s&id=%s">Delete</a>',$_REQUEST['page'],'delete',$item-> uTestimonials_id),
    );
share|improve this answer
    
any idea how i can achieve the desired thing as i have to use key and a value against it – Umesh Awasthi Nov 7 '11 at 16:18
    
@user what about just a regular if setting a variable before array definition code, eh? – Your Common Sense Nov 7 '11 at 16:19
    
@user702325 see my update – Neal Nov 7 '11 at 16:21
    
@Col.Shrapnel I added that ^_^ – Neal Nov 7 '11 at 16:21
    
No need to do that, you can use shorthand notation for an array key. He just didn't specify the else portion of it that's why it was throwing an error. – aziz punjani Nov 7 '11 at 16:22

You cannot do that inside the array definition.

Instead, you could add these to the array after the definition like this:

$actions = array(
    'delete'    => sprintf('<a href="?page=%s&action=%s&id=%s">Delete</a>',$_REQUEST['page'],'delete',$item-> uTestimonials_id),
);

if ($status) {
    $actions['unapprove'] = sprintf('<a href="?page=%s&action=%s&id=%s">Unapprove</a>',$_REQUEST['page'],'unapprove',$item-> uTestimonials_id);
}
else {
    $actions['approve'] = sprintf('<a href="?page=%s&action=%s&id=%s">Approve</a>',$_REQUEST['page'],'approve',$item-> uTestimonials_id);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.