Calling a block function

Permalink 1 user found helpful
Hi There,

I 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)

 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
From the view use:
$controller->tester();


Overall, your example would be better structured as
Controller:
public function tester(){
  $myString = 'Hello';
  return $myString;
}


View:
echo $controller->tester();
JohntheFish replied on at Permalink Reply
JohntheFish
PS.

If its a block destined for the marketplace, you should use
$myString = t('Hello');
obaudains replied on at Permalink Reply
Thanks ~John,

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;
}
JohntheFish replied on at Permalink Reply
JohntheFish
Off the top of my head, have a look at $db->getOne and $db->getAll to run the query rather than $db->Execute. A global text search through concrete's php files will turn up loads of examples.

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.
obaudains replied on at Permalink Reply
Thanks jon, but how do I take the findings of the query (rows) and output them in my view.php.

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
JohntheFish replied on at Permalink Reply
JohntheFish
Method 1. (More conventional and usual method) In the block controller view() function:
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 :
// you can access the data as $useful_data, eg 
//( with some code to see what you have got as a data structure dump)
echo "<pre>";
echo "********************\n";
echo htmlentities(print_r($useful_data,true));
echo "\n====================";
echo "</pre>";


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 :
$useful_data = $controller->get_useful_data();
echo "<pre>";
echo "********************\n";
echo htmlentities(print_r($useful_data,true));
echo "\n====================";
echo "</pre>";


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.
JohntheFish replied on at Permalink Reply
JohntheFish
Looking at your sql again, you should also be using a '?' place holder in your query for security and safety. Again, plenty of examples if you search through the C5 code.
obaudains replied on at Permalink Reply
Again thank you form the reply, but I have had a dig around existing C5 files and cant quite get a handle on it. I will order the book as I think its key to moving forward, however Can you take one final look at my code please and tell me where I am going wrong.

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.
JohntheFish replied on at Permalink Reply
JohntheFish
You have not extracted $provider_name etc from the $data.

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;).
obaudains replied on at Permalink Reply
Thanks John, Yes i do need to run the query as no info has been added to this block. When the user adds this block they select a category from a drop down. The idea is that this will then display all of the items entered into a separate block with this category. This is the only way I can se it working.

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..?
JohntheFish replied on at Permalink Reply
JohntheFish
You need to load $db, as in $db = Loader::db();
obaudains replied on at Permalink Reply
Ahh (sorry, silly mistake). That is perfect, I can now see the array with each database field e.g. [provider_name] [provider_description] etc.

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..?
JohntheFish replied on at Permalink Reply
JohntheFish
obaudains replied on at Permalink Reply
Thanks for your help and patience