How to put a string to make a query using PHP CodeIgniter - javascript

I have different strings that is made by multiple checkboxes when is checked and I would like to use this strings to make a query.
For example:
If I click a specific sequence of checkboxes this returns me this value of string: "SELECT Call_Setup FROM lte_kpi.vw_main_kpis_cidade_rate_daily WHERE node != 'UNKNOWN' and node = 'ABADIA DE GOIÁS' and date between '10/02/2017' and '10/18/2017' order by date"
I save this query into a variable called query1. What I would like is to put this variable (query1) like parameter to perform a query.
This is my model:
<?php
class Model_KPI_Customize extends CI_Model{
function kpi_customize_CustomQuery(){
$query = $this->db->query(/*I would like to put my string here !*/);
return $query->result();
}
}
?>
This is my Controller
public function highcharts_teste(){
$this->load->model('model_kpi_customize');
$data['kpi_customize_string_CustomQuery'] = $this->model_kpi_customize
->kpi_customize_CustomQuery();
}
And I have a view with multiple checkboxes and one paragraph element wich I would like to display the result of a query.
In the example above I would like to return the following values like this, because this is the result when I query it on Database:
99.73
99.48
99.51
99.40
99.23
99.34
99.29
99.10
99.23
99.11
Thanks everyone.
Bye =)

SELECT Call_Setup
FROM
lte_kpi.vw_main_kpis_cidade_rate_daily
WHERE
node != 'UNKNOWN' and
node = 'ABADIA DE GOIÁS' and
date between '10/02/2017' and '10/18/2017'
order by date
Can be written using active_record like below
$where = array(
'node' => 'ABADIA DE GOIÁS',
'date >=' => '10/02/2017',
'date <=' => '10/18/2017'
);
$this->db->select('Call_Setup')
->get_where('vw_main_kpis_cidate_rate_daily', $where )
->order_by("date", "asc");

Related

how to use the variable in snowfalke procedure

var step0=`select INGESTION_SUCCESSFUL,ingestion_uuid from FILE_INGESTION_HISTORY where ingestion_uuid=(
select ingestion_uuid from registration where registry_subject_uuid=:1
qualify row_number() over( order by dnb_latest_received_dt desc)=1)`;
var statement0=snowflake.createStatement( {sqlText: step0,binds: [PARAM_REG_SUB_UUID]} );
variable1= statement0.execute();
variable1.next();
ingsindc=variable1.getColumnValue(1);
ingsuuid=variable1.getColumnValue(2);
when i try to use above ingsuuid in sql where clause it is throwing error
var step1= create or replace temporary table FN_IGSN_REG_LBV1 as select * from registration where ingestion_uuid=**ingsuuid** and (DELETE_INDC=0) qualify row_number() over (partition by REGISTRATION_HASH_KEY order by dnb_latest_received_dt desc, row_created_tmst desc, registration_uuid desc) = 1;
var statement1=snowflake.createStatement( {sqlText: step1} );
statement1.execute();
Binding a variable to a SQL statement allows you to use the value of the variable in the statement.
You can bind NULL values as well as non-NULL values.
The data type of the variable should be appropriate for the use of the value in the SQL statement. Currently, only JavaScript variables of type number, string, and SfDate can be bound. (For details about the mapping between SQL data types and JavaScript data types, see SQL and JavaScript Data Type Mapping.)
Here is a short example of binding:
var stmt = snowflake.createStatement(
{
sqlText: "INSERT INTO table2 (col1, col2) VALUES (?, ?);",
binds:["LiteralValue1", variable2]
}
);
More details: https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html#binding-variables

how to get string 'Sugar&Jaggery, Salt' via <a> in codeigniter?

plz anyone me i m beginner in codeigniter in herf passing url with string but the whole can't get in
controller & can't accepting as sting so what shold i do for it plz someone help me
//Here is html code which pass sting value
index.php/Home/product_show?type='Sugar&Jaggery , Salt'">Sugar & Jaggery , Salt
//controller get sting in type variable but on Sugur got not whole sting and pass type variable to model
public function product_show(){
$type = $_GET['type'];
die(var_dump($type));
$data['testdata']= $this->Globle_model->get_multiple_record($type);
$this->load->view('display_product',$data);
}
//model type variable check the subcategory from database and return to controller
public function get_multiple_record($type){
$this->db->where_in('Subcategory_name',$type);
$get_data = $this->db->get('productmaster');
return $get_data->result_array();
}
//o/p
get this much of string(6) "'Sugar"
Please follow the below way to get all your string into the controller into
$_GET['type'] variable.
Combine all your strings into a single variable using _ into href
Example :
Sugar & Jaggery , Salt
In the controller, I have created one array() with variable subcategory_type .In which key is coming from the view but its value from the database.
Example :
function get_product_type(){
$get_info = $this->input->get();
$subcategory_type = array(
"Sugar_Jaggery_Salt" => "Sugar&Jaggery , Salt"
);
$type_arr = $subcategory_type[$get_info['type']];
$data['testdata']= $this->Globle_model->get_multiple_record($type_arr);
$this->load->view('display_product',$data);
}
In model,
function get_multiple_record($type_array){
$this->db->where_in('Subcategory_name',$type_array);
$get_data = $this->db->get('productmaster');
return $get_data->result_array();
}
Try this and update me in case of anything.

How to get last insert id from mysql database in php?

I want to get last insert id from my table cart in my else statement after the insert. But I am not getting the id.
Please check my code and suggest what am I doing wrong:
// Check to see if the cart COOKIE exists
if ($cart_id != '') {
// Here I want to update but not getting $cart_id, so everytime insert query fire in else statement
}else{
$items_json = json_encode($item);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}') ");
$cart_id = $db->insert_id; // Here is the problem
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}
Your suggestions would be welcome.
Instead of:
$db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}') ");
$cart_id = $db->insert_id; // Here is the problem
Use this, directly from the documentation:
$query = "INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}') ";
mysqli_query($db, $query);
$cart_id = mysqli_insert_id($db);
Get the identity column value AFTER the insert:
create table student (
id int primary key not null auto_increment,
name varchar(20)
);
insert into student (name) values ('John');
select ##identity; -- returns 1
insert into student (name) values ('Peter');
select ##identity; -- returns 2
Or get the next auto incremental value before insert:
$query = $db->query("SHOW TABLE STATUS LIKE 'cart'");
$next = $query->fetch(PDO::FETCH_ASSOC);
echo $next['Auto_increment'];

How to run join query in codenighter

hi i am new to codenighter frame work soome one can help me that how can i convert this join query to codenighter frame work and also told me that where i have to kept the join query i mean in model or controller..?
and how can i fetch the result of this query ..?
i want to pass the class_id as parameter..?
my query is here ..
SELECT e.roll,s.name,p.title,i.amount,i.amount_paid,i.status,i.creation_timestamp
FROM `payment` p
INNER JOIN student s ON s.student_id=p.student_id
INNER JOIN enroll e ON e.student_id=p.student_id
INNER JOIN invoice i ON i.student_id=p.student_id AND e.class_id=6 AND e.year="2016-2017"
You need to keep this SQL query in your model and then pass the result to controller. You can pass the result set to controller from model and can use it from there. If you wan to pass it to view then you need to pass its data variable into view variable.
it should be like this,
In MODEL File:
function get_data(){
$this->db->select('enroll.roll,student.name,payment.title,invoice.amount,invoice.amount_paid,invoice.status,invoice.creation_timestamp');
$this->db->from('payment');
$this->db->join('student', 'student.student_id = payment.student_id', 'inner');
$this->db->join('enroll', 'enroll.student_id = payment.student_id', 'inner');
$this->db->join('invoice', 'invoice.student_id = payment.student_id', 'inner');
$this->db->where(array('enroll.class_id'=>6,'enroll.year'=>'2016-2017'));
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result_array();
}
}
In CONTROLLER you can call this model function like this:
$data['Data'] = $this->student_model->load_countries();
$this->load->view('student_data',$data);
This is as per CodeIgniter active record framework. Hope this will help you.
you can use query method to write your own query .
in query method you can use joins easily
for example
Your Model
class Model_name extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_record(){
$data = $this->db->query('
SELECT e.roll,s.name,p.title,i.amount,i.amount_paid,i.status,i.creation_timestamp
FROM `payment` p
INNER JOIN student s ON s.student_id=p.student_id
INNER JOIN enroll e ON e.student_id=p.student_id
INNER JOIN invoice i ON i.student_id=p.student_id AND e.class_id=6 AND e.year="2016-2017"
');
if($data->num_rows()>0){
return $data;
}
}
}
$this->db->join() is helper funtion to perform join query
Permits you to write the JOIN portion of your query:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs JOIN comments ON comments.id = blogs.id
Multiple function calls can be made if you need several joins in one query.
If you need a specific type of JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, and right outer.
$this->db->join('comments', 'comments.id = blogs.id', 'left');
// Produces: LEFT JOIN comments ON comments.id = blogs.id
You can directly write query in the query() function too.
$query = $this->db->query('SELECT e.roll,s.name,p.title,i.amount,i.amount_paid,i.status,i.creation_timestamp
FROM `payment` p
INNER JOIN student s ON s.student_id=p.student_id
INNER JOIN enroll e ON e.student_id=p.student_id
INNER JOIN invoice i ON i.student_id=p.student_id AND e.class_id=6 AND e.year="2016-2017"');
References: CodeIgniter Docmentation
Refer:-https://www.codeigniter.com/userguide3/database/query_builder.html
You can also use below style:-
In MODEL File:
function get_data(){
$this->db->where(array('enroll.class_id'=>6,'enroll.year'=>'2016-2017'));
$this->db- >select('enroll.roll,student.name,payment.title,invoice.amount,invoice.amount_paid,invoice.status,invoice.creation_timestamp');
$query = $this->db->from('payment')->join('student', 'student.student_id = payment.student_id', 'inner')->join('enroll', 'enroll.student_id = payment.student_id', 'inner')->join('invoice', 'invoice.student_id = payment.student_id', 'inner')->get();
if($query->num_rows()>0){
return $query->result_array(); //You can also use result(); provide object array
}
}
In CONTROLLER you can call this model function like this:
$data['Data'] = $this->student_model->load_countries();
$this->load->view('student_data',$data);
In view you can get this with $data variable

How to get all php array index from javascript?

I have a php file where I saved all language string. This is the content:
function lang($phrase)
{
static $lang = array(
'step_one' => 'First step',
'step_two' => 'Second step',
... and so on ...
);
return $lang[$phrase];
}
Essentially, when I load a javascript file I want store all array index in a variable like this:
var Lang = <?php echo json_encode(lang()); ?>;
this code line is inserted in a script, this script is available in a php file. Now before of execute this line I have imported the php file where all string translation is available. What I'm trying to achieve, is get all index of this array, in the variable Lang.
Actually I can load a single string traduction from php like this:
lang('step_one');
but how I can save this array in javascript variable?
You can use array_keys to retrieve all array keys. To do that you need your function to return the whole array on request. You can do that with leaving the argument ($phrase) empty and do an if condition with empty in your lang function. You also need to set a default value for $phrase in your function to not raise any errors if you don't pass an argument to the function.
echo json_encode(array_keys(lang());
And the function:
function lang($phrase = "")
{
static $lang = array(
'step_one' => 'First step',
'step_two' => 'Second step',
... and so on ...
);
if(empty($phrase)) {
return $lang;
} else {
if(isset($lang[$phrase])) { //isset to make sure the requested string exists in the array, if it doesn't - return empty string (you can return anything else if you want
return $lang[$phrase];
} else {
return '';
}
}
}
I also added isset to make sure the requested element exists in your language array. This will prevent raising warnings.

Categories

Resources