Calling a block function
Permalink 1 user found helpfulI am creating my first custom block.
I have created a function in the controller.php:
function tester() { $myString = "Hello!"; echo $myString; }
I am trying to call this function from my view.php file using:
<?php $func = 'tester'; $func(); ?>
But i keep getting an error Fatal error: Call to undefined function tester().
Am i calling it wrong..?
(Thanks in advance)
If its a block destined for the marketplace, you should use
$myString = t('Hello');
I have this working well now.
Thanks again for your input.
I have another issue that i cant find documented, In my controller I querying the db - this is returning results from which i can se the number of rows using $count. What i need to do now is assign the information I have pulled out to variables that I can use in my view.php
This query has 5 bits of info per row. I need to take the results and loop round them to display the info in my view block.
Any ideas..?
function getCategories() { $db = Loader::db(); $r = $db->Execute("SELECT * FROM btwbproviders WHERE provider_category = '{$Category}' "); $count = $r->RecordCount(); return $count; }
If its any more than a simple query, you may want to put it in a model.
Also, you don't want to duplicate what C5 already provides for loading/saving block data.
Sorry but would it be possible to post the code as an example. This is the last piece of the puzzle, I need to loop through my results and output them in the view.php.
Thanks again
public function view(){ // Pseudocode - incomplete and will not compile .... .... $data = $db->getAll(.....); $useful_data = whatever_manipulations_of_data ($data); $this->set('useful_data', $useful_data ); }
In the block view :
Method 2. (Less common, delays the query until the view is rendered which may be advantageous for some blocks). In the block controller:
// Pseudocode - incomplete and will not compile public function get_useful_data(){ .... .... $data = $db->getAll(.....); $useful_data = whatever_manipulations_of_data ($data); return $useful_data; }
In the block view :
You should get a copy of Remo's book, see the last section 'Concrete5' of http://www.concrete5.org/documentation/how-tos/designers/absolute-b...
Are you really sure you need to run the query? As I noted earlier, if it is block data you are showing, then C5 will already provide it as variables scoped to the block controller and view.
Code for controller: I need to pull out the id, provider name and description from a block that already exists (I assume this is how you do it but I keep getting errors)
public function view(){ $data = $db->getAll("SELECT bID, provider_name, provider_image_fID, provider_description FROM btwbproviders WHERE provider_category = '{$Category}' "); $this->set('provider_name', $provider_name ); $this->set('provider_image_fID', $provider_image_fID ); $this->set('provider_description', $provider_description ); }
This should pull out a few rows. In my view.php I would like to output each row individually in the following format (Each row would contain the div and all of the info)
<div class="item"> <h1><?php echo htmlentities(print_r($provider_name,true)); ?></h1> <p><?php echo htmlentities(print_r($provider_description,true)); ?></p> <?php echo htmlentities(print_r($provider_image_fID,true)); ?> </div> // loops to next <div class="item"> for next row
As before any direction would be much appreciated.
To see the data, try (temporarily) in the controller view():
$this->set('data', $data);
Then in the block view, use the diagnostic code I noted above to see what the data structure looks like (echo htmlentities(print_r($data,true));). It will be an indexed array of associative arrays where each has a key $data[index][provider_name]. If there is only one row, you could use $db->getOne();
As I also noted above. ARE YOU REALLY SURE YOU NEED TO MAKE THIS QUERY?. I have a feeling you are looking for an over-complex solution to a simple problem.
Concrete5 sets up block data for you and also reads it and provides it to the block controller as object variables (such as $this->provider_name;).
I have done as you have suggsted but keep getting an error:
public function view(){ $data = $db->getAll("SELECT bID, provider_name, provider_image_fID, provider_description FROM btwbproviders WHERE provider_category = '{$Category}' "); $this->set('data', $data); }
The error is Fatal error: Call to a member function getAll() on a non-object in line 26 (which is the $data row.... Any ideas why..?
I now need to assign a variable to each database field name. (E.G. I need to assign the database field [provider_name] to $providername. I can then wrap in relevant html and display in the view.php.
Once assigned all variables I need to loo through the reults.
How do i do this please..?
Overall, your example would be better structured as
Controller:
View: