0

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

3
  • What did you intend to do by doing $status ? 'unapprove'? Commented Nov 7, 2011 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. Commented Nov 7, 2011 at 16:21
  • 4
    To let you know, whole your code, with all this sprintf and ternary being extremely unreadable Commented Nov 7, 2011 at 16:22

2 Answers 2

5

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),
    );
Sign up to request clarification or add additional context in comments.

6 Comments

any idea how i can achieve the desired thing as i have to use key and a value against it
@user what about just a regular if setting a variable before array definition code, eh?
@Col.Shrapnel I added that ^_^
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.
@Interstellar_Coder look at the OP's code, the else was specified, but in the wrong place.
|
2

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.