Codeigniter : Auto Fill DropDown From Database not working - codeigniter

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!

Related

Codeigniter framework: How do we trace where the variables in a given 'view' php code snippet are coming from?

I am in a codeigniter project, examining a file in the views folder. The php is as follows:
As a complete beginner, trying to make sense of this, I am trying to change the value of 'username' below to the 'firstname' that is a different field in the database. Changing the field below, from username, to firstname, causes an error.
I notice there is a loop using the variable: $results as $result)
What I want to know -as a lay person - is how to trace back where these variables are being 'called'from and which files to look into in order to find out how to use the required field, in this case, firstname, which is also from the database.
I have looked into the controllers, but can find nothing resembling this and don't know where to start, as the developer hasn't necessarily labelled everything logically.
My question is: As someone coming into a codebase that they don't know, how do you go about tracing back the logic to find out where you need to look to change a variable and make a change.
Any help on this matter would be greatly appreciated.
<?php
$rank = 0;
foreach($results as $result){$rank++;
?>
<tr style="border: 1px solid #ebebeb;padding-bottom: 0px;background-color: #fff;">
<td ><?php echo $rank; ?></td>
<td ><?php echo $result->username; ?></td>
<td ><?php echo $result->marks; ?></td>
<td ><?php echo $result->percentage; ?></td>
<td ><?php echo $result->duration; ?></td>
<td ><?php echo $result->date_time; ?></td>
<td>
<?php /*<i class="glyph-icon tooltip-button demo-icon icon-edit" title="Edit" style="color:blue;" data-original-title=".icon-edit"></i>
*/?>
<a onclick="return confirm('Are you Sure to Delete this Result???');" href="<?php echo site_url('result/delete/'.$result->id); ?>"><i class="glyph-icon tooltip-button demo-icon icon-close" title="Delete" data-original-title=".icon-close" style="color:red;"></i></a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php } ?>
The starting code on the page I mention is below: Again, notice $results. What is it? Where do I identify its origin and seek to find the fields that populate it?
<?php if(empty($results)) {?>
<div id="page-title">
<h2>No Results Found.</h2>
</br>
</div>
<?php } else { ?>
<div id="page-title">
<h2>All Results</h2>
<h3>Quiz : <?php echo $results[0]->quiz_name; ?></h3>
Delete All
</br>
</div>
What I have done so far:
I have found the controller that seems to be relating to it - pasted below:
load->model('results');
if(!$this->session->userdata('ID'))
{
$this->session->set_flashdata('noAccess', 'Sorry');
redirect(site_url());
}
}
public function index($id='')
{
$data['results'] = $this->results->get_resultByQuiz(-1);
$this->load->view('template/header.php');
$this->load->view('result/index.php',$data);
$this->load->view('template/footer.php');
}
public function view($id='')
{
$data['results'] = $this->results->get_resultByQuiz($id);
$this->load->view('template/header.php');
$this->load->view('result/index.php',$data);
$this->load->view('template/footer.php');
}
public function delAll($id = '')
{
$updated = $this->results->delAll($id);
if($updated)
{
$this->session->set_flashdata('resultDeleteSuccess','result');
}
else
{
$this->session->set_flashdata('resultDeleteFail','result');
}
redirect('quiz');
}
public function delete($id = '')
{
$updated = $this->results->delete_results($id);
if($updated)
{
$this->session->set_flashdata('deleteSuccess','result');
}
else
{
$this->session->set_flashdata('deleteSuccess','result');
}
redirect('result');
}
}
I have also found this related model.
<?php if(!defined('BASEPATH')) exit ("No direct script access allowed");
class Results extends CI_Model {
public function get_all_results()
{
$query = $this->db->get('quiz_takers');
return $query->result();
}
public function get_resultByQuiz($ID)
{
$query = $this->db->query("select (select quiz_name from quizes where id = qt.quiz_id) as quiz_name, qt.* from quiz_takers qt where qt.quiz_id = '$ID' order by qt.percentage desc ");
return $query->result();
$this->db->where('quiz_id',$ID);
$query = $this->db->get('quiz_takers');
return $query->result();
}
public function delAll($ID = '')
{
$this->db->where('quiz_id',$ID);
return $this->db->delete('quiz_takers',$data);
}
public function delete_results($ID = '')
{
$this->db->where('id',$ID);
return $this->db->delete('quiz_takers',$data);
}
}
I am still none the wiser in determining where to alter what is held in that $results variable, so that I can change the field from username to firstname.
I suspect it may have something to do with this:
return $query->result();
but where do I search for queries? In models/controllers - or somewhere else?
In codeigniter,
view is where you write the code to be seen on front-end.
model is where you write your DB queries.
controller is where you connect your view and model(front-end
with DB) ie it works as an intermediate. Also, the routes are
generated as your controller-name and then your function-name plus any additional parameter you provided to that function.
So, if your route(URL) is
your-website/controller_name/function_name/additional-parameter
you need to look at the function {function_name} in your controller {Controller_name}.
Sometimes you might see that the controller or the function is not present in the location as it should(from the URL) then you need to check if any route is provided for that particular URL which will be available at application->config->routes.php
Say if your URL is
www.site/xyz
$route['xyz'] = 'controller_name/function_name'; // route set in routes.php
You need to look for function {function_name} in your controller {Controller_name}.
You need not worry about the model or view as they're called and loaded in the controller itself.
Now, about $results,
it is the key provided to the array variable in the controller which acts as variable in the view. See example -
Controller
$data['xyz'] = 'some-value'; // or $whatever['xyz'];
$data['abc'] = 'any-value'; // ...
$this->load->view('some_folder/some_view', $data); // pass $data array or $whatever
View (located at view->some_folder->some_view.php)
echo $xyz; // output: some-value -- key {xyz} of $data becomes a variable
echo $abc; // output: any-value -- key {abc} of $data becomes a variable
For more details, you might wanna look here, here and here.
See if it helps you.

Activate default tabs based on condition

I have the following code a page that holds 3 tabs as follow:
<div id="tab-container">
<button class="tablink" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
<button class="tablink" onclick="openPage('Profile', this, '#F06078')">Profile</button>
<button class="tablink" onclick="openPage('Gallery', this, '#F06078')">Gallery</button>
The idea is to make whatever tab that's called on from a controller the default and land on it. I try passing $data to the page but it's telling me it's undefined. I also think maybe storing the info in a session and calling it on the page where the tabs are, but I'm not that flexible yet with the coding.
function index ($page='wall') {
$data['defaultOpen'] = 'wall';
$data['images_model'] = $this->images_model->get_images();
$this->load->view($page, $data);
}
I know the codes are not all there right now, but hopefully you get the idea. I will probably need to use an IF statement either in php/js and I was hoping someone might give me some feedback on that. Thanks in advance for all input.
controller :
public function index ($page = 'wall') {
$this->load->helper('url'); // only if you haven't load helper in autoload.
$data['images_model'] = $this->images_model->get_images();
$this->load->view('viewName', $data);
}
view :
<?php $active_tab = end($this->uri->segment_array());?>
<button class="tablink<?php echo ($active_tab == 'wall')? ' active':''; ?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
<button class="tablink<?php echo ($active_tab == 'profile')? ' active':''; ?>" onclick="openPage('Profile', this, '#F06078')">Profile</button>
<button class="tablink<?php echo ($active_tab == 'gallery')? ' active':''; ?>" onclick="openPage('Gallery', this, '#F06078')">Gallery</button>
The first method is to use parameters in the URL like http://localhost/example.com/home?tab=wall
Then use this URL parameter to active your tab
$tab = $this->input->get('tab'); //wall
<button class="tablink <?php if($tab == 'wall'){ echo 'active';}?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
Apply same for other tabs as well
The second method is to pas variable to view. Use variable not a parameter in a function
function index() {
$data['defaultOpen'] = 'wall';
$data['images_model'] = $this->images_model->get_images();
$this->load->view($page, $data);
}
Use $defaultOpen like this
<button class="tablink <?php if($defaultOpen == 'wall'){ echo 'active';}?>" onclick="openPage('Wall', this, '#F06078')" id="defaultOpen">Wall</button>
Try something like this
// controller
public function index ($page = 'wall') {
// pass the selected data to view
$data['selectedPage'] = $page;
$data['images_model'] = $this->images_model->get_images();
$this->load->view('viewName', $data);
}
// view
// use ternary operator to set active class to tab
// example:
// <?php echo $selectedPage== 'wall' ? 'active' : '' ?>
// code above means if $selectedPage equals 'wall', set class to active,
// if not do nothing
<button class="tablink <?php echo $selectedPage== 'wall' ? 'active' : '' ?>"
onclick="openPage('Wall', this, '#F06078')"id="defaultOpen">Wall</button>
<button class="tablink <?php echo $selectedPage== 'profile' ? 'active' : '' ?>"
onclick="openPage('Profile', this,
'#F06078')">Profile</button>
<button class="tablink <?php echo $selectedPage== 'gallery' ? 'active' : '' ?>"
onclick="openPage('Gallery', this,
'#F06078')">Gallery</button>

Two dropdown lists in 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();

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);
}
?>

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