What is the purpose of using $this in Codeigniter? - codeigniter

I have a quick question: What is the difference between:
$this->data['page_detail'] = $this->page_model->find_ID();
and
data['page_detail'] = $this->page_model->find_ID();
?
To add more detail:
if ($this->uri->segment(1)=='profile')
{
if ( ! $this->data['page_detail'] = $this->page_model->find_alias($this->uri->segment(1)))
redirect();
Why not?
if ($this->uri->segment(1)=='profile')
{
if ( data['page_detail'] = $this->page_model->find_alias($this->uri->segment(1)))
redirect();

because $this represents the singleton Codeigniter instance
when we load models, we are attaching them to $this instance so we can reference them as a property of this instance.
and you also missing $ sign with data['page_detail'] it should be like this $data['page_detail']

You want to use it as
$data['page_detail'] = $this->page_model->find_ID();
It is a variable

Related

Passing get variable in CodeIgniter

How to pass get variable from my view to the controller and to the model also and load to my view again
without the use of form like
$jcid= $row['id']; // this id from other table which is parent.
$s = "SELECT * FROM `jobs` WHERE job_cat=$jcid";
$re = mysql_query($s);
$no = mysql_num_rows($re);
echo $no;
Thanks in advance
Amit
Instead of using get param use like this
This can be your form url
http://example.com/index.php/news/local/metro/crime_is_up
$this->uri->segment(3); // Will give you news
Like wise in place of 3 replace with 4 will give you metro and so on. You can access this in controller.
Uri class in the codeigniter
http://ellislab.com/codeigniter/user-guide/libraries/uri.html
First use
$this->load->helper('url');
And then
$data['$jcid'] = $this->uri->segment(3);

Calling controllers dynamically

I'm attempting to create dynamic routing in Laravel for my controllers - I know this can be done in Kohana, but I've been unsuccessful trying to get it working with Laravel.
This is what I have right now:
Route::get('/{controller}/{action?}/{id?}'...
So I would like to call controller/method($id) with that.
Ideally this is what I would like to do:
Route::get('/{controller}/{action?}/{id?}', $controller . '#' . $action);
And have it dynamically call $controller::$action.
I've tried doing this:
Route::get('/{controller}/{action?}/{id?}', function($controller, $action = null, $id = null)
{
$controller = new $controller();
$controller->$action();
});
But I get an error message: Class Controller does not exist.
So it appears that Laravel is not including all the necessary files when the controller extends the BaseController.
If I use $controller::$action() it tells me I can't call a non-static function statically.
Any ideas for how to make this work?
You can auto register all controllers in one fell swoop:
Route::controller( Controller::detect() );
If you're using Laravel 4 (as your tag implies), you can't use Controller::detect() anymore. You'll have to manually register all the controllers you want to use.
After reading that Laravel doesn’t support this anymore, I came up with this solution:
$uri = $_SERVER['REQUEST_URI'];
$results = array();
preg_match('#^\/(\w+)?\/?(\w+)?\/?(\w+)?\/?#', $_SERVER['REQUEST_URI'], $results);
// set the default controller to landing
$controller = (empty($results[1])) ? 'landing' : $results[1];
// set the default method to index
$method = (empty($results[2])) ? 'index' : $results[2];
Route::get('{controller?}/{action?}/{id?}', $controller . '#' . $method);
// now we just need to catch and process the error if no controller#method exists.

Multiple models for a view without controllers

As I've just started with Joomla component development, this might sound stupid, or might be trivial.
I would like to know if its possible to have different models attached to a view, without using separate controllers?
My intention is actually to use same model for different views.
Thanx in advance...
Yes, you can load any model in the view with
$model = JModel::getInstance('ModelName', 'ComponentNameModel');
OK, got it working.
Basically you just need to check for the 'view' variable in the JRequest class:
if(JRequest::getVar('view') == 'yourtargetview') {
$modelMain = $this->getModel ( 'yourtargetmodel' );
$viewCallback = $this->getView ( 'yourtargetview', 'html');
$viewCallback->setModel( $modelMain, true ); // true is for the default model;
}
And then in your target view class, reference the model functions as follows(notice the second parameter to get call):
$this->targetFieldValue = $this->get('targetMethod', 'targetModel');
Hope it helps...

Joomla: Write and call a helper function in a component

Fledgling Joomla / PHP developer, hitting a wall understanding how to do this. Everything I found searching has been for older versions of Joomla or other frameworks and so it's all confusing the first time around.
I want to have a helper function that I can call from anywhere in my component. Basically it takes a userID input and returns their full name, let's say hair color and height. Here's the function:
function get_profile_info($userID) {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->SELECT('u.id as UserID
, u.name AS Name
, up.profile_value AS Hair
, up2.profile_value AS Height
');
$query->FROM (' #__users AS u');
$query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 1');
$query->LEFTJOIN (' #__user_profiles AS up ON u.id = up.user_id AND up.ordering = 2');
$query->WHERE(' u.id = '.$userID.'');
$query->GROUPBY (' u.id
, u.name
, up.profile_value
');
$db->setQuery($query);
return $query;
}
I'd put this in a file called "lookups.php" within the "helpers" folder of my component...but here's where I'm not sure what to do next. Top of lookups.php has the obligatory:
<?php defined ( '_JEXEC' ) or die;
So two questions:
1) Do I put everything in a class or keep it as a series of functions (since there will be others)?
2) How do I pass the userID and get back the values of Name, Hair, and Height in the view (view.html.php / default.php)?
Thank you!
==================================
Edit: Based on #Shaz 's response below here's where I'm at (again, just starting off and trying to wrap my head around some of this):
lookups.php
abstract class LookupHelper {
var $Name;
var $Hair;
var $Height;
public function get_profile_info($userID) {
...
(same as above until the next line)
$db->setQuery($query);
$result=$db->loadRow();
$Name = $result[1];
$Hair = $result[2];
$Height = $result[3];
$getprofileinfo = array(Name=>$Name, Hair=>$Hair, Height=>$Height);
$return $getprofileinfo;
}
}
Then in my default.php (will probably move to view.html.php):
$getprofileinfo = Jview::loadHelper('lookups'); //got an error without this
$getprofileinfo = LookupHelper::get_profile_info($userID);
$Name = $getprofileinfo[Name];
$Hair = $getprofileinfo[Hair];
$Height = $getprofileinfo[Height];
So...it's working - but it seems like there's a much easier way of doing this (specifically manually creating an array then recalling by position). Thoughts?
Thank you!!
Create the helper class and include there all your functions:
abstract class HelloWorldHelper
{
/* functions */
}
Call the function and store the result:
$var = HelloWorldHelper::get_profile_info($thatId);
Is not possible to write your helper class inside of an existing helper Joomla file that's already being calling by all your components?
By default Joomla have helper classes doing stuff, so all you have to do is to expand the core helper classes with your owns.

How to check CMS block is active?

I wonder how to check that a particular CMS block is active or not.
So far I have found that CMS block are Mage_Cms_Block_Block class that inherits from Mage_Cms_Block_Abstract class
Mage::log(get_class(Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('promo_space')
Neither of the two classes have methods that would check wether the block is active or not. How do I do it?
Mage::getModel('cms/block')->load('static_block_identifier')->getIsActive()
Replace static_block_identifier with the identifier you assigned to your CMS static block.
Got this myself
I created a method isActive(Identifiere, Value) in helper "Block" in the Mage/Cms local Module.
This is how the method looks
public function isActive($attribute, $value){
$col = Mage::getModel('cms/block')->getCollection();
$col->addFieldToFilter($attribute, $value);
$item = $col->getFirstItem();
$id = $item->getData('is_active');
if($id == 1){
return true;
}else{
return false;
}
}
parameter $attribute is table(cms-block) field such as 'identifier' or 'title' and value can be the name of the static block or identifier itself. Both used to filter down the particular static block you are interested in
Here is how i call the helper
if(Mage::helper('cms/block')->isActive('identifier','promo_space')){
//do that
}
I have also updated the config.xml file for Cms block to read my new helper and the method.
I hope its useful.
This code works for me:
if ( $this->getLayout()->createBlock('cms/block')->setBlockId('YOUR-BLOCK-ID')->toHtml() !== '' ) {}
Maybe this is old, but I use another method that works not only for cms blocks but for any other block loaded on the layout. If you need to check if a block has been loaded:
if($this->getLayout()->getBlock('your_block_name'))
//Do whatever you need here
It's pretty easy!
A better way to do this is to add observer to this event: controller_action_layout_generate_blocks_after which happens right after Magento has initialized and generated Block objects. You have access to the layout and action classes and to all generated blocks before HTML is rendered
//You can check if the block exists in the layout
$layout = $observer->getEvent()->getObserver();
$cmsBlock = $layout->getBlock($identifier);//Returns false if doesn't exist.
//You can check it in the database too:
$cmsModel = Mage::getModel('cms/page')->load($identifier);
if($cmsModel->getId() AND $cmsModel->getIsActive() == 1)
{
//CMS block is active
}

Resources