Two dropdown lists in codeigniter - codeigniter

I have two drop down lists in my form and want to get data from database. after getting first drop down successfully, I tried to do the same for the next one. But it gives an error.
The error I get
My code:
Model
Controller
View

Show your data in all foreach loop like this:
<?php foreach($borrower_name as $row){ ?>
<option value="<?php echo $row['borrower_name ']?>" ><?php echo $row['borrower_name '];?></option>
<?php } ?>
OR
foreach($borrower_name as $row)
{
echo '<option value="'.$row['borrower_name '].'">'.$row['borrower_name '].'</option>';
}

In your modal file replace return $query->result(); with return $query->result_array();

Related

How use condition in codeigniter form_dropdown?

Here state_list, state3 showing undefined error in form_dropdown but here I give condition, how solve this error, I also give condition both controller and model but same error occurred?
<?php
if(!empty($state_list))
{
$state_list;
}
if(!empty($state3))
{
$state3;
}
$js = 'id="state" class="form-control" onchange="get_city(this.value)" onblur="setval(this.name)"';
echo form_dropdown('state', $state_list, $state3, $js);
?>
load the form helper
$this->load->helper('form');
form helper
<?php
$js = 'id="state" class="form-control" onchange="get_city(this.value)" onblur="setval(this.name)"';
if(!empty($state_list)){
echo form_dropdown('state', $state_list, $state3, $js);
}else{
$state_list = array();
echo form_dropdown('state', $state_list, $state3, $js);
}
?>

Codeigniter : Auto Fill DropDown From Database not working

I am trying to fetch data in a drop down field from database in Code Igniter. Here is the code for it :
<input type="hidden"
id="tour_package_id"
class="form-control"
name="tour_package_id" />
<?php $i='class="form-control"
name="tour_name"
id="tour_name"';
echo form_dropdown('tour_package_id', $tour_list,
set_value('tour_package_id', $tour_package_id),$i);?></div>
I am getting the following error for that.
Severity: Notice
Message: Undefined variable: tour_package_id
Filename: Packages/add_location.php
Line Number: 40
Tried all the stuff but its not working.
Thanks
Bhagya
is a drop down a combobox right?
try this:(this is a hard code just change it)
example:
Table(tbl_Category)
ID | Category_Name
1 Hardware
2 Software
Model
public function getCategory(){
$this->db->select('*');
$this->db->from('tbl_Category');
$query = $this->db->get();
return $query;
}
Controller(The trigger to view)(load the model also)
public function show(){
$this->data["result"] = $this->Model->getCategory();
$this->load->view('Category',$this->data); //this->data will get the result value to your view
}
The view(Category.php)
<select>
<?php foreach($result as $results){ ?>
<option value="<?php echo $results->ID; ?>"><?php echo $results->Category; ?></option>
<?php } ?>
</select>
The foreach inside the select makes it great! like if i add more Category the option will automatically adds up! Try this! hope this helps!

Calling a model function in view

i am trying to access a function in model, in my view in codeigniter and its not working.
can somebody please tell me where is the problem.
MODEL:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
VIEW:
<select id="special" >
<?php echo $this->dashboard_ext_model->gettemplates(); ?>
</select>
Change this:
echo "<pre>"; print_r($result);exit;
To this:
echo "<pre>"; print_r($result); echo "</pre>";
Basically remove the exit. Exit aborts the script.
You should never have a good reason to call the model from the view. Model data should be passed to the controller, to then pass to the view.
Since this is MVC (Model View Controller) you shouldn't call a model from a view.
You should be calling the Model inside the controller then passing to the view as an argument.
Model:
function gettemplates()
{
$sql = "select * from srs_templates";
$query = $this->db->query($sql);
$result = $query->result_array();
echo "<pre>"; print_r($result);exit;
return $result;
}
Controller:
function gettemplates(){
//$this->dashboard_ext_model = $this->load->model('dashboard_ext_model');
//uncomment this if you didn't load the model
$data['templates'] = $this->dashboard_ext_model->gettemplates();
$this->view->load('page_name', $data);
}
View:
<select id="special" >
<?php echo $templates ?>
</select>
MVC may look stupid at first but it really helps in large size projects.
MVC DESIGN: http://i.stack.imgur.com/Beh3a.png

How to make second root category navigation in magento?

Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.

MAGENTO getting the descriptions from a sub category

Hi I'm new to magento and have been trying to set up a static block that displays a list of sub categories within a category. I've been succesfull a grabbing the sub-category images and names, but for some reason I can't seem to get the descriptions to show.
Here's the code can't anyone explain why it won't work and how I can fix it?
I've commented out a few lines because I was trying different things to get it to work.
helper('catalog/output');
$category = $this->getCurrentCategory();
getCurrentChildCategories();
?>
<?php foreach ($_categories as $_category): ?> <?php echo
$this->htmlEscape($_category->getCategoryDescription());?>
<?php if($_category->getIsActive()): ?>
<div class="subcategory-image">
<a href="<?php echo $_category->getURL()
?>" title="htmlEscape($_category->getName())
?>">
</a>
<?php /* echo "Find this item->" */ ?>
</div> <div class="sub-category-container">
<h2><a href="<?php echo $_category->getURL()
?>" title="htmlEscape($_category->getName())
?>">htmlEscape($_category->getName())
?>
getURL() ?>"
class="moreLink">[MORE...]
getDescription() ?>-->
getDescription()):
?>
categoryAttribute($_category, $_description, 'description'); ?>
-->
This is one of those cases where Varien decided that they should call "load" on the data collection before returning it when that really isn't necessary and makes the utility function utterly useless.. If you trace the code down for Mage_Catalog_Block_Navigation->getChildrenCategories() you will eventually find this in Mage_Catalog_Model_Resource_Eav_Mysql4_Category:
public function getChildrenCategories($category)
{
$collection = $category->getCollection();
/* #var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
->load();
return $collection;
}
The next to last line ->load(); means that the query is executed and the collection is loaded so it is too late to modify the query. So what you will want to do is copy and paste that in place of calling getChildrenCategories and then add the additional attributes you want to use like so:
$_categories = $category->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
;
Now the collection will include the description attribute so that getDescription() will work. Notice that you do not need to call load(), it happens automatically when you start using the iterator (the foreach loop triggers this). This is why it is pointless for the load() call to be included in that function because otherwise you could have just added one line below the function call:
$categories->addAttributeToSelect('description');
But instead you have to copy the contents of the function to tweak the query.
Change:
$_category->getCategoryDescription()
To this:
$_category->getDescription()

Resources