0

Current code:

$columns = array(
    array('db' => 'code', 'dt' => 0),
    array('db' => 'img', 'dt' => 1),
    array('db' => 'name', 'dt' => 2),
    array('db' => 'short_description', 'dt' => 3),
);

What I would like to create (I know this code is invalid, I need advice on how to achieve what I am going for):

$i=0;
$code=$_POST['code'];
$img=$_POST['img'];
$name=$_POST['name'];
$short_description=$_POST['short_description'];


$columns = array(
    if($code){
    array('db' => 'code', 'dt' => $i),
    i++;
    }else if($img){
    array('db' => 'img', 'dt' => $i),
    i++;
    }else if($name){
    array('db' => 'name', 'dt' => $i),
    i++;
    }else if($short_description){
    array('db' => 'short_description', 'dt' => $i),
    }
);

2 Answers 2

2

try like this

if(!empty($code))
$columns[] =  array('db' => 'code', 'dt' => $i++);
else if(!empty($img))
$columns[] =  array('db' => 'img', 'dt' => $i++);
else if(!empty($name))
$columns[] =  array('db' => 'name', 'dt' => $i++);
else if(!empty($short_description))
$columns[] =  array('db' => 'short_description', 'dt' => $i++);
Sign up to request clarification or add additional context in comments.

Comments

1

You could use array_merge().

$columns = array(
    array('db' => 'code', 'dt' => 0),
    array('db' => 'img', 'dt' => 1),
    array('db' => 'name', 'dt' => 2),
    array('db' => 'short_description', 'dt' => 3),
);

if ({your condition}) {
    $columns = array_merge($columns, array(array('db' => 'short_description', 'dt' => 3)));
}

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.