Query from query result in foreach - php

so i have this function
public function GetNearAirport($adr)
{
$acftrange = $this->GetAcftAdr($adr);
$too = array();
foreach ($acftrange as $key) {
$result = $this->GetNearRange($adr,$key->range);
}
The $acftrange give me some rows, and i need to get a new result for each row of $acftrange. But i need all results in one array So I can insert into the html table. Sorry for my english. In foreach the $key->range is a condition for the query to be made.

I assume getNearRange() returns an array. Use array_merge to append $result to $too
foreach ($acftrange as $key) {
$result = $this->GetNearRange($adr,$key->range);
$too = array_merge($too, $result);
}

Related

how to loop through array with dynamic keys in PHP

I have an array data where I am storing the result of an SQL query as below :
$stmt = sqlsrv_query($db,$sql);
$data = [];
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
if(!empty($row)) { $data[] = $row; } }
then I want to create a group key which is the concatenation of specific keys of my array data as below :
foreach ($data as $d) {
$group_key = $d['id'].'_'.$d['Country'].'_'.$d['Order Numer'];
//rest of my code
}
it works fine but I want to choose the keys dynamically instead of setting up manually id, Country and Order Number...
let's say I have an array $PostKeys = ["id","Country","Order Number"]; this array will vary depending on the values selected by user...
What I did is :
$PostKeys = ["id","Country","Order Number"];
foreach ($data as $d) {
foreach($PostKeys as $value)
{ $array_group_key[] = $d[$value] ; }
$group_key = implode("_",$array_group_key);
// rest of my code
}
I am supposed to get the same result but there is always mismatch. I didn't figure out where is the issue exactly. Any suggestions please ? Thank you very much.
You need to empty $array_group_key each time through the loop. Otherwise, you're appending to the results from all the previous rows.
foreach ($data as $d) {
$array_group_key = [];
foreach($PostKeys as $value)
{
$array_group_key[] = $d[$value] ;
}
}

Adding data to existing array after using array data

I have an array of data that is being created from a MySQL query which I am then using to create more data which I then need back in the array (or a new one) but I haven't been able to get this working at all.
Here is the code I have been trying to use:
$vars['items'] = $this->_query('SELECT customers.*, sites.site_name as `default_location`
FROM `customers`
JOIN `sites` ON customers.site_id=sites.id');
$a=array();
foreach($vars['items'] as $customer)
{
if($customer['record_to_use'] == '2')
{
$results = dns_get_record($customer['cust_mx'], DNS_MX);
$mx = min(array_column($results, "pri"));
$highest = array_filter(
$results,
function($item) use($mx) {return $item["pri"] === $mx;}
);
foreach ($highest as $mx)
{
$results = dns_get_record($mx["target"], DNS_A);
foreach ($results as $a)
{
$vars['items']['ip'] .= $a['ip'];
}
}
}
}
So I need the IP that gets created from each item in the array to get added into that array as $vars['items']['ip']
Thanks in advance!
foreach ($results as $a => $value)
{
$vars['items'][$a]['ip'] = $value['ip'];
}
That's what I needed :)
$vars['items']['ip'] .= $a['ip'];
.= is concatenation if that was a typo in your code.
Try $vars['items']['ip'] = $a['ip'];

PDO ForEach Loop in array (PHP)

I want to have all results from my database in one array
my function:
function GetWinkelProduct($g_Winkel) {
global $g_Conn;
$l_Stmt = $g_Conn->prepare("SELECT pd_id FROM `producten_:Winkel`");
$l_Stmt->bindValue(':Winkel', $g_Winkel, PDO::PARAM_INT);
$l_Stmt->execute();
$l_qurries = new dbquery();
$l_LastProduct = $l_qurries->GetLastProduct($g_Winkel);
while($l_Row = $l_Stmt->fetch(PDO::FETCH_ASSOC)){
$out = array();
foreach($l_Row as $product){
$out[] = $product['pd_id'];
}
}
return $out;
}
other page: <?php print_r($l_qurries->GetWinkelProduct($g_Winkel)); ?>
only I get the first result in the array and when I do $product['pd_id'] I get only the last result.
Try this...
Change fetch to fetchAll in while loop and renove foreach inside while loop
function GetWinkelProduct($g_Winkel) {
global $g_Conn;
$l_Stmt = $g_Conn->prepare("SELECT pd_id FROM `producten_:Winkel`");
$l_Stmt->bindValue(':Winkel', $g_Winkel, PDO::PARAM_INT);
$l_Stmt->execute();
$l_qurries = new dbquery();
$l_LastProduct = $l_qurries->GetLastProduct($g_Winkel);
while($l_Row = $l_Stmt->fetchAll(PDO::FETCH_ASSOC)){
$out = array();
$out[] = $l_Row ['pd_id'];
}
return $out;
}
ref:http://php.net/manual/en/pdostatement.fetchall.php
You must initialize the array outside the while loop and you don't need the foreach.
that way you are creating a new array every time the while iterates, that way you end up replacing the array.
$out = array();
while($l_Row = $l_Stmt->fetchAll(PDO::FETCH_ASSOC)){
$out[] = $l_Row['pd_id'];
}
return $out;

Pass array in the CodeIgniter query

I want to pass an array in codeigniter query. Below is my code.
Controller
foreach($array as $values) {
$array_values = $values['download_subcategory_name'];
}
$this->data['get_downloads_content'] = $this->my_model->get_downloads_content($array_values );
Model
public function get_downloads_content($array_values ){
$this->db->select('*');
$this->db->from('my_table');
$this->db->where_in('download_subcategory_name', $array_values );
$this->db->order_by('download_id', 'ASC');
$query = $this->db->get();
return $query->result();
}
My problem is query showing results for only last value of array instead of all array values.
Please help.
Just need little changes within foreach
$array_values = array();
foreach($array as $values) {
$array_values[] = $values['download_subcategory_name'];
^^^
}
OR
$array_values = array();
foreach($array as $key => $values) {
$array_values[$key] = $values['download_subcategory_name'];
^^^^^^
}
Within your code it's storing only the last value from the foreach loop thats why it only gets the single value stored within it.

Foreach inside function

function dp(){
$this->load->library('heart');
$times = 35;
$i = 0; while ($i<$times){
$this->heart->test();
//10 newest
$query = $this->db->get('test', 10, 20);
//set variables from query
foreach ($query->result() as $getrow)
{
$data1 = $getrow->data1;
$data2 = $getrow->data2;
}
//for each data1; do go();
foreach ($data1 as $id){
$this->heart->go($id,$data2);
}
//increment $i
$i++;
}
}
Hey guys, here is my code. I am trying to get the most newest entries from the database, then setting them as variables. For each variable, I am trying to call a function to it passing $id (data1) and $data2. Will data2 be passed or do I need to do something like $data1 as $id && $data2 as $id2. I need to pass over $data1 and $data2 to go() which should be different everytime.
The problem here is I keep getting 'Invalid argument supplied for foreach()' whenever I try to run the code. Anyone know what the issue is here?
Thank you in advance.
I believe you actually want to be creating arrays of $data1 and $data2.
Currently you are overwriting the values in each of those variable each time through the first foreach loop.
$data1 = array();
$data2 = array();
//set variables from query
foreach ($query->result() as $getrow)
{
$data1[] = $getrow->data1;
$data2[] = $getrow->data2;
}
//for each data1; do go();
foreach ($data1 as $key => $id){
$this->heart->go($id,$data2[$key]);
}
Alternatively you could use data1 and data2 as the key/value pair of the array
$data = array();
foreach ($query->result() as $getrow)
{
$data[$getrow->data1] = $getrow->data2;
}
//for each data1; do go();
foreach ($data as $key => $value){
$this->heart->go($key, $value);
}
You can do it all with a single foreach loop without building unnecessary data arrays:
foreach($query->result() as $getrow) {
$this->heart->go($getrow->data1, $getrow->data2);
}

Categories