How to execute two different query with single foreach in PHP - php

I am working with Php and right now i am inserting query using foreach loop (passing array) but how can i use another query with foreach and pass value into same array ?
Here is my current code
$data=$this->db->get($firstquery) // first query record in array
$second=$this->db->get($second_query); // second query record in array
foreach($records as $rec)
{
$data22 = array
(
'file_id'=>$rec['id'], // here is first query result
'text'=> // How to pass second query result here
);
}

You can call another loop inside main loop and execute another query
$data=$this->db->get($firstquery) // first query record in array
$second=$this->db->get($second_query); // second query record in array
foreach($records as $rec)
{
foreach($second as $sec) //second query loop
{
//enter code here
}
$data22 = array
(
'file_id'=>$rec['id'], // here is first query result
'text'=> // How to pass second query result here
);
}

Related

Laravel database insert with combining array and string

I should insert an array such as $attendanceList = [18354012,18354013,18354014,18354015] and $present = "FALSE" and same for all item of $attendanceList. As you see $attendanceList is array but $present is String.
When I insert like DB::table("attendance")->insert(["id"=>$attendanceList,"present"=>"FALSE"]) returns error.
What should I do? Pairing all item of $attendanceList and $present or there are another ways?
Note: I don't want to use loop if it is possible.
You can prepare array from your data and do bulk insert in one query:
<?php
$attendanceList = [18354012,18354013,18354014,18354015];
$present = "FALSE";
$insertData = array_map(
fn($el)=>['id'=>$el, 'present'=>$present],
$attendanceList
);
$db::table("attendance")->insert($insertData);
Test Laravel DB insert online
I'm not sure why you don't want to use a loop. If you want to insert four separate rows, you're going to need one:
foreach ($attendanceList as $id) {
DB::table("attendance")->insert(["id" => $id,"present" => $present]);
}

Fetch data from mysql using associative array and implode (codeigniter + MySql)

I've gathered data from multiple MySql tables and stored them as associative arrays using a foreach loop with the query.
I would like to use those associative arrays and the implode method in the mysql query to gather more data from a separate table.
I know that with the implode method, when dealing with Indexed arrays, you can just insert the array directly in the "implode section". But with associative arrays, I am unsure how to call all the available arrays and insert them in the query.
Please refer to the attached image for a detailed illustration explaining it further.
Below is also a portion of my code
public function user_implode()
{
$s_id = array(
"id" => 383
);
$count = 0;
foreach ($query->result() as $row)
{
$count = $count + 1;
$loop_number[$count] = $row->id;
}
$this->db->from('occupation');
$this->db->where_in('id',implode("','",$loop_number[$count]));
$query = $this->db->get();
foreach ($query->result() as $row)
{
echo $row->id;
}
echo 'Total Results: ' . $query->num_rows();
}
THANKS ALOT
The second parameter to where_in() should be an array.
You are generating a string with implode() and only of the last value of the array instead of the whole array.
So all you need is:
$this->db->where_in('id', $loop_number);
And I don't see where $query comes from, it seems to be undefined when you use it in the first loop in your method.
Apart from that you should initialize your variable, $loop_number = []; before the loop.

different result by 'while' and 'foreach'

When looping through array using the 'while' I get good results, but when I do the same with foreach I don't get the same. The SELECT statement returns two rows:
731
732
$data = $mysqli->query("SELECT ...");
while($item = $data->fetch_array())
{
echo $item[0]."<br />";
}
This returns:
731
732
foreach ($data->fetch_array() as $item)
{
echo $item."<br />";
}
This returns:
731
731
What am I doing wrong in second aproach with 'foreach'?
Thank you very much!
That's because the array represents a single row in the returned result set. In your foreach loop, $data->fetch_array() will only ever be executed once and that will hold only a single row (which is 731). You'll have to cal fetch_array() again to get the next record. You'll have to use a while loop to get the records and push them into an array first, and then use foreach:
$resultset = array();
while($item = $data->fetch_array()) {
$resultset[] = $item;
}
foreach ($resultset as $row) {
echo $row;
}
Every time whe you call the fetch_array method, it returns one row from the DB as an array with the values of the fields.
So it does NOT return all the rows, but all the fields in one row.
And when you call it again, it will return array with the fields in the second row.
When all the rows in the query are returned, fetch_array() returns FALSE. In the while loop the $row variable becomes false and the lopp stops.

Search PHP Array from results of SQL

I currently have an array in php that stores product names, normally 5 - 10. I would like to create something where the results of a SQL query is matched against the array to make sure they are all correct, and if not to show an error.
So far I have put the results into an array, then ran the query to get the results. I believe I need to put some sort of while loop with the results of the query and check the array in that while loop?
You have the array with the product name($products) and the results of the query($row).
While you loop trough the results you can check if the element is present,otherwise echo error and break the loop:
While(...) {
if(!in_array($row['retrivedprod'],$products)) {
echo 'error';
break;
}
}
You can cycle while receiving results from db with a while loop and checking results with the in_array function http://php.net/manual/en/function.in-array.php
$availability = 0;
$cart_products = array("book", "album");
$available_products = array();
while($row = mysql_fetch_array($result)) {
$available_products[] = $row['product'];
}
foreach($cart_products as $key => $value){
if (in_array($value, $available_products)){
$availability = 1;
}
}

php mysql data only shows one row with foreach loop

ok i have this and it doesn't show all the rows when fetched from mysql database its like this:
mysql_select_db($database_config, $config);
$query_cat = "SELECT * FROM cat";
$cat = mysql_query($query_cat, $config) or die(mysql_error());
$row_cat = mysql_fetch_array($cat);
$arr = array("TIT" => $row_cat['title'],
"DES" => $row_cat['description']);
foreach($arr as $ar){
echo $ar;
}
now it only displays the first row and then stops why is it not displaying all the fields and i don't wanna use while loop for it can anyone explain me the problem??
EDIT: Well basically i want to work it like this
$p = "{TIT}{DES}";
foreach($arr as $k => $p){
$p = str_replace("{".$k."}", $v, $p);
echo $p;
}
Now the problem is not with str_replace its with the loop or database because the database rows are not incrementing and displays only one data.
This will always return the first row. you are fetching only once that will return only first row.
Instead you must fetch all the rows using fetch statement in loop
while($row_cat = mysql_fetch_array($cat)){
echo $row_cat['title'];
echo $row_cat['description'];
}
From PHP mysq_fetch_array documentation:
"Returns an array that corresponds to the fetched row and moves the internal data pointer ahead"
As far as I know, you cannot retrieve every row without using a loop. You should do something similar to this:
while($row_cat = mysql_fetch_array($cat)){
echo $row_cat['title'];
echo $row_cat['description'];
}

Categories