how we can get two values of array from model in codeigniter - php

This code is in model...
i want to return these two values in controller.....please help me if you know
Array ( [disease_name] => magraines ) Array ( [disease_name] => brain cancer )
when i do
return print_r($qq);
then i get one value but i need two value that one value is
Array ( [disease_name] => magraines ) 1
function getDiseaseInfo()
{
$spytoms = $_GET['syptoms'];
foreach ($spytoms as $ss)
{
$query = $this->db->query("SELECT d.disease_name FROM diseases d,syptoms s ,syptoms_disease sd
WHERE
'$ss' = s.syptom_name AND
s.s_id = sd.s_id_fk
AND sd.d_id_fk IN (d.d_id)
");
$qq = $query->row_array();
print_r($qq);
}
}

From your model return $qq only.
you are returning return print_r($qq) thats not the correct way.
print_r will print the entire array.
if you want to return your array values to your controller you have to return it like return $qq;
Update 1
I think you are getting last row's values,if im right you have to follow below steps,
You have to introduce a new array variable above your foreach
and assign your query array values to this newly created array
and you have to return that newly created array
function getDiseaseInfo()
{
$spytoms = $_GET['syptoms'];
$tmpArray = array();
foreach ($spytoms as $ss)
{
$query = $this->db->query("SELECT d.disease_name FROM diseases d,syptoms s ,syptoms_disease sd WHERE '$ss' = s.syptom_name AND s.s_id = sd.s_id_fk AND sd.d_id_fk IN (d.d_id) ");
$qq = $query->row_array();
$tmpArray[] = $qq;
}
return $tmpArray;
}

Related

mysql get selection data to store in array but i only get 1 as return

public function select($sql){
$results = $this->connection_database->prepare($sql);
$results->execute();
if($results->rowCount() >0){};
return $results;
}
public function prepare_product(){
if(!empty($this->directoryfile)){
$table = $this->directoryfile;
$sql = "SELECT * FROM $table ";
$results = $this->select($sql);
//Fetch our results into an associative array
$results = $results->fetch(PDO::FETCH_ASSOC);
//The registration date of the stored matching user
$productname = $results['DATA_PATH'];
echo json_encode($productname);
}
}
lets said mysql database have "ID" and "DATA_PATH", my intention is to get all the "DATA_PATH" data store in array , but i only get 1 "DATA_PATH" as return , what did i miss ?
can anyone give me some help ?
the data do return but i only get one , there is 5 item in my database.
You have to:-
1.Create an array variable
2.Apply a loop
3.Assign all data to that array variable
4.At last apply json_encode() to that array variable
like below:-
$results = $this->select($sql);
$productname = array(); // create an array
while($row = $results->fetch(PDO::FETCH_ASSOC)){ // apply loop
//The registration date of the stored matching user
$productname[] = $row['DATA_PATH']; // assign value to that array
}
echo json_encode($productname); // json encode the array variable
You need to fetch ALL data not one. use fetchAll
public function prepare_product(){
if(!empty($this->directoryfile)){
$table = $this->directoryfile;
$sql = "SELECT DATA_PATH FROM $table ";
$results = $this->select($sql);
//The registration date of the stored matching user
$productname = $results->fetchAll(PDO::FETCH_COLUMN, 0);
echo json_encode($productname);
}
}

Codeigniter/PHP: How to merge distinct arrays and make it unique into one array in query result

- ingredient
- menu
recipe
How will I merge the two array under name into one array and make it unique. As you can see the result on the [0] is
Beef Adobo
qwerty
iswi
and on 1 is
qwerty
iswi
I want the both of them to be in one array and the result should be
Beef Adobo
qwerty
iswi
query:
public function get_halal($name) {
$terms = explode(',', $name);
foreach ($terms as $name) {
$this->db->distinct();
$this->db->select('r_name');
$this->db->from('recipe');
$this->db->join('menu', 'menu.recipe_id = recipe.recipe_id');
$this->db->join('ingredient', 'ingredient.ingredient_id = menu.ingredient_id');
$this->db->where('menu.category_id = 2');
$this->db->like('ingredient.name', $name);
$query = $this->db->get()->result();
$data[] = $query;
}
return $data;
}
controller:
public function ajaxSearchHalal() {
postdata = file_get_contents("php://input");
if (isset($postdata)) {
$post = json_decode($postdata);
$name = $post->name;
if ($this->user_model->get_halal($name)) {
$user_id = $this->user_model->get_halal($name);
$data = array(
'name' => $user_id,
);
echo json_encode($data);
} else {
echo json_encode("false");
}
} else {
echo "Error!";
}
}
Ok, I see what you are doing now. Thanks for including your tables.
From what I see, you are trying to get all the recipe names, from the recipe table, that have the ingredients your are passing.
What you need to do to fix this issue is not worry about how to merge the arrays, but how you can redo your sql query in order to obtain the information you want in just one query. The way you have it now is not efficient as you are calling a query per every iteration.
What you need to do is use WHERE IN, and GROUP BY to get the info you need and group them by a column. Redo your model method like this:
public function get_halal($name) {
$terms = explode(',', $name);
$this->db->select("r.name");
$this->db->from('recipe r');
$this->db->join('menu m', 'm.recipe_id = r.recipe_id');
$this->db->join('ingredient i', 'i.ingredient_id = m.ingredient_id');
$this->db->where('m.category_id = 2');
$this->db->where_in('i.name', $terms);
$this->db->group_by('r.recipe_id');
$q = $this->db->get();
return $q->result();
}
This will give you one result set which you can then pass as JSON to your front-end without having to iterate or merge arrays.
Hope this helps.

delimit PHP/PDO execute result on multiple unrelated tables

Have a PHP factory with a sub class that queries either one or more unrelated tables in a database. The class takes a table name as a parameter and returns an array object.
When more than one table is required, I would like a way to delimit each tables result set in to its own array. The class would then return a multi-dim array.
I would prefer to not instantiate another instance of the factory. Here is the current query/result code block. I've left out all the other non-essential code for brevity
// if array has more than one table to query,
// run queries on each table
$count = count($tname);
if ($count>1) {
foreach ($tname as $value) { //each table name in array
/* $query = "SELECT s.* FROM $value s"; tried with table alias */
$query = "SELECT * FROM $value";
if ($stmt = self::$_conn->prepare($query)) {
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
} else {
return false;
}
}
return $result;
// else if only one table to query
} else {
$string = $tname; //table name
$query = "SELECT * FROM $string";
if ($stmt = self::$_conn->prepare($query)) {
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
return $result;
} else {
return false;
}
}
With more than one table, would return something like:
Array
(
[0] => Array
(
[team_id] => 3
[team_name] => Maverics
)
[1] => Array
(
[team_id] => 4
[team_name] => Stallions
)
[3] => Array
(
[fld_id] => 1
[fld_name] => 6v6-1
)
[4] => Array
(
[fld_id] => 2
[fld_name] => 8v8-2
)
)
Where 0,1 are from one table and 3,4 are from another.
Thank you in advance
Figured it out... Simple actually, just didn't see it before I asked the questions. I changed:
$result[] = $row;
to
$result[$value][] = $row; //line 11 in code block example
This now returns the array with each table name as the array name for the result set.
Thanks anyway,

How do I get the Array value using Codeigniter

I have this select
$this->db->select('modulo_regra.regra_descricao');
$this->db->from('modulo_regra');
$this->db->where('modulo_regra.modulo_regra_id', id);
$query = $this->db->get();
that return to me 2 elements in
return $query->result_array();
Then I put the return in a Array
$permissoes =array('areas' => $this->Regra_model->user_has($regra['regra_id']));
then I the $permissoes to the session
$this->session->set_userdata($permissoes);
So the real problem comes here.
when I'm loading the value from session
$permissoes = array('areas');
$permissoes = $this->session->userdata('areas');
this is its content:
array(2) ([0] => array(1) ([regra_descricao] => (string) clientes_cadastrar)
[1] => array(1) ([regra_descricao] => (string) clientes_visualizar))
So i can't validate it with the in_array(), or other way...I would like to know if there is how if there is away to compare the value in this array with one another variable
like
if(in_array('clientes_cadastrar',$permissoes)){}
I'm new on it... so sorry for the way i ask.
don't put entire array in return i.e
foreach ($query->result() as $row)
{
$return[] = $row->regra_descricao;
}
return $return;
THEN
you can easily find using:
if(in_array('clientes_cadastrar',$permissoes[**'areas'**])){}
hope this helps
U can use the same scope.
$this->db->select('modulo_regra.regra_descricao');
$this->db->from('modulo_regra');
$this->db->where('modulo_regra.modulo_regra_id', id);
$query = $this->db->get();
foreach ($query->result() as $row)
{
$permissoes = array['areas'];
}
$this->session->set_userdata($permissoes);

Why my php array is over written when they have same Keys

I have a custom PHP function which executes a stored procedure and returns an array:
function test(){
$in = array("abc","bcd","efg");
$result = mydba->executestoredprocedure('proc1',$in);
$arr_sim = array();
foreach ($result['recordset'] as $rows) {
if (!empty($rows)) {
echo $arr_sim[$rows['field1']] = $rows['field2'];
}
}
return $arr_sim;
}
In the above function $arr_sim is returning the number of items correctly when the rows["field1"] values are different. If the rows["field1"] values are the same then it is overwriting the first value and returning only the last one. How can I overcome this?
array ( [chicago] => 'sears', [rochester] => 'liberty' )
If the $arr_sim contains these items then it is returned correctly. Because the keys are different.
array ( [chicago] => 'MCD', [chicago] => 'TACOBELL' )
If the $arr_sim contains these items then it is not returned correctly. Because the keys are the same, "chicago".
Array keys must be unique. Instead, do something like this:
// You want the array to look like this
// array('chicago' => array('MCD', 'TACOBELL'));
function test(){
$in = array("abc","bcd","efg");
$result = mydba->executestoredprocedure('proc1',$in);
$arr_sim=array();
foreach ($result['recordset'] as $rows) {
if(!empty($rows)){
if(array_key_exists($rows['field1'], $arr_sim) {
$arr_sim[$rows['field1']][] = $rows['field2'];
} else {
$arr_sim[$rows['field1']] = array($rows['field2']);
}
}
}
return $arr_sim;
}
Replace $arr_sim[$rows['field1']] = $rows['field2'] with $arr_sim[$rows['field1']][] = $rows['field2']. This will create an array of arrays.
echo $arr_sim['chicago'][0]; // MCD
echo $arr_sim['chicago'][1]; // TACOBELL
Technically, you should write something like this to avoid notices:
if (!isset($arr_sim[$rows['field1']])) $arr_sim[$rows['field1']] = array();
$arr_sim[$rows['field1']][] = $rows['field2'];
But you must really ask yourself, is the field1 (city names) worthy of being the primary key for the array? If not, you should choose some other identifier.

Categories