Displaying Data from a Join in Codeigniter 2.2.1 - inner-join

model
function select_nip($data){
$query = $this->db->select('mx_pegawai.nama, mx_pegawai.nip, mx_jabatan.unit_kerja AS unit')
->from('mx_pegawai')
->join('mx_jabatan', 'mx_pegawai.nip = mx_jabatan.nip')
->where('mx_pegawai.nip', $data)
->get();
return $query;
}
view
$i=1;
foreach($hasil->result() as $row){
echo '
<tr>
<td>'.$i.'</td>
<td>'.$row->nama.'</td>
<td>'.$row->nip.'</td>
<td>'.$row->unit.'</td>
</tr>
';
$i++;
}
Error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$unit
Filename: home/cari.php
Line Number: 50

$this->db->select() accepts an optional second parameter.
If you set it to FALSE, CodeIgniter will escaping your field. This is useful if you need a compound select statement where automatic escaping of fields may break them.
$query = $this->db->select('mx_pegawai.nama, mx_pegawai.nip, mx_jabatan.unit_kerja AS unit')
->from('mx_pegawai')
->join('mx_jabatan', 'mx_pegawai.nip = mx_jabatan.nip')
->where('mx_pegawai.nip', $data)
->get();
return $query;

Related

Return all array values with ACF fields and foreach loop

I created a checkbox field in ACF and want to loop this field again and again and show all these values. First I tried it the way the ACF documentation shows, but the following code results in just the value of the first checked checkbox.
function ms_get_department(){
$departments = get_field('vakgebied');
if($departments):
foreach($departments as $department):
echo '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
endif;
}
I also tried to store all the values inside an array but in the code below it just shows 'Array' and don't know how to show all these data in this case.
function ms_get_department(){
$departments = get_field('vakgebied');
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Does anyone know how I can solve this problem in a proper way?
It's not clear where you're adding this function. If it's on the single page then the code should work; however, if it's on any other page then you'd need to pass the post ID to ACF Field.
function ms_get_department(){
$departments = get_field('vakgebied', 123); // 123 being the post ID
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Another thing to check is, make sure ACF field is returning both 'label' and 'value'.

CodeIgniter : Undefined index in model variable

I have the below code in of of my models. Im loading the offer details page and it gave me the error
Severity: Notice
Message: Undefined index: viewc
Filename: statistics/Statistic_model.php
Line Number: 551
Statistic_model:
public function getTotalDetailsViewOfActiveOffer($comid) {
$this->db->select('count(statistic_offer_view_count.id) as viewc');
$this->db->from('statistic_offer_view_count');
$this->db->join('offers', 'offers.id = statistic_offer_view_count.offer_id');
$this->db->where('offers.com_id',$comid);
$this->db->where('offers.approved_status',"2");
$this->db->where('offers.enddate >=',date('Y-m-d'));
$this->db->group_by("offers.com_id");
$query = $this->db->get();
$row = $query->result_array();
$count=$row['viewc']; //this is line 551
}
Any idea how to fix this error?
That error means that the index viewc is not in the array $query->result_array() most likely because your query failed or returned null. You should get into the habit of doing:
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row()->viewc;
}
return false; // or in this case 0 or whatever
In Your Code You Selecting data from database Multiple array's.And assigning at to a variable like a single array.
Your Code....
$row = $query->result_array();
$count=$row['viewc'];
correct way is in your conduction to assign a variable is....
$data = array();
foreah($row as $key => $value){
$data[$key] = $value['viewc'];
}

Cakephp 3.6 Helper that should return model is cast to array in template

Helper:
class QuestionHelper extends Helper
{
protected $_defaultConfig = [];
public function mostRecentAnswer(Question $q): ?Answer
{
$result = (new Collection($q->answers))->max(function($a) {
return $a->created;
});
if ($result) {
return $result;
}
return null;
}
}
I want to use it in a template to display the date, however calling it using property access notation causes an error:
Call: <td><?= h($this->Question->mostRecentAnswer($question)->created) ?></td>
Error: Notice (8): Trying to get property 'created' of non-object [APP/Template\Questions\index.ctp, line 24]
However, wrapping the returned object in a dd shows:
Call: dd(get_class($this->Question->mostRecentAnswer($question)))
Result: "App\Model\Entity\Answer"
Using array access in the template works:
<td><?= h($this->Question->mostRecentAnswer($question)['created']) ?></td>
Is this normal behavior?
Turns out everything was working correctly. The Notice was due to another question, on a different line item, that didn't have any associated answers, and so was returning null. I simply added a wrapper on the helper:
public function mostRecentAnswerCreatedDate(Question $q): string
{
$result = $this->mostRecentAnswer($q);
if ($result) {
return $result->created;
}
return 'Not yet answered';
}
And call it
<td><?= h($this->Question->mostRecentAnswerCreatedDate($question)) ?></td>

Codeigniter Model: How to use values from a query for further calculations?

i have a function in a model with the following query, and i want to make some calculations with value1, value2 and value3 outside the query. how to get the values out of this result()?
function ($id,$username$) {
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where("username=$username and id = $id");
$query=$this->get();
$result = $query->result();
$result['$testvalue'] = (value1 * value2/113) + value3;
return $result; }
can someone help? i already found out how to use values from a table, but not from a query!
ok now i know how to use row_array.
i call the row_array in the controller
$data = $this->model_testmodel->testfunction($id, $username);
...
$this->load->view('pages/view_test', $data);
now i wanna know how the view would look like.
i tried many different ways.
in the moment i am stuck with ...
<?php foreach $data as $value: ?>
<label>Value1:</label>
<input type="text" name="value1" value="<?php echo $value['value1']; ?>">
<label>Value2:</label>
<input type="text" name="value2" value="<?php echo $value['value2']; ?>">
...
<?php endforeach; ?>
but i get two errors:
Message: Undefined variable: data
Message: Invalid argument supplied for foreach()
Try this...
function ($id,$username)
{
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where(array('id'=>$id,'username'=>$username));
$query=$this->db->get();
$result = $query->row();
$testvalue = (($result->value1 * $result->value2/113) + $result->value3);
//test here
echo $testvalue;
$res = array();
$res['testvalue'] = $testvalue;
$res['value1'] = $result->value1;
$res['value2'] = $result->value2;
$res['value3'] = $result->value3;
return $res;
}
It returns $res as array.Easy to execute of you got problem comment.. if it works accept.Lets enjoy..
Then make a function call and accept array in a variable..like this..
first load model then make function call.from your controller
$this->load->model('model_name');
$data= $this->model_name->function_name($id,$username);
//Fetch returned array like this..
echo $data['testvalue'];
echo $data['value1'];
//so on

codeigniter multiple table joins

function in codeigniter model is not working. I'm getting http error 500 because of below code in codeigniter model function. if I remove this code.. code is working fine.
function get_ad_by_user($a,$b)
{
$this->db->select('title,ci_categories.category,ci_subcategories.subcategory,state,city,locality,ad_website,description,phone_number,address,image_token1,image_token2');
$this->db->from('ci_pre_classifieds');
$this->db->join('ci_categories', 'ci_categories.category_id = ci_pre_classifieds.category');
$this->db->join('ci_subcategories', 'ci_subcategories.subcategory_id = ci_pre_classifieds.sub_category');
$this->db->where('ci_pre_classifieds.id',$a);
$this->db->where('ci_pre_classifieds.ad_string_token',$b);
$q=$this->db->get();
$this->db->last_query();
log_message('debug','******query'.$this->db->last_query().'********');
if($q->num_rows()==1)
{
foreach($q->result_array() as $row)
{
$data['title]=$row['title'];
$data['category']=$row['ci_categories.category'];
$data['sub_category']=$row['ci_subcategories.subcategory'];
$data['state']=$row['state'];
$data['locality']=$row['locality'];
$data['ad_website']=$row['ad_website'];
$data['description']=$row['description'];
$data['phone_number']=$row['phone_number'];
$data['address']=$row['address'];
}
}
else
{
$data['message']="sorry either classified expired or deleted";
}
return $data;
}
I'm getting error in for each block..if I remove everything working...How to copy $row array in $data array
$data['title]=$row['title']; has a ' missing.
This could be the reason but without that error message, it's hard to tell.
Also, you may want to reduce the function to the following:
function get_ad_by_user($a,$b)
{
$data = array();
$this->db->select('title,ci_categories.category,ci_subcategories.subcategory,state,city,locality,ad_website,description,phone_number,address,image_token1,image_token2');
$this->db->from('ci_pre_classifieds');
$this->db->join('ci_categories', 'ci_categories.category_id = ci_pre_classifieds.category');
$this->db->join('ci_subcategories', 'ci_subcategories.subcategory_id = ci_pre_classifieds.sub_category');
$this->db->where('ci_pre_classifieds.id',$a);
$this->db->where('ci_pre_classifieds.ad_string_token',$b);
$q=$this->db->get();
log_message('debug','******query'.$this->db->last_query().'********');
if($q->num_rows()==1)
{
$data[] = $q->row_array();
}
else
{
$data['message']="sorry either classified expired or deleted";
}
return $data;
}
If you are only returning one row in your query, you can simply use $q->row_array() or $q->row() to return the single row.
Just in case, I would also declare $data = array() at the start of your get_ad_by_user function, otherwise it "could" complain that $data does not exist.

Resources