0

I'm working on a similar thing as this one. But I'm trying to assign button either "Join" or "Enter" based on if someone joined the group. The problem is that I'm not sure how I can pass the variable from the category ID ($cats_id) to the view file.

I created a function in the model that checks if the row exists and returns true.

// check if joined the group
    public static function checkIfJoined($cats_id)
    {
        $database = DatabaseFactory::getFactory()->getConnection();
        $users_id = Session::get('user_id');

        $sql = "SELECT cats_id,users_id FROM categories_joined WHERE users_id = :users_id AND cats_id = :cats_id";
        $query = $database->prepare($sql);
        $query->execute(array(':users_id' => $users_id, ':cats_id' => $cats_id));

        // fetchAll() is the PDO method that gets all result rows
        if ($query->rowCount() >= 1 || Session::get('user_account_type') == 7) {
            return true;
        } 
    }

Then in Controller I render the model to the view.

public function index()
{   
    $cats_id = ""; // this doesn't work right obviously
    $this->View->render('dashboard/index', array(
        'categories' => DashboardModel::getAllCategories(),            
        'joined' => DashboardModel:: checkIfJoined($cats_id)           
    ));
}

in the view I pass the variable from the preview function 'categories'.

    <?php if ($this->categories) { ?>

                    <?php foreach($this->categories as $key => $value) { ?>
...
<?php $cats_id = $value->cat_id; if ( $this->joined == true ): ?>Enter
<?php else: ?>Join
<?php endif; ?>
1
  • Are you trying to pass data from the view to the controller? Commented Dec 28, 2016 at 23:28

1 Answer 1

2

You can never pass anything from view to controller because view is parsed after controller.

What you can do here is use model directly by calling DashboardModel::checkIfJoined($cats_id) in your view but that's not perfect approach.

It'll be better to prepare that data in the controller and then pass it to view.

Example controller

public function index()
{
    $this->View->render('dashboard/index', array(
        'categories' => DashboardModel::getAllCategories(),
        'userCategories' => DashboardModel::getUserCategories()
    ));
}

Example view

<?php
if ($this->categories) {
    foreach ($this->categories as $key => $value) {
        if (in_array($value->id, $this->userCategories) {
            echo 'Joined';
        } else {
            echo 'Join';
        }
}
?>

In this example DashboardModel::getUserCategories() should return results from SELECT cats_id FROM categories_joined WHERE users_id = :users_id.

Sign up to request clarification or add additional context in comments.

6 Comments

How will that change in the view file?
@Guliverkel probably it's best to fetch all categories that user joined and pass them to the view. There you can check if given category ID exist in that list. It'll be more efficient than checking each category in separate query.
That's what I thought. This is such a simple thing to do in the procedural php, how come it's difficult to do in mvc? So what is the right way to display either "enter" or "join" button based on if the row exists in the table?
All categories are fetched and passed via 'categories' => DashboardModel::getAllCategories(), I do separate query to see if the user actually joined that and that is stored in the separate table because the user can join multiple categories
@Guliverkel yes, you are fetching all categories in one query but you can also fetch all data from pivot table. That way you'll get two arrays: categories entities and user categories entities. You should then pass both to view (or better pass only IDs for user categories to do simple in_array on them).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.