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.
Related
it's been 3 hours that i'm trying to delete a row in mysql based on a id ...
Seems simple right ?
Taking into consideration that the array might contains several value:
$result = Array ( [3] => 4_Couture )
Array ( [3] => 4_Couture )
$sql_delete = "DELETE FROM users_resumes WHERE id_training_key = ? ";
$stmt_delete= $pdo->prepare($sql_delete);
foreach($result as $r) {
$stmt_delete->execute($r);
}
This seems to be right no ?
error : PDOStatement::execute() expects parameter 1 to be array, string given
Any, any, any clue is very welcome ! thanks a lot from France !
Assuming $result is a one-dimensional array like
$result = [ 3 => '4_Couture' ];
That means you're trying to call $stmt->execute() with a single string value where it requires an array.
I suggest you use bindParam instead
$stmt_delete = $pdo->prepare("DELETE FROM users_resumes WHERE id_training_key = ?");
$stmt_delete->bindParam(1, $r);
foreach ($result as $r) {
$stmt_delete->execute();
}
I have a foreach loop that goes through a list of items. For each of these items, I have a while loop that grabs data out of a database.
$output = array();
//$reference is a multidimensional array has been passed to this page
where the element `color` contains the color I want.
foreach ($reference as $c) {
$color = $c['color'];
$query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
$exquery = mysqli_query($con, $customerQuery);
while ($row = mysqli_fetch_array($exquery)) {
$person = $row['person'];
array_push($output[$color], $person);
}
}
So this loops through, the first time searching 'red', and finding 5 people in the fake table who like red. Next, 'blue', where it finds 1 person, and then 'green' where it finds 3.
If I look at the individual results, my first array has "red, blue, green" and my second array has these lists of names.... I just don't know how to add them into an array together.
I'm trying to build an array like this:
Array
(
[Red] => Array
(
[0] => John
[1] => Sally
[2] => Bob
...
)
[Blue] => Array
(
[0] => Luke
)
[Green] => Array
(
..etc...
)
I'm not using array_push correctly though - I'm getting an Warning: Illegal offset type error. What am I doing wrong?
It's been a while since I've worked with PHP, but I think you need to initialize each "color" array that you're going to push into. So...
$output = array();
//$reference is a multidimentional array has been passed to this page
where the element `color` contains the color I want.
foreach ($reference as $c) {
$color = $c['color'];
$query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
$exquery = mysqli_query($con, $customerQuery);
while ($row = mysqli_fetch_array($exquery)) {
$person = $row['person'];
if (!array_key_exists($color, $output)) {
$output[$color] = array();
}
array_push($output[$color], $person);
}
}
Try changing:
array_push($output[$color], $person);
Into:
$output[$color][] = $person;
From the manual on array_push:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
I am using PDO statement like below
$sql1 = "select food_typename from foodtypes WHERE 1";
$statement1 = $pdo->prepare($sql1);
$statement1->execute();
$results1 = $statement1->fetchAll(PDO::FETCH_ASSOC);
print_r($results1);
I am getting output as below:
Array
(
[0] => Array
(
[food_typename] => Punjabi
)
[1] => Array
(
[food_typename] => Indian
)
)
I want it be like
Array('Punjabi','Indian')
Any suggestions please?
If you're running PHP >= 5.5
$results = array_column($results1, 'food_typename');
If you're running earlier versions of PHP,
$results = array_map(
$results1,
function($value) {
return $value['food_typename'];
}
);
Though I don't really understand why you can't work with the original array in the first place
You can use array_map but foreach works just as well and actually runs faster than array_map for cases like this:
// Set a test array.
$results1 = array();
$results1[] = array('food_typename' => 'Punjabi');
$results1[] = array('food_typename' => 'Indian');
// Set the final reults in an array.
$results_final = array();
foreach ($results1 as $results1_value) {
$results_final[] = $results1_value['food_typename'];
}
// Dump the line array for debugging.
echo '<pre>';
print_r($results_final);
echo '</pre>';
And the output of that would be:
Array
(
[0] => Punjabi
[1] => Indian
)
The query way:
SELECT GROUP_CONCAT(food_typename), 1 AS dummy
FROM foodtypes
GROUP BY dummy
Then you need to retrieve the first field of the only record you will get back from mysql, and turn it into an array: see return group_concat data as array
I have just started using CodeIgniter and want to get data from database using $query->result(), but without a foreach loop. Here is my current code:
$this->db->select('m_name');
$query1 = $this->db->get("marchant_details",1);
$rows1 = $query1->result();
However, I don't want to use a foreach loop like this, to retrieve the data:
foreach($query1->result() as $rows1)
{
$name[] = $rows1->m_name;
}
Can anyone offer an alternative solution?
I just did a quick search. I'm going to sleep, but can you try something like this:
$query->result_array();
If it doesn't work, I'll check tomorrow.
source: user-guide Maybe it will come handy.
There are two assumptions: either I misunderstood the question or the others did.
The point: by passing 1 as second parameter to $this->db->get(); method, it sets LIMIT 1 to the query and you'll get only 1 row as result.
So, why should you need to use a loop on a single row db result?
If you use $query->result(); the result would be something like:
Array
(
[0] => stdClass Object
(
[m_name] => Foo
)
)
To get the m_name value you can do the following:
$result = $query->result();
echo $result[0]->m_name;
By any reason, if you need a numeric array contains the value (as you did it in your loop) you can simply do it by $name[] = $result[0]->m_name;.
And if you use $query->result_array(); the result would be something like:
Array
(
[0] => Array
(
[m_name] => Foo
)
)
To get the m_name value you can do the following:
$result = $query->result_array();
echo $result[0]['m_name'];
But if you stop limiting the query, there are multiple rows in your query result, you can use rows1 = $query1->result_array(); instead. and array_map() to manipulate the elements of the given array:
$rows1 = $query1->result_array();
// `$row1` will be something like this:
Array
(
[0] => Array
(
[m_name] => Foo
)
[1] => Array
(
[m_name] => Bar
)
[2] => Array
(
[m_name] => Baz
)
)
Use array_map() to manipulate the result array:
function getName($array) {
return $array['m_name'];
}
$array = array_map("getName", $rows1);
print_r($array);
If you are using PHP v5.3+ you do the following:
$array = array_map(function($array) {
return $array['m_name'];
}, $rows1);
Well there is the official page in the CodeIgniter User Guide for generating DB query results depicting variations like
$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()
If you work with CodeIgniter, its charm is that it effectively quite nicely documented.
I would assume you can go further down the abstraction layers if that's what you want, respectively you can not use the DB class but the class of your choice as custom library or whatever.
Why then are you not happy with the given possibilities, respectively how do you want to generate your query results?
you can do like this
$data=array();
$this->db->select('m_name');
$query1 = $this->db->get("marchant_details",1);
$data['name'] = $query1->result_array();
return $data;
and for more information
result_array(); // For multiple rows
row_array(); // For one row
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);