How use condition in codeigniter form_dropdown? - codeigniter

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

Related

Code Igniter:Navigation(View) to Model:Severity: Notice Message: Undefined property:

UPDATE:
$ci =&get_instance();
$ci->load->model(your model);
$ci->(your model)->(your function);
Note: You have to call your model in your controller.Its working fine
Using above answer from (access model from view in codeigniter?)
My above code should work. But why it does not.
I am loading my menu in navigation using database. Im connecting directly from my model. In my navigation I have
$CI =& get_instance();
$CI->load->model('masterdata/MasterDataRole_','masterdatarole');
$menu = $CI->masterdatarole->loadMenu();
In my masterdata/MasterDataRole_ I have
<?php
class MasterDataRole_ extends CI_Model{
//menu
//submenu
//screen
//check access rights
public function hasAccess($page_id,$level){
$query = $this->db->select('RoleAccess')
->from('masterdatarolemapping')
->where('ItemLevel',$level)
->where('ItemSysID',$page_id)
->where('MasterDataRoleID',$this->session->userdata('UserID'))
->get();
if($query){
if($query->num_rows() > 0){
$data = $query->row();
return $data->access;
}
}
}
}
But I get error saying
A PHP Error was encountered
Severity: Notice
Message: Undefined property: MasterDataRole::$menu
Filename: templates/navigation.php
Line Number: 117
In my Navigation I have:
<?php foreach ($menu as $m): echo $m;?>
<?php if($CI->menu->hasAccess($m->SysID,'menu') == 'yes'): ?>
<?php endif; ?>
<?php endforeach; ?>
Any Idea is appreciated.
UPDATE
error is on this <?php if($CI->menu->hasAccess($m->SysID,'menu') == 'yes'): ?>
this line <?php foreach ($menu as $m): echo $m;?> give the list of menu
$CI->load->model('masterdata/MasterDataRole_','masterdatarole');
$menu = $CI->masterdatarole->loadMenu();
Should be masterdatarole not menu

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!

redirect issue in codeigniter when using if statement

i have been trying to redirect the same previous page after delete or inserting the data using if condition in the controller to flash the message but there is something i am missing.
<form action="<?php echo base_url(); ?>curdler/add/tbl_category/addCat/category" method="post">
Category Title<input type="text" name="category_title"/> </br>
<input type ="submit" value="submit">
</form>
<?php echo $this->session->flashdata('msg'); ?>
Controller
public function add() {
$data = $_POST;
$tableName = $this->uri->segment(3);
$content = $this->uri->segment(4);
$folderName = $this->uri->segment(5);
$this->load->model('curdmodel');
if($this->curdmodel->add($data, $tableName)){
$this->session->set_flashdata('msg', 'Category added');
redirect('welcome/index/'.$content.'/'.$folderName);
} else{
$this->session->set_flashdata('msg', 'Category Not Added');
}
}
when using the if statement it goes to the different url but without if statement its working fine.
model
public function add($data, $tableName) {
$this->db->insert($tableName, $data);
}
Your redirect only occurs if the if statement evaluates to be true, if the condition is false codeigniter is just setting the flashdata and then ending the script.
Consider changing the order of your code to something like
if($this->curdmodel->add($data, $tableName)){
$this->session->set_flashdata('msg', 'Category added');
} else{
$this->session->set_flashdata('msg', 'Category Not Added');
}
redirect('welcome/index/'.$content.'/'.$folderName);
Additionally, your model does not contain any return value and therefore will never pass data back for the if statement to be evaluated. You should update your model as follows:
public function add($data, $tableName) {
$this->db->insert($tableName, $data);
if($this->db->affected_rows() > 0) {
return true;
}
}

CodeIgniter - I can't update row in my table

I can't find the correct way update one row in my table.
My view:
...
<?php echo form_open('ImenikController/verify_editing_phonebook/'.$this->uri->segment(3)); ?>
Ime i prezime*:
<input type='text' name='ime_prezime' value=""> <br><br>
Ulica i broj: <input type='text' name='ulica' value=""> <br><br>
Mesto: <input type='text' name='mesto' value=""> <br><br>
Telefon*: <input type='text' name='telefon' value=""> <br><br>
<u>Napomena: Polja sa zvezdicom su obavezna.</u> <br /> <br />
<input background:url('images/login-btn.png') no-repeat; border: none;
width='103' height='42' style='margin-left:90px;' type='submit' value='Izmeni'>
<?php echo form_close(); ?>
...
My Controller:
function verify_editing_phonebook()
{
if ($this->session->userdata('logged_in'))
{
if ($this->session->userdata('admin') == 1)
{
$this->form_validation->set_rules('ime_prezime', 'Ime i prezime', 'trim|required|xss_clean');
$this->form_validation->set_rules('telefon', 'Telefon', 'trim|required|xss_clean');
if ($this->form_validation->run() == TRUE)
{
$id = $this->uri->segment(3);
if (isset($id) and $id > 0)
{
$this->load->model('LoginModel');
$this->LoginModel->edit_phonebook($id);
redirect(site_url().'ImenikController/', 'refresh');
}
}
else {
$temp = $this->session->userdata('logged_in');
$obj['id'] = $temp['id'];
$data['records'] = $this->LoginModel->get_Username($obj);
$this->load->view('ErrorEditing', $data);
}
}
else {
$this->load->view('restricted_admin');
}
}
else {
$this->load->view('restricted');
}
}
My Model:
function edit_phonebook($id)
{
$data = array ('ime_prezime' => $this->input->post('ime_prezime'),
'ulica' => $this->input->post('ulica'),
'mesto' => $this->input->post('mesto'),
'telefon' => $this->input->post('telefon'));
$this->db->where('id', $id);
$this->db->update('pregled', $data);
}
That solution doesn't work.
I get the url: localhost/imenik114/ImenikController/verify_editing_phonebook
It is a blank (white) page. And not editing row in table.
Basic Debugging Strategies
(1) Have you created all the view files?
(2) Have you tested edit_phonebook($id) independently?
(3) What does redirect(site_url().'ImenikController/', 'refresh'); display?
Did you define the index function for ImenikController?
(4) What URL did you use when you say 'That solution doesn't work.' ?
(5) If your URL is: "localhost/imenik114/ImenikController/verify_editing_phonebook"
you did not type in id in your 3rd segment
(6) If you are not logged in, do you see the correct restricted view?
(7) If you are logged in and NOT admin, do you see the correct restricted_admin view?
Potential Bug
Looking at this part of your code:
if ($this->form_validation->run() == TRUE)
{
$id = $this->uri->segment(3);
if (isset($id) and $id > 0)
{
$this->load->model('LoginModel');
$this->LoginModel->edit_phonebook($id);
redirect(site_url().'ImenikController/', 'refresh');
}
// You need to handle the case of $id not set
else
{
// load a view with error page saying $id is missing...
}
}
if your form validates, and you don't pass in segment(3), your controller will not load a view, therefore, you will get a blank page.
You need to check the case of $id not present, see code.
Code Fix
One more detail: the statement $id = $this->uri->segment(3); will set $id either to the id number or FALSE, therefore, you don't need isset($id) in your if statement. I would write $id = $this->uri->segment(3,0); to set the default to 0 instead of FALSE to keep the logic a bit clearer.
Thanks for answer but I solved my problem somehow.
I made a link in my view:
Edit
And in view for editing:
<?php echo form_open('Controller/verify_editing_phonebook/'.$this->uri->segment(3)); ?>
Function verify_editing_phonebook passed trough validation and loading view.
Thanks once again and sorry for my English...

Codeigniter Message: Undefined variable: infos

I am a newbie in CI. I used MY_Controller.php as main controller. I could open the ajax as the div#page loader. Now , My problem is although I load /about page, I get the database entries for the services model. How can i get the about table for the about controller ?
..
function render_page($view) {
if( ! $this->input->is_ajax_request() )
{
$this->load->view('templates/header', $this->data);
}
$this->load->view($view, $this->data);
if( ! $this->input->is_ajax_request() )
{
$this->load->view('templates/menu');
$this->load->view('templates/footer', $this->data);
}
}..
My services_model:
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
My home controller :
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
When I use them in the home view there is no problem I can see the services_table :
<ul class="blog-medium">
<?php foreach($records as $row): ?>
<li>
<div class="blog-medium-text">
<h1><?php echo $row->title; ?></h1>
<p class="blog-medium-excerpt"><?php echo $row->content; ?><br />
Devamını Okumak için →</p>
</div>
<?php endforeach ?>
I want to use the same way in about page.
About_model:
class About_model extends CI_Model {
function getAll() {
$q = $this->db->get('abouts');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
About controller :
public function view($page = 'about')
{
$this->load->helper('text');
$this->data['records']= $this->about_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
And this is my view file of about :
<div id="content">
<?php foreach($infos as $row): ?>
<h3 style="text-align: center;"> <?php echo $row->title; ?></h3>
<div class="hr"> </div>
<?php echo $row->content; ?>
<?php endforeach; ?>
I get the error telling me :
Severity: Notice
Message: Undefined variable: infos
Filename: pages/about.php
Line Number: 3
Why cant i get the abouts table?
You are calling a variable $infos in your foreach, but it is never passed in as a variable to your view.
Read the docs about Adding Dynamic Data to the View
You will either need to set $data['infos'] to something or, and I'm guessing this what you intended, use $records in your foreach
The above answers your specific, but after you provided the repo for your source, there are issues you are struggling with. I highly suggest you read through the entire documentation, staring with the Introduction: Getting Started, continuing to the Tutorials and then General Topics.
The reason you are having problems here, you have your routes.php setup to that everything is routed into the Home controller executing the view method. This method, while it accepts the page you want to see is always returning a fetch of the services model. Your other controllers are not getting executed at all. Based on your controller setup, if you would just remove the custom route, http://theurl/about would route to the About Controller. The default method to be loaded is index so if you change view to index, then it would be displayed by default.

Resources