Combining Functions - php

How can put return tow function following in one function marge() and echo it with json_encode.
function get_gr()
{
//$tourf_id = $this->input->post('toname');
$id = '102';
$query = $this->db->order_by('id','desc')->get_where('table1', array('id' => $id));
if ($query->num_rows() == 0) {
return 0;
} else {
$query = $query->row();
return array('guide' => $query->guide);
}
}
function get_res()
{
//$id = $this->input->post('name');
$id = '102';
$data = array();
$query_r = $this->db->order_by('id','desc')->get_where('table2', array('relation' => $id));
if($query_r->num_rows() > 0){
foreach ($query_r->result() as $row) {
$data[] = array(
'name_re' => $row->name_re,
'id' => $row->id
);
}
return $hotel_data;
}else{
return 0;
}
}
function marge(){
echo json_encode(get_gr().get_res()); //This line 991
}
I get this erroe from above php code:
Fatal error: Call to undefined function get_gr() in D:\xampp\htdocsapplication\controllers\faile.php on line 991
What do i do?

If inside a controller, you must refer to get_gr() as $this->get_gr()

You can do a merge as two array elements.
echo json_encode(array(get_gr(), get_res()));

Related

How to pass values from a assigned variable to view?

I wanted to pass some values to the view.
The $result may be 0 or 1 or '' and I tried to use this below code:
public function whatwedo($result='')
{
$result = array();
$result['status'] = $result;
$this->load->view('admin/whatwedo',$result);
}
public function add_whatwedo()
{
//$this->load->library('form_validation');
$this->form_validation->set_rules('text-input','Title','required');
if($this->form_validation->run() != true)
{
$result = 0;
$this->whatwedo($result);
}
else
{
$this->load->model('admin_model');
$result = $this->admin_model->ins_whatwedo($this->input->post());
//print_r($result);exit();
$this->whatwedo($result);
}
}
And in the view:
<?php
print_r($status);
?>
But, the $status is Array ( )
The problem is this line:
$result = array();
Because now the $result variable is an empty array so when you create the index status on result you assign it an empty array.
To fix you can do something like:
public function whatwedo($input = '')
{
$result['status'] = $input;
$this->load->view('admin/whatwedo', $result);
}
or even...
public function whatwedo($input = '')
{
$this->load->view('admin/whatwedo', ["status" => $input]);
}

How to access array values in laravel

This method i am using for storing currentTime in database using ajax call and its working
public function saveTime(Request $request, $lession_id){
$user = Auth::user();
if($user === null){
return response()->json(['message' => 'User not authenticated', 404]);
}
$video = \App\Lession::where('id',$lession_id)->first();
$lesson_id = $request->lession_id;
$progress = $request->time;
//save them somewhere
$watch_history = $user->watch_history;
$watch_history_array = array();
if ($watch_history == '') {
array_push($watch_history_array, array('lession_id' => $lesson_id, 'progress' => $progress));
} else {
$founder = false;
$watch_history_array = json_decode($watch_history, true);
for ($i = 0; $i < count($watch_history_array); $i++) {
$watch_history_for_each_lesson = $watch_history_array[$i];
if ($watch_history_for_each_lesson['lession_id'] == $lesson_id) {
$watch_history_for_each_lesson['progress'] = $progress;
$watch_history_array[$i]['progress'] = $progress;
$founder = true;
}
}
if (!$founder) {
array_push($watch_history_array, array('lession_id' => $lesson_id, 'progress' => $progress));
}
}
$data['watch_history'] = json_encode($watch_history_array);
$check = User::where('id',$user->id)->update(['watch_history' => $watch_history_array]);
return response()->json(['message' => 'Time saved', 200]);//send http response as json back to the ajax call
}
But when I use this another method to get the time back for playback it's getting the array inside an array
public function getTime(Request $request, $lession_id){
$user = Auth::user();
if($user === null){
return response()->json(['message' => 'User not authenticated', 403]);
}
$user_video = User::where('id',$user->id)->first();
$array = $user_video->watch_history;
foreach ($array as $key => $value) {
echo $check;
}
}
My current Output in a database is as follows
[{"lession_id":"157","progress":"71.449464"},{"lession_id":"156","progress":"92.113123"}]
So help me to get the values out of that for each lession id

how to convert array value to string and save to database

I have trouble, how to save array value in database, Each time I enter data only the last data stored in the database.
here is my code :
Controller :
function tambah_rm($id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Tambah Detail Rekam Medis';
$this->form_validation->set_rules('tgl_berobat', 'tgl_berobat', 'required');
$this->form_validation->set_rules('anamnesa', 'anamnesa', 'required');
$this->form_validation->set_rules('diagnosa', 'diagnosa', 'required');
$this->form_validation->set_rules('therapi', 'therapi', 'required');
$this->form_validation->set_rules('keterangan', 'keterangan', 'required');
if ($this->form_validation->run() === FALSE)
{
$data['obat']=$this->m_pasien->get_obat();
$data['header']=$this->m_pasien->header_pasien($id)->row_array();
$this->template->load('template','v_tambahRM',$data);
}
else
{
$this->m_pasien->tambahrm($data);
redirect('pasien');
}
}
My Model :
function tambahrm()
{
$this->load->helper('url');
$username = trim($this->session->userdata('id_user'));
$data = array(
'tgl_berobat' => $this->input->post('tgl_berobat'),
'anamnesa' => $this->input->post('anamnesa'),
'diagnosa' => $this->input->post('diagnosa'),
'therapi' => $this->input->post('therapi'),
'keterangan' => $this->input->post('keterangan'),
'id_user' => $username,
'id_pasien' => $this->input->post('id_pasien'),
);
if ($id == 0) {
return $this->db->insert('tbl_riwayat', $data);
} else {
$this->db->where('id', $id);
return $this->db->update('tbl_riwayat', $data);
}
}
function get_obat()
{
$data_obat = array();
$this->db->select('nama_obat');
$this->db->from('tbl_obat');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$data_obat[] = $row;
}
}
return $data_obat;
}
View
<div class="form-group m-b-20">
<div class="col-xs-12">
<label for="therapi">Therapi</label>
<select class=" form-control js-example-basic-multiple" multiple="multiple" name="diagnosa">
<?php
foreach($obat as $option)
{
?>
<option value="<?=$option['nama_obat']?>"><?=$option['nama_obat']?></option>
<?php
}
?>
</select>
</div>
</div>
And this is a screenshoot of data stored in the database, which I put yellow color is the last data I input in multiple select field.
Php have multiple option to convert array into string some of the option you can try are listed below.
implode() /explode()
json_encode() / json_decode()
serialize() / unserialize()
Above answer telling you how to convert array into serialized . So i only mention other two option in my answer.
to convert the array into a string and back:
#Option one
function tambahrm()
{
$this->load->helper('url');
$username = trim($this->session->userdata('id_user'));
$data = array(
'tgl_berobat' => $this->input->post('tgl_berobat'),
'anamnesa' => $this->input->post('anamnesa'),
'diagnosa' => json_encode($this->input->post('diagnosa')),
'therapi' => $this->input->post('therapi'),
'keterangan' => $this->input->post('keterangan'),
'id_user' => $username,
'id_pasien' => $this->input->post('id_pasien'),
);
if ($id == 0) {
return $this->db->insert('tbl_riwayat', $data);
} else {
$this->db->where('id', $id);
return $this->db->update('tbl_riwayat', $data);
}
}
function get_obat()
{
$data_obat = array();
$this->db->select('nama_obat');
$this->db->from('tbl_obat');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$row['diagnosa'] = json_decode($row['diagnosa'],true);
$data_obat[] = $row;
}
}
return $data_obat;
}
#option two
function tambahrm()
{
$this->load->helper('url');
$username = trim($this->session->userdata('id_user'));
$data = array(
'tgl_berobat' => $this->input->post('tgl_berobat'),
'anamnesa' => $this->input->post('anamnesa'),
'diagnosa' => implode(",",$this->input->post('diagnosa')),
'therapi' => $this->input->post('therapi'),
'keterangan' => $this->input->post('keterangan'),
'id_user' => $username,
'id_pasien' => $this->input->post('id_pasien'),
);
if ($id == 0) {
return $this->db->insert('tbl_riwayat', $data);
} else {
$this->db->where('id', $id);
return $this->db->update('tbl_riwayat', $data);
}
}
function get_obat()
{
$data_obat = array();
$this->db->select('nama_obat');
$this->db->from('tbl_obat');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$row['diagnosa'] = explode(",",$row['diagnosa']);
$data_obat[] = $row;
}
}
return $data_obat;
}
First option using explode and implode and second option is using json_encode and decode.
you can use any option according to your requirement. But i personally suggest you json encode or serialized pick from this two as they do not change key index of any type of array .
You can use
json_encode() / json_decode()
serialize() / unserialize()
to convert the array into a serialized string and back:
function tambahrm()
{
$this->load->helper('url');
$username = trim($this->session->userdata('id_user'));
$data = array(
'tgl_berobat' => $this->input->post('tgl_berobat'),
'anamnesa' => $this->input->post('anamnesa'),
'diagnosa' => json_encode($this->input->post('diagnosa')),
'therapi' => $this->input->post('therapi'),
'keterangan' => $this->input->post('keterangan'),
'id_user' => $username,
'id_pasien' => $this->input->post('id_pasien'),
);
if ($id == 0) {
return $this->db->insert('tbl_riwayat', $data);
} else {
$this->db->where('id', $id);
return $this->db->update('tbl_riwayat', $data);
}
}
function get_obat()
{
$data_obat = array();
$this->db->select('nama_obat');
$this->db->from('tbl_obat');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$row['diagnosa'] = json_decode($row['diagnosa']) ?: null;
$data_obat[] = $row;
}
}
return $data_obat;
}
For reference, see:
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.serialize.php
http://php.net/manual/en/function.unserialize.php

Joomla JTable Load Array and edit fields

I see that one can use an array with Jtable load, I tried the following:
public function delete($id = null) {
$app = JFactory::getApplication();
$id = $id ? $id : $app->input->get('files', array(), 'ARRAY');
$file = JTable::getInstance('Document','Table');
$file->load($id);
$file->date_removed = date("Y-m-d H:i:s",time());
if($file->store())
{
return true;
} else {
return false;
}
}
print_r($id) is:
Array
(
[0] => 1
[1] => 2
)
But I am having no luck. I keep getting the following error:
0 - Missing field in database: TableDocument   1.
Documentation on JTable
Any Help Greatly Appreciated.
Well actually you doing it wrong. You can load JTable with few field values, but it only returns 1 result;
Example use:
$data = array('id' => 100, 'user_id' => 101);
$table->load($data);
The code above will search for table entry with id = 100 AND user_id = 101. You cannot load 2 table entries like that.
My suggestion would be:
public function delete($id = null) {
$ids = array();
$return = true;
if ($id) {
$ids[] = $id;
} else {
$ids = JFactory::getApplication()->input->get('files', array(), 'ARRAY');
}
if (count($ids) > 0) {
foreach ($ids as $id) {
$file = JTable::getInstance('Document','Table');
$file->load($id);
$file->date_removed = date("Y-m-d H:i:s",time());
$temp = $file->store();
$return = $return || $temp;
}
}
return $return;
}

Codeigniter result array return only one row

Am trying to encode an array to json for use with jquery.
This is the function from my model
function get_latest_pheeds() {
$this->load->helper('date');
$time = time();
$q = $this->db->select("user_id,pheed_id,pheed,datetime,COUNT(pheed_comments.comment_id) as comments")
->from('pheeds')
->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left')
->group_by('pheed_id')
->order_by('datetime','desc')
->limit(30);
$rows = $q->get();
foreach($rows->result_array() as $row) {
$data['user_id'] = $row['user_id'];
$data['pheed_id'] = $row['pheed_id'];
$data['pheed'] = $row['pheed'];
$data['comments'] = $row['comments'];
$data['datetime'] = timespan($row['datetime'],$time);
}
return $data;
}
And this is from my controller
function latest_pheeds() {
if($this->isLogged() == true) {
$this->load->model('pheed_model');
$data = $this->pheed_model->get_latest_pheeds();
echo json_encode($data);
return false;
}
}
It returns only 1 row from the database when I run the code in the browser.
Please help me out
You are overwriting data in every iteration !!
Use something like
$data[] = array(
'user_id' => $row['user_id'];
'pheed_id' => $row['pheed_id'];
'pheed' => $row['pheed'];
'comments' => $row['comments'];
'datetime' => timespan($row['datetime'],$time);
) ;
This is good but your syntax should be 'user_id' => $row['user_id'], for each element of the array

Categories