Cam
Cam

Reputation: 1902

Pass Variable argument into array

Im sure this has been answered, so if you have a relevent link please let me know.

I need to pass a condition into an array. (if this is a bad idea also please offer another solution im all ears).

PHP

 $solUpdate = "";
    if($info[$i]->shortSubject == $el && $info[$i]->id != $tid){
                $solStat = $wpdb->get_content('is_ticket','ticket_id ='.$tid,'solarStatus');
                $check = solarCurl($info[$i]->id,"Tickets",false);
                $check = json_decode($check);
                if($solStat != $st){
                    $solUpdate = ("solarStatus"=>$st);
                }
                $wpdb->update('is_ticket', array('solarID'=>$info[$i]->id,$solUpdate), array('parent_ticket'=>$tid, 'solarTType'=>$check->problemtype->id));
            }

Here is what I am trying to accomplish,

If $solStat != $st, then I want to update that Row with $st, but.... $st may not always be different, so there is no actual update to do, BUT... I dont want to write an else to my if, I think thats terrible code practice. Im sure this is easy, and I have looked to find how to do this. Thank you for your help.

Upvotes: 0

Views: 25

Answers (1)

axiac
axiac

Reputation: 72226

The line:

$solUpdate = ("solarStatus"=>$st);

is not valid PHP.

You can collect the columns you want to update in an array stored in $solUpdate before running the $wpdb->update() call:

$solUpdate = array();
if ($info[$i]->shortSubject == $el && $info[$i]->id != $tid) {
    $solStat = $wpdb->get_content('is_ticket', 'ticket_id='.$tid,'solarStatus');
    $check = solarCurl($info[$i]->id,"Tickets",false);
    $check = json_decode($check);

    $solUpdate['solarID'] = $info[$i]->id;
    if ($solStat != $st) {
        $solUpdate['solarStatus'] = $st;
    }

    $where = array(
        'parent_ticket' => $tid,
        'solarTType' => $check->problemtype->id
    );

    $wpdb->update('is_ticket', $solUpdate, $where);

}

Upvotes: 2

Related Questions