How do I get the Array value using Codeigniter - php

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);

Related

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

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;
}

Access array variable in CodeIgniter

this is my module
function order_alert($email){
$this->db->select("tbl_customer_registration.cus_mobile");
$this->db->from('tbl_customer_registration');
$this->db->where('tbl_customer_registration.cus_email', $email);
$query = $this->db->get();
echo '<pre>';
print_r($query->result()) ;
}
It return the following result for the above code
Array
(
[0] => stdClass Object
(
[cus_mobile] => 0716352642
)
)
In the contoller I use foreach
foreach($result as $row) :
echo $row['cus_mobile'];
to get [cus_mobile] out of the above array but it gives me all [cus_mobile] rows in the tbl_customer_registration.
How do I get the only [cus_mobile] => 0716352642 out of it.
$row->cus_mobile
since you have an array of objects.. not really sure if i got your question
(UPDATED)
Try this..
foreach($query->result() as $row) {
echo $row->cus_mobile;
}
If I got well your question, what your doing is retrieving data as array of objects and trying to access its data as pure array. To do so as you mean you might retrieve data result as an array there is method called result_array() you might use it instead $query->result().
echo '<pre>';
print_r($query->result_array()) ;
foreach($result as $key => $row) :
echo $row['cus_mobile'];
function order_alert($email){
$this->db->select("tbl_customer_registration.cus_mobile");
$this->db->from('tbl_customer_registration');
$this->db->where('tbl_customer_registration.cus_email', $email);
$query = $this->db->get();
if($query->num_rows()==1)
$result_row = $query->row();
return $result_row;
else
return false;
}
foreach($result_row as $row) {
echo $row->cus_mobile;
}
This will work for if result has one row... now to need to handle the case for greater than one row.Thanks

Codeigniter function array always returns no

Rather noobish question. Given the following code:
public function in_group($group)
{
$session = $this->CI->session->userdata('logged_in');
$query = $this->CI->db->get_where('people_groups', array('people_id' => $session['user_id']));
$array = array();
foreach ($query->result() as $row)
{
$array = $row->group_id;
}
if ( $array == $group)
{
return 'YES';
}
return 'no';
}
The result is always no, when in fact it should be yes according to the database.
What i am trying to do is to check the people_groups table to see if the current user is in the requested $group. I have research as to how to use the arrays in the following way, but i fear i have done something rather noobish.
NOTE: The $group_id references the column in the table that stores the group id's
It's not inserting values into the array, so change this:
$array = $row->group_id;
to
$array[] = $row->group_id;
You should just change your query to select where there is a row with that user and group id, something like this:
$this->CI->db->select('*');
$this->CI->db->from('people_groups');
$this->CI->db->where('people_id', $session['user_id']);
$this->CI->db->where('group_id', $group);
$query = $this->CI->db->get();
Then you can just check if there is a result or not, instead of looping through all the results.
I descovered that i missed a section in the user guide to achieve what i wanted the correct code should have been:
public function in_group($group)
{
$session = $this->CI->session->userdata('logged_in');
$query = $this->CI->db->get_where('people_groups', array('people_id' => $session['user_id']));
foreach ($query->result_array() as $row)
{
if ( $row['group_id'] == $group)
{
return 'YES';
}
return 'no';
}
}
Thank you to the people who responded, I hope this helps other people making silly mistakes :)

how to do a foreach loop in php?

I have a function that has a query and a foreach loop:
$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);
foreach($object as $val){
$results = array('id'=>$val->id, 'from_id'=>$val->from_id);
$this->result[] = $this->notify($results);
}
return $results;
The issue here is that if I return $object I get 2 records:
Array
(
[0] => stdClass Object
(
[from_id] => 6
[id] => 3
)
[1] => stdClass Object
(
[from_id] => 6
[id] => 1
)
)
and return $results has 1 record:
Array
(
[id] => 1
[from_id] => 6
)
Also if I return $this->result;, $this->result[] = $this->notify($results); does run twice but uses the same record twice returned by $results instead of using the 2 records from $object
Hope you guys can understand my issue.
ps: I am using the zend framework
Any ideas?
Edit: notify is a function in another class
$getResults isn't being set anywhere in your code, visibly. It looks like you ought to be returning $this->result instead, as that's where the results are being stored. That's my best guess given the amount of code you've given us. If you can provide more code, I can further update my answer if it doesn't work for you.
Given your comment, update your code to this:
$sql = "SELECT * FROM explore WHERE id = $id";
$object = $this->db->select($sql);
foreach($object as $val){
$results = array('id'=>$val->id, 'from_id'=>$val->from_id);
$this->result[] = $this->notify($results);
}
return $this->result;
If you're only returning $results, it'll be filled with the last item, not every item.
The reason you have a problem is that you redefine the $results array on each iteration of the foreach loop, instead of adding an element to it.
This is where the problem is:
//...
foreach($object as $val){
$results = array('id'=>$val->id, 'from_id'=>$val->from_id);
//... ^ you are reassigning the whole value of $results
Do this instead:
//...
$results = array();
foreach($object as $val){
$results[] = array('id'=>$val->id, 'from_id'=>$val->from_id);
//... ^^ note the array push instead of complete reassign
EDIT as #Cyclone has rightly pointed out, the above answer is in fact wrong. You need to be doing one of two things:
returning $this->result instead of $results
populating the $results variable with the processed data, instead of $this->result.
Essentially, you either need to change:
return $results;
to:
return $this->result;
Or, change the loop to this:
$results = array();
foreach($object as $val){
$results[] = $this->notify(array('id'=>$val->id, 'from_id'=>$val->from_id));
}
Which one you want to do depends on whether you actually need $this->result - i.e. whether you need to keep the results in the object after this code has executed.

Adding to a multidimensional array in PHP

I have an array being returned from the database that looks like so:
$data = array(201 => array('description' => blah, 'hours' => 0),
222 => array('description' => feh, 'hours' => 0);
In the next bit of code, I'm using a foreach and checking the for the key in another table. If the next query returns data, I want to update the 'hours' value in that key's array with a new hours value:
foreach ($data as $row => $value){
$query = $db->query('SELECT * FROM t WHERE id=$row');
if ($result){
$value['hours'] = $result['hours'];
}
It's all fine except that I've tried just about every combination of declarations for the foreach loop, but I keep getting the error that the $value['hours'] is an invalid reference. I've tried declaring $value[] ... but that doesn't work either. I don't need to iterate through $value so another foreach loop isn't necessary.
Surely this is easier than my brain is perceiving it.
Here's the whole snippet:
foreach($_gspec as $key => $value){
$sql = sprintf('SELECT * FROM List WHERE specialtyID=%s', $key);
$query = $db->query($sql);
if ($query->num_rows() !== 0){
$result = $query->row_array();
$value['hours'] = $result['hours'];
}
}
You want
$data[$row]['hours'] = $result['hours']
$row would be better named as $key (that is what it is!)
Some people would suggest using pointers, but I find this way makes more sense to me.
You need to use ampersand in front of the $value in foreach to pass it by reference like this:
foreach ($data as $row => &$value){
$query = $db->query($sql);
if ($result){
$value['hours'] = $result['hours'];
}
}
More info here: http://php.net/manual/en/control-structures.foreach.php
As of PHP 5, you can easily modify
array's elements by preceding $value
with &. This will assign reference
instead of copying the value.
Use reference ->
foreach ($data as $row => & $value) {
$query = $db->query('SELECT * FROM t WHERE id=$row');
// [...]
if ($result) {
$value['hours'] = $result['hours'];
}
}

Categories