Adding php code to include in view - custom component

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
mrscooper
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Fri Nov 29, 2013 6:34 pm

Adding php code to include in view - custom component

Post by mrscooper » Tue Feb 04, 2014 10:25 pm

Hello! I understand that to use a function you create it in the model and define the variable in the view.html.php. This works when the function returns a single value. What if I need to include some php that doesn't return a single value:
example:

Code: Select all

public function myfunction(){
$myarray = array(.....);
$a = array();
$b = array();

foreach($myarray as $array)
{
   $a[] = $array[0];
   $b[] = $array[1];
}
}
I want to be able to use both variables $a and $b in the view.

I appreciate any help or direction. Thanks!

User avatar
H13
Joomla! Ace
Joomla! Ace
Posts: 1545
Joined: Sun Dec 10, 2006 6:39 pm
Location: Czech Republic
Contact:

Re: Adding php code to include in view - custom component

Post by H13 » Tue Feb 04, 2014 11:27 pm

Hi, just return an object or array.

Code: Select all

$returnValue = array();

$i = 0;
foreach($myarray as $array)
{
   $returnValue['a'][$i] = $array[0];
   $returnValue['b'][$i] = $array[1];
   $i++;
}
return $returnValue;
Jan
- Phoca Cart - Joomla eCommerce App - https://www.phoca.cz/phocacart
- Phoca Gallery - powerful image gallery
- Phoca Restaurant Menu - https://www.phoca.cz/phocamenu
- Phoca Download - download manager for Joomla

mrscooper
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Fri Nov 29, 2013 6:34 pm

Re: Adding php code to include in view - custom component

Post by mrscooper » Tue Feb 04, 2014 11:49 pm

Thanks for the info.

So that would go in the model?

Sorry if I sound like a dork but how would I use each individual variable in the tmpl page?

User avatar
H13
Joomla! Ace
Joomla! Ace
Posts: 1545
Joined: Sun Dec 10, 2006 6:39 pm
Location: Czech Republic
Contact:

Re: Adding php code to include in view - custom component

Post by H13 » Wed Feb 05, 2014 2:12 pm

Hi, if you add all your variables to one array in a model, you just load it in your html (view) and you can access the return variable by its arrays.

$var = $model->myfunction();

$a = $var['a'];
foreach($var['a'] ....)

$b = $var['b'];
foreach($var['b'] ...)

etc.
- Phoca Cart - Joomla eCommerce App - https://www.phoca.cz/phocacart
- Phoca Gallery - powerful image gallery
- Phoca Restaurant Menu - https://www.phoca.cz/phocamenu
- Phoca Download - download manager for Joomla

mrscooper
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Fri Nov 29, 2013 6:34 pm

Re: Adding php code to include in view - custom component

Post by mrscooper » Wed Feb 05, 2014 3:30 pm

Thank you! This helps me so much! I appreciate your patience!

User avatar
H13
Joomla! Ace
Joomla! Ace
Posts: 1545
Joined: Sun Dec 10, 2006 6:39 pm
Location: Czech Republic
Contact:

Re: Adding php code to include in view - custom component

Post by H13 » Wed Feb 05, 2014 4:22 pm

Ok
- Phoca Cart - Joomla eCommerce App - https://www.phoca.cz/phocacart
- Phoca Gallery - powerful image gallery
- Phoca Restaurant Menu - https://www.phoca.cz/phocamenu
- Phoca Download - download manager for Joomla


Locked

Return to “Joomla! 3.x Coding”