0

I'm using full calendar from Arshaw, this is how I'm adding events to the calendar

echo json_encode(array(

        array(
            'id' => 111,
            'title' => "Place Date",
            'start' => "$place_date[0]",
            'color' => "purple"
        ),

        array(
            'id' => 222,
            'title' => "Due Date",
            'start' => "$due_date[0]",
            'color' => "red"
        ),

        array(
            'id' => 333,
            'title' => "Confirmed",
            'start' => "$confirmdate[0]",
            'end' => "$promisedate[0]",
            'color' => "blue"
        )

    ));


}

I don't always have information about "confirmdate" so I need to perform an if statement to check if my mysql returns an empty value or not, I like to achieve something like this:

if (mysqli_num_rows($confirmdate[0] > 0) {
echo  array(
            'id' => 333,
            'title' => "Confirmed",
            'start' => "$confirmdate[0]",
            'end' => "$promisedate[0]",
            'color' => "blue"
        )
}

Is there a way to perform an if like this inside a json_encode?

1
  • I don't think you want to call mysqli_num_rows like that. I took it out in my answer. Commented Nov 22, 2013 at 17:34

1 Answer 1

2

There's no way to conditionalize an entire element in an array() call (the fact that it's in json_encode is immaterial). Construct your array in multiple steps:

$result = array(
    array(
        'id' => 111,
        'title' => "Place Date",
        'start' => "$place_date[0]",
        'color' => "purple"
    ),
    array(
        'id' => 222,
        'title' => "Due Date",
        'start' => "$due_date[0]",
        'color' => "red"
    )
);

if ($confirmdate[0] > 0) {
    $result[] = array(
        'id' => 333,
        'title' => "Confirmed",
        'start' => "$confirmdate[0]",
        'end' => "$promisedate[0]",
        'color' => "blue"
    );
}

echo json_encode($result);
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know you needed to be an expert to know how to add something to an array.

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.