ERROR Message: Trying to get property of non-object in CODEIGNITER - php

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

Related

How can I pass a jQuery variable to PHP?

I am trying to dynamically change the options of the second selector by what the user choses in the previous one. But it only gives a syntax error.
</script>
<select name="budova" id="idBudova">
<option value="" selected="selected">Vyberte...</option>
#foreach($budovy as $budova)
<option>{{$budova->nazov}}</option>
#endforeach
</select><br>
<script>
$("#idBudova").change(function () {
if ($('#idBudova').val() >= 1) {
$('#miestnost').show();
<?php $miestnosti = Miestnosti::where("id_budovy", "=", $('#idBudova').val())->get(); ?>
} else { //here is the mistake
//how to make php see the variable
$('#miestnost').hide();
}
});
</script>
<div hidden id="miestnost">
<select name="miestnost" id="idMiestnost">
<option value="" selected="selected">Vyberte...</option>
#foreach($miestnosti as $miestnost)
<option>{{$miestnost->nazov}}</option>
#endforeach
</select></div>```

Country state concept in codeigniter

This is My View Page
<select name="category" value="c_id" onChange="getsubcategorydetails(this.value)" style="display:">
<option value="" selected="selected" >Select Categories</option>
<?php if(isset($categories)) : foreach($categories as $row) :?>
<option value=<?php echo $row->c_id; ?>><?php echo $row->c_name; ?></option>
<?php endforeach;?>
<?php else :
endif;
?>
</select>
</div>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<tr>
<td>
<select name=" select id="subcategory">
<option value="" selected="selected" >Select Sub Category</option>
<?php foreach($sub_categories as $stt): ?>
<option value="<?php echo $stt->s_c_id; ?>"><?php echo $stt->s_c_name; ?></option>
<?php endforeach; ?>
</select>
</select></td>
</tr>
This is My Controller
public function getsubcategorydetails($c_id)
{
$c_id = $this->uri->segment(3);
// echo $c_id;
$data['sub_categories'] = $this->m_category->getsubcategorydetails($c_id);
//print_r($data['sub_category']);
$this->load->view('my_admin/newpost_1',$data);
}
This is My Model
function getsubcategorydetails($c_id='')
{
$this -> db -> select('*');
$this -> db -> where('c_id', $c_id);
$query = $this -> db -> get('subcategories');
return $query->result();
}
When i Click Category Select Box The Values returned from Controller ... But It Not Displayed On View ... What's Wrong With That .... Thanks In Advance
Here in my code,
iam posting categoryid through ajax in onchange event
and getting the subcategory for the categoryid from controller
and make the selection box in controller with the subcategory
and put the selection box in subcategory id.
Try this method.
View file
<select name="category" onChange="getsubcategorydetails(this.value)" >
<option value="" >Select Categories</option>
<?php if(isset($categories)) : foreach($categories as $row) :?>
<option value=<?php echo $row->c_id; ?>><?php echo $row->c_name; ?></option>
<?php endforeach;?>
<?php else :
endif;
?>
</select>
<select name="subcategory" id="subcategory">
<option value="" >Select Sub Category</option>
</select>
Controller
public function getsubcategorydetails($c_id)
{
$c_id = $this->input->post('c_id');
$sub_categories = $this->m_category->getsubcategorydetails($c_id);
$subcategory='<option value="" >Select Sub Category</option>';
foreach($sub_categories as $stt)
{
$subcategory.='<option value="'.$stt->s_c_id.'">'.$stt->s_c_name.'</option>';
}
echo $subcategory;
}
Model
function getsubcategorydetails($c_id='')
{
$this -> db -> select('*');
$this -> db -> where('c_id', $c_id);
$query = $this -> db -> get('subcategories');
return $query->result();
}
Script
function getsubcategorydetails(c_id)
{
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>yourcontroller/yourfunction",
data: {c_id:c_id},
success: function(data) {
$('#subcategory').html(data);
}
});
}
I suggest you to use JQuery AJAX to fetch the subcategory details.
Currently you are following wrong coding methods like calling a codeigniter controller function on an "OnChange" event with getsubcategorydetails($c_id) which is not right and makes it a wrong practice. OnChange event is used to call only Javascript functions.
To manipulate DOM, use Javascript/JQuery for it. Use the controller as a relay for getting appropriate formatted data into your JQuery.
Better option is Fetch data using AJAX and Append that to dependent select box onChange Event.

array prints result in controller but not giving result in view ,codeigniter

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.

how to fetch data after clicking on dropdown box

hi i m new in codeigniter plz help me. In my db there is one table name car details.i am fetching city field in drop down box so its work well.now after clicking on specific city i have to fetch all the data so how its possible.plz help me
model file
function getCity()
{
$this->load->database();
$query = $this->db->query('select DISTINCT mco_registercity from cardetailso');
if($query->num_rows() > 0)
{
return $query->result();
}
return array();
contoller file
$data['cities'] = $this->car_model->get_city();
view file
<?php foreach($city as $each){ ?>
<option value="<?php echo $each->mco_registercity; ?>">
<?php echo $each->mco_registercity; ?></option>';
<?php } ?>
//in view file
<select name="something" id="something" onchange="getCityDetails(this.value);">
<?php foreach($city as $each){ ?>
<option value="<?php echo $each->mco_registercity; ?>">
<?php echo $each->mco_registercity; ?></option>';
<?php } ?>
</select>
//in JAVASCRIPT SECTION
function getCityDetails(city)
{
//make ajax call with the city value
}
Hope it will help you. Please let me know if you need any further help.

Pass a PHP Variable into HTML Select "Value"

I am trying to pass some php via a dropdown in html , the php then needs to be used to aid the execution of more php. However I do not think it is working. Any suggestions would really be appreciated, its been bugging me a while. This is not the first form to use post on this page, the dropdown is populated after a search has been performed using another previous form.
HTML:
<form method="post" name="results">
<select class="form-control textinput">
<option value="<?php $response->body->results[0]?>"><?php echo $response->body->results[0]->name; ?></option>
<option value="<?php $response->body->results[1]?>"><?php echo $response->body->results[1]->name; ?></option>
<option value="<?php $response->body->results[2]?>"><?php echo $response->body->results[2]->name; ?></option>
<option value="<?php $response->body->results[3]?>"><?php echo $response->body->results[3]->name; ?></option>
<option value="<?php $response->body->results[4]?>"><?php echo $response->body->results[4]->name; ?></option>
</select>
<button type="submit" class="btn btn-primary" style="margin-left: auto; margin-right: auto; text-align: center;">Go</button>
</form>
PHP:
<?php
//Results from dropdown are put into selection
$selection = $_POST["results"];
//Selection is put into result var, should look like $response->body->results[x]
$result = $selection;
// IF Statement to only print result if the api call is successfull
if ($response->code == 200) {
if ($result->name == null) {
$printthis = "{$gametitle} returned no results, try and enter the full and accurate name";
}
else {
$printthis = "{$result->name} has a score of {$result->score} on {$result->platform}";
}
}
?>
First, you missed the 'echo' statement, and you also can't put an entire object into a value. If
$response->body->results[0]
has an id, thats what you should be using
something like this
<?php echo $response->body->results[0]->id ?>
I would do this with a loop.
for($i = 0; $i < count($response->body); $i++) {
echo '<option value="'.$i.'"><'.$response->body->results[$i]->name.'</option>';
}

Categories