So I have this code block in my controller (I know, db stuff should be in model..I’ll transfer it over there once working). Searches the database for a “subscription” and echos in the view. That works. However, in my view, I want to add an IF statement like the second block of code below – but it’s not working. Suggestions? In other words, how would you write the IF statement in the view? Pretty sure the query portion is incorrect…
// displays current subscription in view
$userid = $this -> session -> userdata('user_id');
$this -> db -> select('subscription');
$this -> db -> where ('id', $userid);
$query = $this -> db -> get('subscriptions');
if($query -> result() == TRUE) // if subscription exists for user, display:
{
foreach ($query->result() as $row)
{
$data['packagename'] = $row->subscription;
}
}
In view:
<?php if($query -> result() == TRUE) : ?>
<? echo $packagename; ?>
<?php endif ?>
Basing on what I see from your code, I am getting the feeling that you think whatever variables you use in your controller are automatically available within your view. This is not the case. You should be setting your variables and performing your logic in the controller and passing what you want displayed to your view.
This is what your controller should look like:
You do not need a
foreachas you are pulling by ID – you only want one record.Then, in your view, just display it like this: