Making a small block type, if else not working
PermalinkI am attamepting to make a small blocktype and I have set it up to have two categories, either:
employee [id 1]
or
employer [id 2]
And I want to use this specific id to change the background color of my div using if/else statement, but no matter if I choose that a person belongs to category 1 or category 2, it does not change the background color.
I need help!!
My code is here for view.php
<?php if ($this->categoryID == 1) { echo "<div class=\"user-information\" style=\"background: green\"; >"; }else { echo "<div class=\"user-information\" style=\"background: red\"; >"; } $html = Loader::helper('html'); $picture = $this->controller->getPicture(); echo "<h2>{$title}</h2>"; if ($picture) { echo $html->image($picture->getURL()); } echo "<p>{$description}</p>"; echo $categoryID; echo $category;
Please do not hesitate to respond if you have any input on this matter :0)
//Carsten - Denmark
Thanks for the reply.
In my controller I do not have this, all I have is some functions, but all the ID´s are set in my controller.php and with the code I showed yesterday I am able to echo the categoryID and have it shown in my html code, without errors.
So I am a bit in the dark as to why I just can not make a standard if/else on behalf of this.
For instance in my controller.php, I have this (among others):
function getCategories() { $db = Loader::db(); return $db->GetAssoc('SELECT categoryID, category FROM btUserInformationCategories ORDER BY category'); }
I do not know if this gives you a bit more insight...?
If any thoughts, please reply
//Carsten - Denmark
That said.
The same is not true for the variables. You should be "staging" you variables in the controllers view() method.
For example in controller.php
public function view() { // additional controller logic here $this->set('categories', $this->getCategories()); } protected function getCategories() { $db = Loader::db(); return $db->GetArray('SELECT categoryID, category FROM btUserInformationCategories ORDER BY category'); }
Then in our view.php we can do
<?php foreach($categories as $category) { if($category['categoryID'] == 1) { // do stuff } else { // do other stuff } ?>
This is part of the MVC Architecture that Concrete5 uses.
There is a bit of an explanation here
http://www.concrete5.org/documentation/developers/blocks/mvc-approa...
EDIT: Updated to reflect error reported below.
I now tried putting your code into play in my controller but all it says is this:
Fatal error: Cannot redeclare btUserInformationCategories::view() in C:\xampp\htdocs\concrete5422\blocks\product_information\controller.php on line 43
I am wondering, this additional controller logic, what might that be exactly?
public function view() { // additional controller logic here $this->set('categories', getCategories()); } protected function getCategories() { $db = Loader::db(); return $db->GetArray('SELECT categoryID, category FROM btUserInformationCategories ORDER BY category'); }
Hope you can help
//Carsten
public function view() { $this->set('categories', $this->getCategories()); }
It is now starting to look like something, but I just ran into another problem, and if you have the time and energy to look at it please do so, and perhaps reply.
My code is now:
<?php foreach($categories as $category) { if($category['categoryID'] != 1) { echo "<div class=\"user-information\" style=\"background: green\" >"; } else { echo "<div class=\"user-information\" style=\"background: red\" >"; } $html = Loader::helper('html'); $picture = $this->controller->getPicture(); echo "<h2>{$title}</h2>"; if ($picture) { echo $html->image($picture->getURL()); } echo "<p>{$description}</p>"; echo $categoryID; //this is either a 1 or a 2 numerically
But now it echo´s four blocks, but I only made two?
The first block comes out with the number 1 on both. The first is red and the other green.
Then comes two blocks with the number 2, the first is red and the last one is also green ?
Any thoughts ?
//Carsten
It looks like you are call $this->categoryID which is what you would typically do in controller.php
In order to make variables available in view.php you need to 'set' them in the controller.
example(in controller.php)
Then in your view.php you can just use it like an ordinary variable
Hopefully that's is what you were doing. If not let me know a little more info and I'll try to explain a little better.
Best Regards,
Mike