Lets refer to ‘Sort by‘ section on Yelp:
I have 3 anchor() functions that generate the 3 links. When one of the ‘sort by’ option is selected, I want to remove the hyperlink/anchor() and bold it to show that it is selected just like on the page above.
Right now I am thinking of doing a uri_to_assoc() to $arr because the selected option will be in the url like controller/function/sort_by/best_reviews/.., then based on what is the key (best_reviews) or element (sort_by), use the following code in view
<?php
$arr = uri_to_assoc(3);
if($arr['sort_by'] == 'best_reviews') {
echo "<strong>Best Reviews</strong>";
} else {
anchor('controller/function/sort_by/best_reviews', 'Best Reviews');
]
?>
Now I believe the MVC idea is to keep little php code (logic code?) in the view. In this case, will you put the above code in the view, or have that logic in controller/model then pass the final code to display (either the echo or the anchor()) in an array and just echo the array in view?
.
.
Mini Question (PHP)
If $arr[‘sort_by’] is not defined in the case that /sort_by/variable does not exist in the url, the statement if($arr[‘sort_by’] == ‘best_reviews’) will give an error. Will u nest all the above code in a if(isset($arr[‘sort_by’])){} ?
You are modifying presentation, so you must do it in the view. It has nothing to do with any business logic whatsoever. You can use code in the view, but for the sole purpose of presentation; there’s no limit on the amount of code, only on the purpose of it.