I'm trying to pass data from controller to view page.. My query loaded from the model prints the result but when passed into view page, data not giving any result,instead it shows error:Message: Undefined index: Country_id .Why this really happens for.?
public function something()
{
$data['country']= $this->some_model->getCountry();
$this->load->view('another/location.php',$data);
//print_r($data['country']);
}
my view:
<div class="form-group">
<label for="" class="control-label col-xs-4">Country*</label>
<div class="col-md-6">
<select class="form-control" name="country" id="country">
<option value="">Select</option>
<?php foreach ($country as $count) { ?>
<option value="<?php echo $count["Country_id"]; ?>"><?php echo $count["Country"]; ?></option>
<?php } ?>
</select>
</div>
</div>
and model:
public function getCountry()
{
$this->db->select('*');
$this->db->from('tbl_country');
$query = $this->db->get();
return $query->result();
}
Print $country in view. Use echo ""; print_r($country);. I think you used foreach loop for data print and Country_id doesn't exist in database table column.So this error occured.
In CodeIgniter, when you pass data from controller to view.
You can access the passed data with keys.
In your example, you are passing $data['country'].
So, in view, you need to print echo '<pre>'print_r($country);echo '</pre>';
Please check it this was correct in your case.
Related
My CONTROLLER is named Posts,
public function addmore(){
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$Asystemtype=$this->postModel->fetchDoctype();
$data = [
'Asystemtype' => $Asystemtype,
'Adoctype' => trim($_POST['Adoctype']) ];
$this->view('posts/addmore', $data);
}
My MODEL is named Post,
I have a database table named systemtitles, with column named ID and titles.
public function fetchDoctype(){
$this->db->query('SELECT * FROM systemtitles');
$results = $this->db->resultSet();
return $results;
}
My VIEW is named addmore
<form action="<?php echo URLROOT; ?>/posts/addmore" method="post" >
<div>
<label for="Asystemtype">System Type: <sup>*</sup></label>
<select id="systype" name="Asystemtype" class="form-control form-control-lg" value="<?php echo $data['Adoctype']; ?>">
<?php foreach($data['Asystemtype'] as $Asystemtype) :?>
<option value = "<?php echo $Asystemtype->titles; ?>"> <?php echo $Asystemtype->titles; ?> </option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="Adoctype">Document Type: <sup>*</sup></label>
<select id="doctype" name="Adoctype" class="form-control form-control-lg" value="<?php echo $data['Adoctype']; ?>">
<option value="Select">Select Doc Type</option>
<option value="HRS">HRS </option>
<option value="HDD">HDD </option>
</select>
</div>
i tried to launch my website, These were the observations,
my SELECT box is simply blank nothing is getting populated, why?.
I printed var_dump from controller
var_dump($Asystemtype);
it was echoing all(that is in my database atleast i have five rows!).
Help needed for my select box to be populated from db for Asystemtype. My
Adoctype has no issues as I am populating directly from the form.
I found my mistake. What I have done is from my controller I was sending an empty data!! In my POST else statement had
else{ $Asystemtype=$this->postModel->fetchDoctype(); $data=[ 'Asystemtype' =>'', 'Adoctype' => ''];
$this->view('posts/addmore', $data);
}
that was a mistake. That is, if I did not do a post then it was having 'Asystemtype' =>'' as null value. Now I corrected to 'Asystemtype' => $Asystemtype
Thanks Alexander Dobernig for your valuable suggestions/comments
I have a function in my controller which is trying to fetch the value from view template and print the same through post method but something else is printing on there.
public function editProfile(){
$profile = $this->input->post('profile');
echo ("PROFILE:" . $profile);
}
This is my view template which is taking profile id from the application_info array stored in the database and matching it with the other profiles id so that the value of the particular id can be fetched and display out to the user-side using form.
$profile_id = $application_info[0]->profile_id;
<form role="form" action="<?php echo base_url() ?>Job_profiles/editProfile" method="post" id="editApplication" role="form">
<div class="form-group">
<label for="profile">Profile</label>
<select class="form-control" id="profile" name="profile">
<option value="0">Select profile</option>
<?php
if(!empty($profiles))
{
foreach ($profiles as $p)
{
?>
<option value="<?php echo $p->profile; ?>" <?php if($p->profile_id == $profile_id) {echo "selected=selected";} ?>><?php echo $p->profile ?></option>
<?php
}
}
?>
</select>
</div>
While printing the value of the variable inside the view template, everything is going fine. But when displaying the value through the controller, it prints nothing.
Thanks.
this controller controller code. $name contain array of values i have display name in dropdownlist
public function category()
{
$name =DB::select('select name from category');
$category = json_encode($name);
return view('Addworkout')->with('categoryname',$category);
}
this is my view page
<select id="select1" class="form-control select2-hidden-accessible" style="width: 100%" data-placeholder=" select category" tabindex="-1" aria-hidden="true">
<?php foreach($categoryname as $cate) { ?>
<option value="" disable selected><?php echo $cate->name; ?></option>
<?php } ?>
</select>
When you do
$category = json_encode($name);
you're serializing the results of the query into a string, which is indeed an invalid argument for a foreach loop.
Why are you doing so?
I'm trying to get data from a certain table where the member id from a dropdown lis value is equal to the id from the other table. And i'm using jquery ajax also.
Here's my controller transactions.php
function getdataload(){
$this->db->where('member',$this->input->post('member'));
echo $this->db->get('loading_service')->row()->member;
}
And here's is my view.php
<label for="member">Member</label>
<select class="form-control" id="member" name="member" required onchange="showCustomer(this.value)">
<option selected="" value="">--select--</option>
<?php foreach ($members as $row): ?>
<option value="<?php echo $row->mem_id; ?>"><?php echo ucwords($row->mem_fname.' '.$row->mem_lname) ?></option>
<?php endforeach ?>
</select>
<p class="help-block text-danger" style="margin-left: 600px; margin-top: -450px; color: red;"><span id="select_member"> </span></p>
<script>
$('#member').on('change',function(){
$.post('<?php echo base_url("transactions/getdataload")?>',
{
mem_id:$(this).val()
}).done(function(res)
{
$('#select_member').text(res);
});
});
</script>
Please help me with this guys:
The first thing to do is make sure you are getting results from your query. Try something like
$query = $this->db->get(...);
if ($query->num_rows() > 0) {
return json_encode($query->result());
} else {
return "No data found";
}
then handle the no data error gracefully in your ajax success code. I sort of guessed that you would want your query result returned as json.
Try:
model
function getData()
{
$this->db->select('field_name');
$this->db->from('loading_service');
$this->db->where('member',$this->input->post('member'));
$query=$this->db->get();
return $query->row();
}
Controller
function getdataload(){
$data['member'] = $this->model_name->getData();
$this->load->view("view_name",$data);
}
I have a code that fetches all the customers from my database. i fetch all the customers in my drop down. Now i want to select a customer and when i submit i want to create a key for the selected customer.
This is my Controller code:
public function index()
{
$id = $this->data['org'] = $this->key_m->get_organisation();
$this->data['key'] = $this->key_m->genrate_license();
$this->data['subview'] = 'admin/key/index';
$this->load->view('admin/_layout_main',$this->data);
}
The View:
<div class="col-sm-3">
<div class="col-md-12">
<div class="form-group">
<h3><label for="sel1"> Organization Name:</label></h3>
<select required name="org-list" id="org-list" class="form-control">
<option value="">Select</option>
<?php foreach($org as $value) { ?>
<option id="emp" value="<?php echo $value['org_name'];?>"><?php echo $value['org_name'];?></option>
<?php } ?>
</select>
<?php echo "<br>"; ?>
</div>
</div>
</div>
So i want to save the the id for the customer and the key generated in database. when i select any customer[company here] in the dropdown
Write your option as below:-
<option id="emp" value="<?php echo $value['org_name'].'|'.md5(uniqid(rand(), true));?>"><?php echo $value['org_name'];?></option>
I have used md5(uniqid(rand(), true)) for generating unique key.
And in your controller,
$test = explode('|', $_POST['org-list']);
echo $test[0] gives you value and $test[1] gives you key.
It will generate always different unique string depend on micro-time and random number.
<?php
// Here microtime will be unique everytime
echo substr(md5(microtime()*rand(0,9999)),0,20); // 20 is length of key
?>