I have 3 queries like this in my Controller function:
function getDataDetail(Request $request)
{
$id = $request->get('id');
$taskData['taskData'] = DB::select("SELECT NAME_, PRIORITY_, ASSIGNEE_, DUE_DATE_, START_TIME_, END_TIME_, PROC_DEF_ID_ FROM act_hi_taskinst WHERE PROC_INST_ID_ LIKE '%$id%'");
$varData['varData'] = DB::select("SELECT NAME_, TEXT_ FROM act_hi_varinst WHERE PROC_INST_ID_ LIKE '%$id%'");
$imagepath['imagepathData'] = DB::connection('mysql_2')->select("SELECT imagePath FROM images WHERE processInstanceId = $id");
if(count($taskData) > 0 && count($varData) > 0 && count($imagepath) > 0) {
return view('datatrackingdetail', $taskData, $varData, $imagepath);
} else {
return view('datatrackingdetail');
}
}
And I retrieve query value in my view by foreach loop. Problem is my code work fine with only 2 query variables in if function, if there are 3 query variables in my code and it's not work, error say:
"Undefined variable imagepathData" in my view.
How should I fix it? Thank you very much!
Try
DB::connection('mysql_2')->table('images')->select("imagePath")->where(['processInstanceId' => $id]);
Or you shoud use raw select in your query
Related
I'm trying to load all the data from table "main_tests" where main_tests.id = test_name_price.test_id and where test_name_price.list_id = 1
I'm trying to do that but there it's just saying wrong syntax I'm beginner to CodeIginter Thanks For Advance
I have looked a lot for the answer but I couldn't get what I need exactly or either their ways don't work with me
here is my code
$result = $this->db->select('test_name_price.*, main_tests.*')
->from('test_name_price')
->where('test_name_price.list_id', 1)
->join('main_tests',
'test_name_price.test_id = main_tests.id')
->get();
if (count($result) > 0) {
foreach ($result as $itemsl) {
echo "
<option value='$itemsl->testname' data-code='$itemsl-
>test_code'>$itemsl->test_id</option>
";
}
}
echo "</select>"
EDIT :
that's the code in my controller , that view is addpatient
public function addpatient()
{
$this->load->model("khmodel","Khmodel");
$this->load->view('addpatient', array('result_array' =>
$result_array));
}
and here is my view
<?php foreach ($result_array as $key => $value) : ?>
<option value='<?= $value['testno'] ?>' data-code='<?=
$value['testno'] ?>'><?= $value['test'] ?></option>
<?php endforeach; ?>
and here is the MasterPage function I'm using to load the view , tell me if I need to add anything else
public function MasterPage($view='',$table='',$da='',$spg='',$szpg='')
{
if (!empty($table)) {
$data['result']=$this->Khmodel->get($table,$da,$spg,$szpg,"id
desc");
$data['pages']=$this->Khmodel->pagesno($table,$szpg);
}
$data['sview']=$view;
$data['index']=($view == 'home') ? '' : 'iner_page';
$getlang =$this->uri->segment(1);//$this->input->cookie('shlang', TRUE);
if($getlang=='en'){
$this->lang->load('en', 'en');
$data['lang']='en';
$data['nav_align']='right';
$data['xnav_align']='left';
}
else{
$this->lang->load('ar', 'ar');
$data['lang']='ar';
$data['nav_align']='left';
$data['xnav_align']='right';
}
//$this->load->view('main/close');
$this->load->view('main/header',$data);
if($view!='home'){
$this->load->view('main/top_header',$data);
}
$this->load->view('main/'.$view);
$this->load->view('main/footer',$data);
}
Well this what's worked with me like a charm !
I have used #Sherif Salah First Part of code INSIDE THE VIEW WITHOUT CONTROLLER OR MODEL
and It worked actually !!
Thanks for your Help
$table_one = 'test_name_price';
$table_two = 'main_tests';
$this->db->select("$table_one.*,$table_two.*");
$this->db->join($table_two, "$table_two.id =
$table_one.test_id", 'left');
$this->db->from($table_one);
$this->db->where("$table_one.list_id =
1");
$query = $this->db->get();
$result_object = $query->result();
if (count($result_object) > 0) {
foreach ($result_object as $itemsl) {
echo "<option value='$itemsl->tsprice' data-code='$itemsl-
>testno'>$itemsl->test</option>";
}
}
Here is how to join tables in codeigniter, but make sure that there are now similar field names or you have to select it individually and rename the field by hand.
$table_one = 'table_one_name';
$table_two = 'table_two_name';
$this->db->select("
$table_one.*,
$table_two.*
");
$this->db->join($table_two, "$table_two.id = $table_one.table_two_id", 'left');
$this->db->from($table_one);
$query = $this->db->get();
$result_object = $query->result();
$result_array = $query->result_array();
Note: if your code above is correct, you have to get the result as $result->result() as an object or $result->result_array() as an array.
Edit: Here is a quick example about how to use this array in your view, the above code will return array of arrays and you can process this array as:
<?php foreach ($result_array as $key => value) : ?>
<option value='<?= $value['name'] ?>' data-code='<?= $value['code'] ?>'><?= $value['id'] ?></option>
<?php endforeach; ?>
Note: please keep your logic in your controller, just pass the result to the view.
Edit: Lets keep it simple for now, supposingly your query worked fine and you can check for this by var_dump($result) so now you need to pass this data to the view however you wanna do it but supposingly AGAIN you got you result in the controller and all works fine, now you just have to pass it to the view like this:
$data = array("result" => $result);
$this->load->view('your_view', $data);
and in your view just loop through this array using the variable result or whatever you named it and that's it.
IMHO.. you should work through codeigniter's documentation and follow any step by step tutorial in codeigniter cause your problem is not that you don't know how to join two table, its about how to use the framework itself.
function getjobid() {
global $geotag_table, $wpdb;
$qry = "SELECT * FROM mirdc_jo_form WHERE jo_id = (SELECT MAX(jo_id) FROM mirdc_jo_form)";
$desc = $wpdb->get_results($qry);
return $desc; }
can you help me guys.. i am trying to get the id from my table and try to show the data to my wordpress..
<?php foreach (getjobid() as $generatedid) {
echo $generatedid;?> #<------ this is the error
this is my wordpress code..
can you teach me how i convert the object to string.. to show the result to my wordpress.
Please try this is batter for your code
function getjobid() {
global $geotag_table, $wpdb;
$qry = "SELECT jo_id FROM mirdc_jo_form WHERE jo_id = (SELECT MAX(jo_id) FROM mirdc_jo_form)";
$desc = $wpdb->get_col($qry);
return $desc[0]; }
$lastid = getjobid();
$generatedid is an Object and containing entire row of data. You need to specify what column you want from that row.
You should try var_dump($generatedid) to view all the data.
foreach (getjobid() as $generatedid) {
echo $generatedid->jo_id; // or whatever ID you want from DB
}
I am trying to get a list of coupons through ajax when the checkboxes are selected. So everything else is working fine but the query is returning only the first match.
So my query is:
$this->db->from('tbl_coupons');
if($storeids !=''){
$ids = array($storeids);
$this->db->where_in('coupon_store', $ids );
}
$this->db->where('coupon_cat', $catid);
$this->db->where('coupon_status', 'active');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$ds = $query->result_array();}
According to this my SQLquery becomes
SELECT * FROM `tbl_coupons`
WHERE `coupon_store` IN('1,97')
AND `coupon_cat` = '16'
AND `coupon_status` = 'active'
But this query is returning values with coupon_store=1 and no results are coming for coupon_store=97
I checked values for coupon store 97 which exists in that category.
use below way if data exist it will be part of query.
storeids = explode(',',storeids);
$ids = array();
foreach($storeids as $val){
$ids[] = $val;
}
if(!empty($ids)){
$this->db->where_in('coupon_store', $ids );
}
hope it will create proper sql query
The query is mostly correct, except at line 2, where you need to make the change as:
WHERE coupon_store IN('1','97')
everything else remains the same.
This is model i have to search result by using mysql and codeigniter result searching fine but it show all male but not showing age and cast properly
public function get_selected($age, $cast) {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->like('age', $age);
$this->db->or_like('cast', $cast);
$this->db->where("gender='male'");
$data = $this->db->get();
if ($data->num_rows() > 0) {
return $data->result_array();
} else {
return FALSE;
}
}
Remove like and or_like use inside where condition. If you want provide gender also give in same line.
$this->db->where("age LIKE %$age% or cast LIKE %$cast%");
Try This:
$this->db->query("SELECT * FROM " . $this->db->dbprefix('bride_groom_register') . " WHERE gender = male AND age = '".$age."' AND cast = '".$cast."' ");
Hopefully it will help you.
Why are you using the like clause instead of the where clause?
$this->db->where('age', $age);
$this->db->where('cast', $cast);
or
$this->db->where([
'age' => $age,
'cast' => $cast
]);
I am having some trouble in returning values from model to controller Using CodeIgniter. I am passing two arrays from controller to model. I am checking whether both the arrays are not empty and looping them using foreach loop to fetch some values from database and returning the query to controller. Here's my current code
if (!empty($search_for_area) && !empty($search_for_requirement))
{ foreach($search_for_requirement as $search_value_1)
{
foreach($search_for_area as $search_value_2)
{
if($search_value_2 != null && $search_value_1 != null)
{
$nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
print_r($nearbytution->result());
}
}
}
//Line 1
}
print_r($nearbytution->result()); works fine and i am able to view the results. Where should i put return $nearbytution; so that i can get all the fetched values? I tried it in Line 1 but i was getting only values of last array value.
function returnStuff($search_for_area,$search_for_requirement) {
$arr_area = array();
$arr_filter = array();
if ( ! empty($search_for_area) and ! empty($search_for_requirement)) {
foreach($search_for_requirement as $search_value_1) {
foreach($search_for_area as $search_value_2) {
if($search_value_2 != null && $search_value_1 != null) {
$arr_area[] = $this->db->escape($search_value_2);
$arr_filter[] = $this->db->escape_like_str($search_value_1);
}
}
}
}
$str_area = 'NULL';
if ($arr_area)
$str_area = implode(', ', $arr_area);
$str_filter = "'^-$'";
if ($arr_filter)
$str_filter = "'(".implode('|', $arr_filter).")'";
$query = $this->db->query("
SELECT name, area, contactno
FROM tut_listing
WHERE area IN ({$str_area}) AND categoryfilter REGEXP {$str_filter} and partner > '' group by name
");
return $query->result();
}
Seriously, do consider this approach. You only need to bother the poor Mysql server with one call and you get all the data you want at the same time.
Apart from that, practice consistent style in your code. It will save both your time and your hair in the long run.
CodeIgniter escape()
MySQL REGEXP
Try this in the loop:
$nearbytution[] = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name')->result();
return $nearbytution;may then be placed at your "Line 1". It will contain an array of query results.
Unless I misunderstand your question why don't you just store all the results in a big array and then return that array?
function returnStuff($search_for_area,$search_for_requirement) {
$data = array();
if (!empty($search_for_area) && !empty($search_for_requirement)) {
foreach($search_for_requirement as $search_value_1) {
foreach($search_for_area as $search_value_2) {
if($search_value_2 != null && $search_value_1 != null) {
$nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
$data[] =$nearbytution->result());
}
}
}
}
return $data;
}
Now $data will be an array of results, each containing the query results. If your result set has to be cleaned up (to remove duplicates for instance) you can just loop through it and do that cleanup.
What you could also maybe do is build a large SQL query in your foreach loops and then just do that one big query and return the results.