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
Related
In all the examples of Codeigniter queries at https://www.codeigniter.com/user_guide/database/results.html, I find that the name of the field must be known to get its value.
$query = $this->db->query("SELECT title,name,body FROM table");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
For example, if I want to get the title, I will perform row->title. Is there a way to get the title using an index e.g. like $row[0]?
Use result_array function returns the query result as a pure array
$query = $this->db->query("SELECT title,name,body FROM table");
$result = $query->result_array();
foreach($result as $res){
echo $res['title'];
echo $res['name'];
echo $res['body'];
}
if you want to access via index then use array_values:
$result = $query->result_array();
foreach($result as $res){
$r = array_values($res);
echo $r[0];
echo $r[1];
echo $r[2];
}
<?php
$result = $subject_code->result_array();
foreach($result as $res){
$r = array_values($res);
print_r($r);
}
?>
I applied the above code but in o/p index of each value is comming 0
o/p- Array ( [0] => 201 ) Array ( [0] => 202 ) Array ( [0] => 203 ) Array ( [0] => 204 ) Array ( [0] => 205 )
Slightly more elegant way:
$data = $this->db->query("SELECT title,name,body FROM table")->result_array();
array_walk($data,function(&$row) {
$row = array_values($row);
});
And you have $data array with numeric indexes.
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);
I use codeigniter. I want print rows name of database by foreach. is not array value row name in the database.
My way don't work and is output: Array
In the Controller:
$query = $this->db->query("SELECT * FROM welcome ORDER BY id desc");
$data = array();
foreach ($query->result() as $row)
{
$data['output'] = array('name' => $row->name);
}
$data['output'] = $data;
$this -> load -> view('welcome', $data);
In the view:
<?=$output?>
I put as your example, but queries should be in model:
$query = $this->db->query("SELECT * FROM welcome ORDER BY id desc");
$data = array();
foreach ($query->result() as $row){
$data['output'][] = $row->name;
}
$this -> load -> view('welcome', $data);
In the view:
<pre>
<?=print_r($output)?>
$output is an array of values, so to print it out, you need to use a function such as print_r().
<?php
print_r($output);
?>
Using echo or the PHP short output tags on an array will output the data type Array instead of it's contents.
You should not use <?=$output?> to see arrays.
Arrays are structured variables that are composed of multiple keys and values.
That way you should use either:
print_r() - http://php.net/manual/en/function.print-r.php
or
var_dump() - http://php.net/manual/en/function.var-dump.php
if you want to print your array, display it all with this:
foreach ($output as $stuff) {
print_r($stuff);
/* and a break like */
/* in care your array is multidimensional */
}
$this->db->select('id, user_id')->from('be_users')->where('id',$user_id);
$data['user_individual'] = $this->db->get();
If this is my db query, how do I get an array of one db row...
ie. I want to do something like $data['user_individual']['id']->format_as_array...
CodeIgniter provides several methods to perform on query results.
See here: https://codeigniter.com/user_guide/database/results.html
result() returns an array of PHP objects.
row() returns a single PHP object for that row.
result_array() returns an array of arrays.
row_array() returns a single array for that row.
row() and row_array() optionally take a parameter which is the number of the row you'd like to return.
Beyond this, it's difficult to tell exactly what you're asking for. You should be able to get your data out exactly as you like with these methods.
Edit:
Incidentally, the way to access these methods is through the query object returned from the $this->db->get() call:
$query = $this->db->get();
$rows = $query->result(); //array of objects
$rows_array = $query->result_array(); //array of arrays
$row = $query->row(); //single object
$row_array = $query->row_array(); //single array
When you use $this->db->get(); you can use $this->db->result(); which returns an arrayobject.
$query = $this->db->get('table_name');
foreach ($query->result() as $row)
{
echo $row->title;
}
See: http://codeigniter.com/user_guide/database/results.html for details on how to obtain resultsets from the database.
Instead of $ this-> db-> get () use $ this-> db-> row_array ();
$this->db->select('id, user_id')->from('be_users')->where('id',$user_id);
$data['user_individual'] = $this->db->row_array();
/**
* format_database_array
*
* Lets you format the database object data to array
#access public
#param String $db_object : database query object
#return String : Return array with data
*/
if ( ! function_exists('format_database_array'))
{
function format_database_array($db_object)
{
$db_array = array();
if($db_object-> num_rows >0)
{
foreach ($db_object->result_array() as $row)
{
foreach ($row as $key => $value)
{
$db_array[$key] = $value;
}
}
}
return $db_array;
}
}
I am pretty new to php and could sure use some help understanding how to get my result the way I need it from a database query.
What I need is an associative array like this, 'bla'=>'bla'. What I am getting from my foreach loop is this from a print:
[0] => Array
(
[0] => test0
[name] => test0
[1] => 1
[customer_id] => 1
)
[1] => Array
(
[0] => test
[name] => test
[1] => 2
[customer_id] => 2
)
Here is my loop:
foreach($res as $key=>$val)
{
// have no idea..
}
Can someone please help me to format my results so that they are like 'index'=>'value'
Thanks for any help.
Here is a sample code that uses a foreach but yet pulls an association. I don't get it. I am thinking that my result set with the indexes are because I am not writing the loop correctly. Here is the code that uses the foreach
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
Here is the part of the database class that I am using to fetch the results.
$returnArray = array();
$i=0;
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
if($row)
$returnArray[$i++] = $row;
}
mysql_free_result($result);
return $returnArray;
After using the code that was given to me to omit the index numbers, here is what I am now left with. Its close but not what I need.
Array
(
[id] => 1
[cust] => bobs auto
)
This is what the above line should read like
'1' => 'bobs auto'
What I am trying to do is to format the output for a JSON call for a suggestion box.
I cannot get this to work. Here is everything after my db connection.
$out_array = array();
foreach($items as $key=>$val)
{
if(is_int($key))
{
continue;
}
$out[$key['id']] = $val['cust'];
}
//echo'<pre>';
//print_r($out_array);
//echo'</pre>';
foreach ($out_array as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
OK, I think I am coming down to the home stretch. I have what I need sort of. This is the code I have so far.
$out_array = array();
foreach($items as $key)
{
$out_array[$key] = $val;
//$out_array[$key['id']] = $key['cust'];
}
Notice that the commented line does not work, It outputs like the id twice but the line that isn't commented out works just fine. Here is the output from that.
Array
(
[8] =>
[FAT BURGER] =>
)
From this point, would I just use another foreach to iterate over the entire set of data? The array output you see above is from a print_r.
This is what I now have and it returns the correct association however, I must comment out the strpos condition to get any results back and I don't know why. Am I correct in nesting these foreach loops like I have?
$out_array = array();
foreach($items as $key)
{
// $out_array[$key] = $val;
$out_array[$key['id']] = $key['cust'];
foreach ($out_array as $key=>$value)
{
if (strpos(strtolower($key), $q) !== false)
{
echo "$key|$value\n";
}
}
}
So you don't want the numeric indexes in your array? You must be using mysql_fetch_array(), which returns your results with both numeric and string keys. Use mysql_fetch_assoc() to get an array with only the string keys (the string being the column name).
Try something like this. It works by skipping the integer indices, and putting the non-integer indices into an output array.
$out_array = array();
foreach($res as $key=>$val) {
if(is_int($key)) {continue;}
$out_array[$key] = $val;
}
EDIT:
$out_array = array();
foreach($items as $key=>$val)
{
if(is_int($key))
{
continue;
}
}
$out[$out_array['id']] = $out_array['cust'];
//echo'<pre>';
//print_r($out_array);
//echo'</pre>';
foreach ($out as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
Assuming this is a MySQL database, the results, if more than one, are returned as a multidimensional array.
When you run the query:
$query = "SELECT * FROM Table WHERE ...";
$query = mysql_query($query);
while($info = mysql_fetch_assoc($query)){
//$info is now a single row, associative array
echo print_r($info);
}
the echo print_r displays the results the way you are looking for them now 'index'=>'value'
EDIT: based on comments.
If you absolutely CAN'T get rid of the mysql_fetch_array then you'll have to hack the code. It's not clean and I strongly advise refactoring but below is the code you'll need to create an array of field name indexes only from what you're given
$my_array = array();
$info = $data[0]; //grab the first row of your data set from the original question.
foreach($info as $index => $value){
if(!is_int($index)){
$my_array[$index] = $value;
}
}
The newly created $my_array will be in the format you're looking for.
You got this array from a query and result function from PHP, yeah?
If you were using mysql, it's actually easier to do it like below.
$query = mysql_query("SELECT * FROM dbname"); //Create the query to your database
while($data = mysql_fetch_array($query)) //Loop through our results from the query
{
echo($data['fieldname']."<br/>"); //echo out data through the loop
}
$ret = array();
foreach($rows as $row)
{
$ret[$row['id']] = $row['cust'];
}
$json = json_encode($ret);
echo $json;
// Prints something like:
//
// {1:'bob'}
Note the use of json_encode.
Ok, regarding my last question. I was incorrect in nesting the foreach loops. I also had a typo in my code. It is working, finally. Thank you to all that have helped me!