1

I had no idea it was not possible to use if else statement inside an array in php. I've searched stackoverflow and found that shorthand version should actually work fine.

I tried it but still have errors and page doesn't load, my code:

query_posts (array( ($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : "") 'order' => 'DESC', 'orderby' => $prefooter_order_logic, 'posts_per_page' => '10', 'post_type' => 'post', 'category_name' => $prefooter_category_select )); 

This doesn't give me error:

but it doesn't work...

($prefooter_order_logic == 'xviews' ? " 'v_sortby' => 'views', 'v_orderby' => 'desc', " : " 'order' => 'DESC', 'orderby' => $prefooter_order_logic, "), 

I've decided to stop playing around and do it easiest way:

btw, any thoughts if this is the best way to do it? or not?

 if ($prefooter_order_logic == 'xviews') { query_posts (array( 'v_sortby' => 'views', 'v_orderby' => 'desc', 'posts_per_page' => '10', 'post_type' => 'post', 'category_name' => $prefooter_category_select )); } else { query_posts (array( 'order' => 'DESC', 'orderby' => $prefooter_order_logic, 'posts_per_page' => '10', 'post_type' => 'post', 'category_name' => $prefooter_category_select )); } 

4 Answers 4

2

That will produce syntax error, unexpected T_CONSTANT_ENCAPSED_STRING because you are missing a comma at the end of this line:

($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : ""), ^ here 

Edit:

Just modify the array after creating it, based on your condition:

$arr = array( 'posts_per_page' => '10', 'post_type' => 'post', 'category_name' => $prefooter_category_select ); if($prefooter_order_logic == 'xviews') { $arr['v_sortby'] = 'views'; $arr['v_orderby'] = 'desc'; } else { $arr['order'] = 'DESC'; $arr['orderby'] = $prefooter_order_logic; } query_posts($arr); 

Alternatively, use array_merge():

$arr = array( 'posts_per_page' => '10', 'post_type' => 'post', 'category_name' => $prefooter_category_select ); $arr = $prefooter_order_logic == 'xviews' ? array_merge($arr, array('v_sortby' => 'views', 'v_orderby' => 'desc')) : array_merge($arr, array('order' => 'desc', 'orderby' => $prefooter_order_logic)); query_posts($arr); 
Sign up to request clarification or add additional context in comments.

4 Comments

You were right, it was comma at the end. But now none of my array variables are read. I also have to place $ variable inside else part... I updated my question with new code, can you take a look.
what if I move whole query under if? if == xviews -> query 1, else query 2. And finish playing around with things I don't know :D
@SandroDzneladze yes you can put the whole query under the if else but that will be duplicating code.
Readability of if else is better yes, only if you don't duplicate the whole query under both outcomes.
2

here is the problem first that there is not ) after the ? and , at teh end

$prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : "") 

should be

($prefooter_order_logic == 'xviews' )? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : ""), 

1 Comment

here is the problem first that there is not ) after the ? - that has nothing to do with the problem
1

You should specify what errors you have, and it's hard to tell what you are trying to do.

However, you are missing a comma at the end of the line with the ternary operator:

'desc'," : "") 

Put a comma after that.

Comments

0

I guess you couldn't evaluate PHP code like this:

"'v_sortby' => 'views', 'v_orderby' => 'desc'," 

It should looks like piece of text for PHP interpreter, not part of your array, but I'm not sure.

Comments